Hi, I have am trying to write a simple client/server, where the client
reads in a line of text and sends it to the server. the server
capitalizes the text and sends it back...but the server is never
finishing reading from the client socket input stream...any ideas?
client (snippet):
// connect to the server
sock = new Socket("127.0.0.1", 8000);
byte[] bytes = msg.getBytes();
// send the user input to the server
BufferedOutputStream bos = new
BufferedOutputStream(sock.getOutputStream());
bos.write(bytes);
bos.flush();
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
server (snippet):
ServerSocket sock = new ServerSocket(8000);
while (serving) {
try {
Socket client = sock.accept();
int val = -1;
BufferedInputStream bis = new
BufferedInputStream(client.getInputStream());
while ((val = bis.read()) != -1) {
System.out.print((char) val);
}
System.out.println("done reading from client...");
BufferedOutputStream bos = new
BufferedOutputStream(client.getOutputStream());
bos.write("GOT IT!".getBytes());
bos.flush();
client.close();
} catch (IOException e) {
e.printStackTrace();
serving = false;
} finally {
if (sock != null) {
try {
sock.close();
} catch (IOException e) {}
}
}
}
sock.close();
thanks
Gordon Beaton - 02 Oct 2006 18:19 GMT
> Hi, I have am trying to write a simple client/server, where the
> client reads in a line of text and sends it to the server. the
> server capitalizes the text and sends it back...but the server is
> never finishing reading from the client socket input stream...any
> ideas?
Your server reads until EOF on the client connection, i.e. it will
read until the client *closes* the OutputStream (or does
shutdownOutput). Perhaps you can see why that might be a problem if
the client intends to communicate more with the server after sending
the initial request.
I'd suggest changing the protocol slightly, for example the server
could read a single *line* of text (look at
BufferedInputStream.readLine()).
/gordon

Signature
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Java and Swing - 02 Oct 2006 19:08 GMT
> I'd suggest changing the protocol slightly, for example the server
> could read a single *line* of text (look at
> BufferedInputStream.readLine()).
well i changed the client to do....
BufferedOutputStream bos = new
BufferedOutputStream(sock.getOutputStream());
bos.write(bytes);
bos.write("\n".getBytes());
bos.flush();
and the server to do
String line = null;
BufferedReader reader = new BufferedReader(new
InputStreamReader(client.getInputStream()));
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
...same problem however.
Gordon Beaton - 02 Oct 2006 19:14 GMT
> ...same problem however.
Same mistake too. Your server doesn't send a reply until it has read
to EOF from the client (when readLine() returns null). Send the reply
after each line instead. At EOF, close the client connection.
/gordon

Signature
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Joshua Jung - 02 Oct 2006 22:42 GMT
>> ...same problem however.
>
[quoted text clipped - 3 lines]
>
> /gordon
Hey thanks guys for your help! I figured out what was wrong:
Problem 1. I was not allocating enough heap memory (the -Xmx option for
java JRE) for my server.
Problem 2. I had to set the maximum file descriptors to a much higher
number (using the UNIX ulimit -S -n command).
I think the reason things were so hard to track was because I was having
problems with both of these issues.
Once I did both these things... I am now cranking out thousands of
connections with no problem.
Thanks again!
Josh <><
Matt Humphrey - 02 Oct 2006 18:27 GMT
> Hi, I have am trying to write a simple client/server, where the client
> reads in a line of text and sends it to the server. the server
[quoted text clipped - 34 lines]
> bos.write("GOT IT!".getBytes());
> bos.flush();
The server is waiting for EOF which the client does not send. Flush does
not send an EOF and the absence of data does not constitute EOF. Only
closing the stream does that, which you may or may not want to do. If you
are intendeding to keep the stream open, you must either send some marker
(e.g. CR/LF) to tell the server to stop reading or send a length to tell it
how much to read. TCP is stream-oriented, not packet oriented.
Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/