Hi,
I am trying to get a client-server connection established using sockets and serversockets. The serversocket accepts the client's socket as expected but when trying to send a message with the client's socket to the socket created by the accept() method in the server something is not right and the message is not delivered leading to a deadlock situation. I am posting my socket code below hoping that someone can tell me what I am doing wrong. Let me know if you need more info. Thanks!
Server:
while(true){
try{
System.out.println("waiting for new client");
acceptSoc = serverSoc.accept();
System.out.println("Client accepted");
System.out.println("Local IP:" + acceptSoc.getLocalAddress());
System.out.println("Local port:" + acceptSoc.getLocalPort());
System.out.println("Remote IP:" + acceptSoc.getInetAddress());
System.out.println("Remote port:" + acceptSoc.getPort());
writer = new PrintWriter(acceptSoc.getOutputStream());
reader = new BufferedReader(new InputStreamReader(acceptSoc.getInputStream()));
System.out.println("Waiting to receive from client");
String buffer;
buffer = reader.readLine();
while ((buffer = reader.readLine()) != null)
System.out.println(buffer);
writer.println("ACKNOWLEDGEMENT");
}catch(Exception e) { System.err.println(e); break;}
}
Client:
try{
//Create socket
soc = new Socket(serverIP, serverPort);
System.out.println("Local IP:" + soc.getLocalAddress());
System.out.println("Local port:" + soc.getLocalPort());
System.out.println("Remote IP:" + soc.getInetAddress());
System.out.println("Remote port:" + soc.getPort());
//Create writer
writer = new PrintWriter(soc.getOutputStream());
//Create reader
reader = new BufferedReader(new InputStreamReader(soc.getInputStream()));
//Writes to server
writer.println("Testmessage");
//Receives from server
System.out.println("Waiting to receive from server");
String buffer = reader.readLine();
}catch(Exception e){ System.err.print(e); }
Server printouts:
waiting for new client
Client accepted
Local IP:/127.0.0.1
Local port:5454
Remote IP:/127.0.0.1
Remote port:1144
Waiting to receive from client
Client printouts:
Local IP:/127.0.0.1
Local port:1144
Remote IP:localhost/127.0.0.1
Remote port:5454
Waiting to receive from server
/H
I am trying to get a client-server connection established using sockets and serversockets. The serversocket accepts the client's socket as expected but when trying to send a message with the client's socket to the socket created by the accept() method in the server something is not right and the message is not delivered leading to a deadlock situation. I am posting my socket code below hoping that someone can tell me what I am doing wrong. Let me know if you need more info. Thanks!
Server:
while(true){
try{
System.out.println("waiting for new client");
acceptSoc = serverSoc.accept();
System.out.println("Client accepted");
System.out.println("Local IP:" + acceptSoc.getLocalAddress());
System.out.println("Local port:" + acceptSoc.getLocalPort());
System.out.println("Remote IP:" + acceptSoc.getInetAddress());
System.out.println("Remote port:" + acceptSoc.getPort());
writer = new PrintWriter(acceptSoc.getOutputStream());
reader = new BufferedReader(new InputStreamReader(acceptSoc.getInputStream()));
System.out.println("Waiting to receive from client");
String buffer;
buffer = reader.readLine();
while ((buffer = reader.readLine()) != null)
System.out.println(buffer);
writer.println("ACKNOWLEDGEMENT");
}catch(Exception e) { System.err.println(e); break;}
}
Client:
try{
//Create socket
soc = new Socket(serverIP, serverPort);
System.out.println("Local IP:" + soc.getLocalAddress());
System.out.println("Local port:" + soc.getLocalPort());
System.out.println("Remote IP:" + soc.getInetAddress());
System.out.println("Remote port:" + soc.getPort());
//Create writer
writer = new PrintWriter(soc.getOutputStream());
//Create reader
reader = new BufferedReader(new InputStreamReader(soc.getInputStream()));
//Writes to server
writer.println("Testmessage");
//Receives from server
System.out.println("Waiting to receive from server");
String buffer = reader.readLine();
}catch(Exception e){ System.err.print(e); }
Server printouts:
waiting for new client
Client accepted
Local IP:/127.0.0.1
Local port:5454
Remote IP:/127.0.0.1
Remote port:1144
Waiting to receive from client
Client printouts:
Local IP:/127.0.0.1
Local port:1144
Remote IP:localhost/127.0.0.1
Remote port:5454
Waiting to receive from server
/H