• Post Reply Bookmark Topic Watch Topic
  • New Topic

[SOLVED] Can I pass sockets to different threads?  RSS feed

 
Marco Bianchi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hallo, I am writing a client/server program and I am having troubles passing sockets among classes.
Basically, the client connects to a login manager, which checks credentials and then passes the socket to another class for further communication.
If i close the DataInputStream and the DataOutputStream, I get a "socket closed" exception when i try to open new ones in the "other" class (although I did not close the socket itself), whereas if I do not close the streams, I get a EOFException.
Maybe my login manager thread closes the socket when it dies, even though that socket is referenced in another thread?
I am starting to think that it is not possible to achieve what I was trying to do and that I'd need to open a new socket, but I'd rather not if possible so please let me know if it can be done...
I am not posting the code now because it's quite long, but I'll certainly will if needed.

Thanks in advance!
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you close the streams that closes the socket. Unless you need the socket for some other reason, all you need to do is pass the IO stream references.\

Here is simple code to show it:



output:

 
Marco Bianchi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes, actually I had overlooked a couple of close() in the client...
So that part works now, even passing the reference of the entire socket and not just the streams.

Thanks a lot!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!