Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Unable to Implement Simple Echo Server & Client

JavaprogJul 16 2012 — edited Jul 16 2012
When i'm running my Server 1st & running Client later , Client is getting connected but the message from client is not getting passed to Server

Here is the code for client & Server.

Code for Client:
     import java.io.*;
     import java.net.*;
     public class EchoClient {
	public static void main(String[] args) throws IOException 
	{
		Socket echoClient=null;
		BufferedReader br= null;
		PrintWriter pwr;
		try 
		{
			
			echoClient= new Socket("localhost",9999);
			pwr = new PrintWriter(echoClient.getOutputStream());
			br= new BufferedReader(new InputStreamReader(echoClient.getInputStream()));
			pwr.println("This is Client");
			System.out.println("Message received from server is ");
			while (br.ready())
			{
				System.out.println(br.readLine());
			}
			
		
		}
		catch(UnknownHostException uhe)
		{
			uhe.printStackTrace();
		}
		//Write Data into Socket
		echoClient.close();
			
		
	}
	
	
	
}
Code for Server:
     import java.io.*;
     import java.net.*;
     public class EchoServer {
  
	
	public static void main(String[] args) throws IOException
	{
		ServerSocket srvSock;
		Socket client;
		BufferedReader bfr;
		PrintWriter pw;
		
		try
		{
			srvSock= new ServerSocket(9999);
			System.out.println("Server Started & Listening for Client request\n");
			client= srvSock.accept();
			System.out.println("Client Connected!\n");
			bfr =new BufferedReader (new InputStreamReader(client.getInputStream()));
			System.out.println("Message from client is : "+bfr.readLine());
			pw= new PrintWriter(client.getOutputStream());
			pw.println("Message received in Server");
		}
		catch (SocketException se)
		{
			se.printStackTrace();
			
		}
		
		
		
		}
	}
I'm unable to fish out what exactly is happening.Plz Explain me the problem in detail.

Edited by: sabre150 on Jul 16, 2012 2:09 PM

Moderator action : { code} tags added. Please add them yourself in the future.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 13 2012
Added on Jul 16 2012
9 comments
1,399 views