Hello Members,

The following Client.java and Server.java works fine:

Client.java

import java.net.*;
import java.io.*;
   
   public class client
   {
     public static void main (String args[])
        throws IOException
     {
       Socket sock = new Socket(InetAddress.getLocalHost (), 2000);
       BufferedWriter dataout;
       String Message = "How are you?";
       BufferedReader datain;
       System.out.println ("Sending "+ Message + " ...");
   
       dataout = new BufferedWriter (new OutputStreamWriter (sock.getOutputStream ()));
       dataout.write (Message, 0, Message.length ());
       dataout.flush ();
       sock.close ();
     }
   }

Server.java:

import java.net.*;
   import java.io.*;
   
   public class server
   {
     public static void main (String args[])
        throws IOException
     {
       ServerSocket s = new ServerSocket (2000);
       Socket sock;
       BufferedReader datain;
       BufferedWriter dataout;
       String Message = "who are you??";
       
       System.out.println ("Server starting ...");
       sock = s.accept ();
   
       datain = new BufferedReader (new InputStreamReader (sock.getInputStream ()));
       System.out.println (datain.readLine ());
       
       
       sock.close ();
       s.close ();
     }
   }

Now, I added the following code such that upon receving a message from the Client, the Server then sends a message back to the Client, which the Client then displays. When the I ran the code below, both the Client and Server windows are blank. Can you tell me my errors?

Client.java

import java.net.*;
import java.io.*;
   
   public class client
   {
     public static void main (String args[])
        throws IOException
     {
       Socket sock = new Socket(InetAddress.getLocalHost (), 2000);
       BufferedWriter dataout;
       String Message = "Howdy!";
       BufferedReader datain;
       System.out.println ("Sending "+ Message + " ...");
   
       dataout = new BufferedWriter (new OutputStreamWriter (sock.getOutputStream ()));
       dataout.write (Message, 0, Message.length ());
       dataout.flush ();
       datain = new BufferedReader (new InputStreamReader (sock.getInputStream ()));
       System.out.println (datain.readLine ());
   
       sock.close ();
     }
   }

Server.java

import java.net.*;
   import java.io.*;
   
   public class server
   {
     public static void main (String args[])
        throws IOException
     {
       ServerSocket s = new ServerSocket (2000);
       Socket sock;
       BufferedReader datain;
       BufferedWriter dataout;
       String Message = "who are you??";
       
       System.out.println ("Server starting ...");
       sock = s.accept ();
   
       datain = new BufferedReader (new InputStreamReader (sock.getInputStream ()));
       System.out.println (datain.readLine ());
       dataout = new BufferedWriter (new OutputStreamWriter (sock.getOutputStream ()));
       dataout.write (Message, 0, Message.length ());
       dataout.flush ();
       
       
       
       sock.close ();
       s.close ();
     }
   }

Thank you!!

Recommended Answers

All 3 Replies

Just a guess, but since these are running on the same machine the server may be sending the response before the client has opened its input stream?
You could try opening both the input and output streams before starting to write anything.

Hello,

Ezza - Thanks for the link.

James - Thanks for the suggestion. I will try that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.