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.