简体   繁体   中英

Passing socket objects between processes, TCP, Java

Ok so I've found out Sockets are not serializable... so I cant pass them over TCP...

My problem is I have a homework assignment where I have 10 servers that must listen on one socket(lets call it request). For input from any of x number of clients that write to that socket. Then after one of the server processes reads a message from request it must communicate with that client over its own socket...

I tried making each server socket and the request socket on the server side, then passing those to the clients when they connected to the server... but this doesn't work...

Any tips on how I might do this? Having TCP not be 1-1 is really toying with me here.

Passing a socket over a TCP connection is like trying to pass a telephone over a telephone call, or trying to fax your fax machine. What you need to do is organize another connection between the parties concerned.

EDIT: In fact your assignment as stated doesn't even make sense:

I have 10 servers that must listen on one socket(lets call it request).

That's not even correct terminology. Servers listen at ports, not sockets, and 10 servers listening at one port is impossible. They must each have their own port.

For input from any of x number of clients that write to that socket.

See above. Clients don't write to 'that socket'. They create their own socket that is connected to the server port, and they write to that.

Then after one of the server processes reads a message from request it must communicate with that client over its own socket

If the server has received a connection from a client it already has a socket representing its endpoint to that connection. So all the server has to do is write the response back to the same socket it read the request from.

In short you have a major terminology problem, but you don't have a software problem at all.

Passing sockets seems crazy to me. If you're trying to write a better server, you'll have a hard time beating Netty . I'd recommend giving it a look.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM