0

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

hudson
  • 21
  • 1
  • 8
  • You've got a null value somewhere in the server side code while in runtime. I don't have line numbers here but it's line 29. If I had to guess from the code shown, I'd first figure that `request` is probably the culprit – jseashell Feb 20 '17 at 15:45
  • yes socket.receive(request); – hudson Feb 20 '17 at 15:47
  • 1
    Don't use empty catch blocks! Put an `e.printStackTrace();` where you're catching the `SocketException` and you'll find your *real* error. – Kayaman Feb 20 '17 at 15:52
  • Address already in use: Cannot bind – hudson Feb 20 '17 at 16:01
  • Well there's your problem. The `NullPointerException` is just a side-effect of you ignoring an exception, which is a very bad thing to do. – Kayaman Feb 20 '17 at 16:04

0 Answers0