Client/Server Socket problem

J

Java and Swing

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
 
G

Gordon Beaton

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
 
M

Matt Humphrey

Java and Swing said:
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();

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 (e-mail address removed) http://www.iviz.com/
 
J

Java and Swing

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.
 
G

Gordon Beaton

...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
 
J

Joshua Jung

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top