0

Hi and thanks in advance,

I don't think my question is a duplicate of the suggested one. I know about nullpointer exceptions, but I don't know how to solve this issue (assuming the root is a nullpointer exception or isn't).

So I'm trying to set up client to client communication, and I'm having some trouble.

So currently I'm having one act as a server and the other a client (for messages in one direction), and I'm struggling to get the server to send confirmation of the connection back to the client in the form of a simple text message.

Here is my code below. My code at the moment makes the client send a simple hello message to the server which works. The commented out code is my latest attempt to send a connection confirmation message back to the client, but it hasn't worked.

Here is the server code:

 try
                    {
                        //SeederPanel.setVisible(true);
                        //LeecherPanel.setVisible(false);

                        //waiting to be connected to
                        System.out.println("Seeder waiting for leecher..");
                        DatagramSocket SeederSocket = new DatagramSocket(2345);
                        byte[] SeederBuffer = new byte[512];
                        DatagramPacket DGP = new DatagramPacket(SeederBuffer,SeederBuffer.length);

                        //receiving connection attempt
                        String message = "";
                        SeederSocket.receive(DGP);
                        message = new String(SeederBuffer);

                        System.out.println(message);

                        /*
                        //Response to connection attempt
                        String returnGreeting = "You have successfully connected to the seeder!";
                        SeederBuffer = returnGreeting.getBytes();
                        DatagramPacket DGPResponse = new DatagramPacket(SeederBuffer,SeederBuffer.length);
                        SeederSocket.send(DGPResponse);
                        */

                        if(message=="Hello from the leecher")
                        {
                            NotifyLeecherConnect();
                        }


                    }
                    catch (SocketException e)
                    {
                        e.printStackTrace();
                    }
                    catch (UnknownHostException e)
                    {
                        e.printStackTrace();
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }   

And here is the client code:

try 
                {
                    //attempting to connect
                    InetAddress MyAddress = InetAddress.getByName(txtHostAddress.getText());
                    DatagramSocket LeecherSocket = new DatagramSocket();

                    String Greeting = "Hello from the leecher";
                    byte[] LeecherBuffer = Greeting.getBytes();

                    DatagramPacket dp = new DatagramPacket(LeecherBuffer,LeecherBuffer.length,MyAddress,Integer.parseInt(txtPort.getText()));
                    LeecherSocket.send(dp);

                    System.out.println("Activation test");

                    //????

                    /*
                    //Receiving successful connection response
                    DatagramPacket DGPReceiveResponse = new DatagramPacket(LeecherBuffer,LeecherBuffer.length);

                    String message = "";
                    LeecherSocket.receive(DGPReceiveResponse);
                    message = new String(LeecherBuffer);

                    System.out.println(message);
                    */


                    //LeecherSocket.close();
                } 
                catch (UnknownHostException e1) 
                {
                    e1.printStackTrace();
                } 
                catch (SocketException e1) 
                {
                    e1.printStackTrace();
                } 
                catch (IOException e1)
                {
                    e1.printStackTrace();
                }

Also, in the code just to avoid any confusion: the seeder is the server and leecher is the client.

Any ideas? Thank you.

Impact
  • 25
  • 9
  • 2
    What is the error message you get when you try to send a message back? Also: `if(message=="Hello from the leecher")` will not work, you have to use `equals()` when you want to compare objects, not `==`. – Progman Sep 11 '17 at 10:49
  • The code for: `if(message=="Hello from the leecher") { NotifyLeecherConnect(); }` is part of another attempt. NotifyLeecherConnect()i is currently an empty method. – Impact Sep 11 '17 at 10:53
  • As for your other question. If I uncomment the commented code, and use the program, it doesn't even let the clients message to the server through first off, and here is the error code: `Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: null address || null buffer` – Impact Sep 11 '17 at 11:14
  • I don't think its a duplicate. I know about nullpointer exceptions, but I don't know how to solve this issue (assuming the root is a nullpointer exception). I forgot to show you where it says the errors happened. `at ClientFrame1$1.actionPerformed(ClientFrame1.java:83)`. That points to this line: `SeederSocket.send(DGPResponse);` – Impact Sep 11 '17 at 11:46

0 Answers0