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!!