client socket binding to the same local/remote addresses
Hi all,
My Java program binds client socket to local address, local port, remote address and remote port. after that from different thread the program creates new socket and bind it to the same address as the first one. The question is: is there possibility that the second socket will be copy as the first one (with the same input/output streams) so that two threads in fact use the same TCP connection.
(Operating system is HP).
Thank you,
Kate
[477 byte] By [
takitakia] at [2007-10-2 20:07:40]

No there's no chance of two sockets using eachother's input/output streams. But you must be doing something wrong because it's not possible to bind two sockets to one and the same host/port pair. You should get an IOException ("Address already in use" or something similar). Are you sure you're catching the exception?
Also, why are you manually binding (client) sockets to specific local hort/ports? What are you trying to do?
The situation is following: there is hash table of different connection (to different IP/ports). When some thread wants to send something to a remote machine it pick ups connection from the hash table, if it doesn't exist - creates one. each connection has a socket, bind to particular local/remote adress. It seems that there is a bug so that although there is existing connection at the hash table, the new one created. when I run test on Solaris10 I don't get "Address is already in use".
I have no need to store streams, my problem that each connection has it's own messages numbers(the application implements some protocol over TCP). so if a message number 1 was sent by some connection, and when different thread wants to send a message to the same remote host, it tries to pick up existing connection, but because of a bug creates the new one, which sends the message with number 1 also. And I see that there are situation whent the first thread receives response to the message number 1 althougth it was response to the message number 1 that the second thread has sent. So my question is if there is any possibility of this absurd situation?
> And I see that there are situation whent the first thread receives response to the message number 1 althougth it was response to the message number 1 that the second thread has sent.
It looks like your first thread works with connection (he's able to receive) so how are you going to use connection in second thread while connection still in use by first thread?
I think architecture of your program should be reviewed...
> the application has been working for years on the
> small number of remote machines, but now we can see
> problems that were transparent for us before. So you
> say that there is no legal way for sockets to share
> the same streams.
It probably worked before because you never had two connections communicating with your program concurrently. Simply put, i think your program is flawed. Why not just use a thread per socket and deal with them as seperate entities rather than some complicated stream sharing scheme (which afaik isnt actually possible to begin with).