Socket Reading Error

I had a basic server and client set up: a server in c and client in python. The client would take in input and send it to the server. When the server recieved data from the client, it would echo it back to the client.

I decided to move the client into Java: now everything works except recieving messages from the server. When it gets to System.exit(0), it suddenly prints out all the messages in a clump and exits with an error. The error isn't as much of a problem as the whole clump thing, though:

la

laa

laaaa

quit

Cleaning up... done.

Good-bye!

Server said: lalaalaaaa

Exception in thread"Thread-0" java.util.NoSuchElementException

at java.util.Scanner.throwFor(Scanner.java:817)

at java.util.Scanner.next(Scanner.java:1317)

My code for initializing the Scanner:

socketIn =new Scanner(System.in);

try{

socketIn =new Scanner(clientSock.getInputStream());

}

catch (IOException e)

{ System.out.println("There was an error initializing the input stream. You will be able to send messages, but not recieve them.");

}

The code for reading from the socket inputstream:

new Thread(new SocketReadThread()).start();

...

privateclass SocketReadThreadimplements Runnable

{

publicvoid run()

{

String s ="";

while((s = socketIn.next()) !=null)

System.out.println("Server said: " + s);

}

}

Please help me!

Thanks,

Andy

[2271 byte] By [Zyphona] at [2007-10-3 6:24:59]
# 1
The reading doesn't work even when I take it out of the Thread and call it just once... and the Socket's InputStream's available() method returns zero until the end.
Zyphona at 2007-7-15 1:10:55 > top of Java-index,Core,Core APIs...
# 2

Try this (you'll need a BufferedReader):

private class SocketReadThread implements Runnable

{

public void run()

{

String s = "";

Boolean done = false;

while(!done)

{

s = socketIn.readLine()

if (in.equals("quit")

{

done = true;

}

else

{

System.out.println("Server said: " + s);

}

}

}

}

Let me know if you need anymore help. Regards,

doy2001a at 2007-7-15 1:10:55 > top of Java-index,Core,Core APIs...