aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes client-serer...plz help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "client-serer...plz help" Watch "client-serer...plz help" New topic
Author

client-serer...plz help

kriti sharma
Ranch Hand

Joined: Feb 16, 2001
Posts: 160
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

Joined: Jul 03, 2001
Posts: 2120
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.


SCJP2. Please Indent your code using UBB Code
kriti sharma
Ranch Hand

Joined: Feb 16, 2001
Posts: 160
thank you so much, my code is working now.thanks a lot!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: client-serer...plz help