I have written a client-server TCP Socket program such that the client will enter a string and that will be displayed by the server(echo program)... the condition is when I enter bye the program should exit... Well and good the client program is exiting ... but the server program is exiting with a java.io.DataInputStream.readUTF() and readFully exceptions.. plz help me.. whats wrong in the code...
/*Client*/
import java.net.*;
import java.io.*;
public class echoclient {
public static void main(String args[]) throws Exception
{
Socket soc = new Socket("127.0.0.1",4000);
OutputStream sout = soc.getOutputStream();
DataOutputStream out = new DataOutputStream(sout);
DataInputStream din = new DataInputStream(System.in);
while(true)
{
String message = din.readLine();
if(message.equals("bye"))
break;
out.writeUTF(message);
}
soc.close();
}
}
/*Server*/
import java.io.*;
import java.net.*;
public class echoserver {
public static void main(String args[]) throws Exception
{
ServerSocket server = new ServerSocket(4000);
Socket soc = server.accept();
System.out.println("Server Started....");
InputStream sin = soc.getInputStream();
DataInputStream in = new DataInputStream(sin);
while(true)
{
String fromClient = in.readUTF();
if(fromClient.equals("bye"))
{break;}
System.out.println("Client says : "+fromClient);
}
soc.close();server.close();
}
}
Exception:
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:340)
at java.io.DataInputStream.readUTF(DataInputStream.java:589)
at java.io.DataInputStream.readUTF(DataInputStream.java:564)
at echoserver.main(echoserver.java:15)