• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

[SOLVED] Can I pass sockets to different threads?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
He was expelled for perverse baking experiments. This tiny ad is a model student:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic