> I execute the server and it waits for petitions. Then, i execute the
> client and everything goes fine. The server does his work and waits
> another client. The problem comes when i execute another client. The
> problem is this: java.net.ConnectException: Connection refused

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
First of all thanx for your interest :)
I've closed the socket aClient in the function Client.receive and not the
listener but goes wrong yet.
When i execute the client the second time it remains in stand by as the
server. If i close(^C) the client and execute it again(only the client), it
conects with the server but the server returns nothing.
I think it's a socket that lost the comunication after the first client is
executed and this is why i can't use it again.
But i don't know how to fix it ;(
> > I execute the server and it waits for petitions. Then, i execute the
> > client and everything goes fine. The server does his work and waits
[quoted text clipped - 9 lines]
> [ do not email me copies of your followups ]
> g o r d o n + n e w s @ b a l d e r 1 3 . s e
Gordon Beaton - 07 Dec 2004 19:25 GMT
> I've closed the socket aClient in the function Client.receive and
> not the listener but goes wrong yet. When i execute the client the
[quoted text clipped - 3 lines]
> lost the comunication after the first client is executed and this is
> why i can't use it again. But i don't know how to fix it ;(
Your code and your explanation are unclear.
Regardless, here are some tips:
- in the server, separate the part where you accept incoming
connections to the ServerSocket, from the part where you handle a
single connection (the Socket returned by accept()).
- in your your earlier example, it appears from send() that the server
connects to the client in order to send data. If the client has
connected to the server, the server can send the data using the same
socket connection. There is no need to make a second connection for
data in the reverse direction.
- if you handle each client in a separate thread, the server can
accept new connections without waiting for earlier ones to finish.
- when you read from an InputStream, there is no guarantee that you
will read all of the requested data at once. This is especially true
when the stream comes from a Socket. You may need to read several
times. Check the return value from read(). Or, use the readFully()
method of DataInputStream.
- have a look at the Java tutorial. There is a simple example of a TCP
client and server in the networking trail:
http://java.sun.com/docs/books/tutorial/networking/index.html
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e