This discussion is archived
1 Reply Latest reply: Jul 14, 2012 4:40 PM by EJP RSS

Data not passing between Client & Server

Javaprog Newbie
Currently Being Moderated
Hello,
My Java Version is: 1.7.0_05. PLatform: Windows 7 Ultimate SP1, x64 Environment.

I've used a Free port. And here's the problem:

I've written a simple Client & Server Program. The Client initiates the communication and when server accepts the client connection, the Client simply sends a message to server.There Server reads the message & displayed on console & then it gives response to Client.And that server's response is read by client & displayed on console. I.e this is a TRADITIONAL Echo Server Program.

Though this is a simple task I'm unable to understand the reason for it.

Here's the Code for SERVER:
import java.io.*;
import java.net.*;
public class EchoServer {

     public static void main(String[] args) throws IOException
     {
          
          ServerSocket srvSock = new ServerSocket(2550);

          System.out.println("Server socket created");

          Socket client = srvSock.accept();

          System.out.println("Client connected");

          BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));

          System.out.println(br.readLine());

          PrintWriter pwr = new PrintWriter(client.getOutputStream());

          pwr.println("Message received by Server");

          br.close();

          pwr.close();

          client.close();

          srvSock.close();
     }
}     
And code of CLIENT :
import java.io.*;
import java.net.*;
public class EchoClient {
     public static void main(String[] args) throws IOException 
     {
          Socket client = new Socket("localhost",2550);

          PrintWriter pr = new PrintWriter(client.getOutputStream());

          pr.println("Hello from Client");

          BufferedReader bir = new BufferedReader(new InputStreamReader(client.getInputStream()));

          bir.readLine();

          pr.close();

          bir.close();

          client.close();

          }

     }
When i've stopeed running the Server i've got this Error:
Exception in thread "main" java.net.SocketException: Connection reset
     at java.net.SocketInputStream.read(Unknown Source)
     at java.net.SocketInputStream.read(Unknown Source)
     at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
     at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
     at sun.nio.cs.StreamDecoder.read(Unknown Source)
     at java.io.InputStreamReader.read(Unknown Source)
     at java.io.BufferedReader.fill(Unknown Source)
     at java.io.BufferedReader.readLine(Unknown Source)
     at java.io.BufferedReader.readLine(Unknown Source)
     at EchoClient.main(EchoClient.java:10)
Thanks in Advance :)

Edited by: Javaprog on Jul 15, 2012 2:28 AM

Edited by: Javaprog on Jul 16, 2012 7:09 PM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points