Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

client-serer...plz help

 
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();
}
}
 
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!
 
Seriously Rick? Seriously? You might as well just read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic