开发者

Passing socket objects between processes, TCP, Java

开发者 https://www.devze.com 2023-04-09 18:17 出处:网络
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 re

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 migh开发者_运维技巧t 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消