Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

client-serer...plz help

 
kriti sharma
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am pasting the code of client.java and server.java
the problem is that the server does not receive message from the client,it just hangs and on terminating the sessions,runtime exception is shown on server which says ...connection reset by peer ....
please help!
client.java
--------
import java.io.*;
import java.net.*;
class Client
{
public static void main(String str[]) throws Exception
{
Socket s=new Socket("localhost",8080);
PrintWriter out=new PrintWriter(s.getOutputStream());
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedReader sysin=new BufferedReader(new InputStreamReader(System.in));
String userin,server;
while(!((userin=sysin.readLine()).equals("END")))
{
out.println(userin);
System.out.println("\n message sent to server");
}
while(!((server=in.readLine()).equals("END")))
{
System.out.println("from server"+server);
}
in.close();
sysin.close();
out.close();
s.close();
}


}

----------
server.java
------
import java.io.*;
import java.net.*;
class Server
{
public static void main(String str[]) throws Exception
{
ServerSocket ss= new ServerSocket(8080);
Socket client=null;
client=ss.accept();
System.out.println("client connection accepted");
PrintWriter out=new PrintWriter(client.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(client.getInputStream()));
out.println("server started");
String message;
while(!((message=br.readLine()).equals("END")))
{
out.println(message);
System.out.println("\nmessage sent to client");
}
out.close();
br.close();
client.close();
ss.close();
}
}
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to use the PrintWriter constructor that takes a boolean to specify the Writer flushes the output with each println.
Look at the two whiles in a row. There won't be any reception in the client from the server untill END is sent by the client.
 
kriti sharma
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much, my code is working now.thanks a lot!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic