This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer (Exam 1Z0-830) Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide and have Khalid Mughal and Vasily Strelnikov on-line!
See this thread for details.
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

client-serer...plz help

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you so much, my code is working now.thanks a lot!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic