This is my server side code
public class Rfc865UdpServer {
static DatagramSocket socket;
public static void main(String[] argv) {
try{
socket = new DatagramSocket (17); //open port 17 RFC865 quote of the day
} catch (SocketException e){}
while (true)
{
try{
byte[] buffer = new byte[1024]; //declare the buffer and the size of the buffer
byte[] sendData = new byte[1024];
DatagramPacket request = new DatagramPacket(buffer,buffer.length);
socket.receive(request);
String test = new String (request.getData());
InetAddress IPAddress = request.getAddress(); //get address of the source of the sender
int port = request.getPort(); // get the port of the sender
// System.out.println ("RECEIVED : " + test);
//3.send udp reply to client
String capSen = test.toUpperCase();
sendData = capSen.getBytes();
DatagramPacket reply = new DatagramPacket (sendData , sendData.length, IPAddress ,port); //creating packet containing the buffer for the quote, IP and port of the Destination.
socket.send(reply); //send packet created earlier using RFC865 udp
} catch (IOException e) {}
}
}
}
I console got an error:
Exception in thread "main" java.lang.NullPointerException at cnetwork.Rfc865UdpServer.main(Rfc865UdpServer.java:29)
Client side
public class Rfc865UdpClient {
static DatagramSocket socket;
public static void main(String[] argv){
// 1. Open UDP socket
try {
socket = new DatagramSocket ();
} catch (SocketException e){}
try {
//2. Send UDP request to server
byte[] buffer = new byte[1024];
byte [] sendData = new byte [1024];
String msg = new String ("hello there " + InetAddress.getLocalHost().getHostAddress());
sendData = msg.getBytes();
InetAddress dest = InetAddress.getByName("abc");
DatagramPacket request = new DatagramPacket(sendData,sendData.length, dest ,17);
socket.send(request);
//3. Receive UDP reply from server
DatagramPacket reply = new DatagramPacket (buffer, buffer.length);
socket.receive(reply);
String QOTD = new String (reply.getData());
System.out.println("From Server : "+ QOTD);
socket.close();
} catch (IOException e){}
}
}
Client has no error when I compile it