Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Client-Server socket problem

Status
Not open for further replies.

Stovio

Programmer
Mar 20, 2002
30
SE
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
 
The BufferedReader class will "end" a socket read on a "\n" character - which you are not sending, so your code will sit and do nothing forever.

--------------------------------------------------
Free Database Connection Pooling Software
 
Thanks for your answer sedj.
Simply adding "\n" to the message made the server code pass the readLine() call but it didn't fill the string "buffer" with anything. Also, the server's attempt to send the acknowledgement after receiving the test message was not received at all by the client (the "/n" was added to the message). Should I consider using another class then BufferedReader or some other writer than PrintWriter (i tried BufferedWriter and it's newLine() function without success). Did I do something else wrong?

This is really making me tired.. :/

server:
Code:
try{
	System.out.println("waiting for new client");
	acceptSoc = serverSoc.accept();
	
	pWriter = new PrintWriter(acceptSoc.getOutputStream(), true);
	bWriter = new BufferedWriter(new OutputStreamWriter(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("Received: '" + buffer + "'");					
	
	pWriter.println("ACKNOWLEDGEMENT\n");
	//bWriter.write("ACKNOWLEDGEMENT");
	//bWwriter.newLine();
	pWriter.flush();
		
}catch(Exception e) { System.err.println(e); break;}

client:
Code:
try{
	//Create socket
	soc = new Socket(serverIP, serverPort, homeIP, homePort);
	
	//Create writer
	pWriter = new PrintWriter(soc.getOutputStream(), true);
	bWriter = new BufferedWriter(new OutputStreamWriter(soc.getOutputStream()));
	
	//Create reader
	reader = new BufferedReader(new InputStreamReader(soc.getInputStream()));

	//Writes to server
	
	pWriter.println("Test message\n");
	//briter.write("Testmessage");
	//bWriter.newLine();
	pWriter.flush();

	//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

Waiting to receive from client
Received: ''

Client printouts:

Waiting to receive from server

/H
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top