简体   繁体   English

在进程,TCP,Java之间传递套接字对象

[英]Passing socket objects between processes, TCP, Java

Ok so I've found out Sockets are not serializable... so I cant pass them over TCP... 好的,所以我发现套接字不可序列化...因此我无法通过TCP传递它们...

My problem is I have a homework assignment where I have 10 servers that must listen on one socket(lets call it request). 我的问题是我有一个家庭作业,其中有10台服务器必须在一个套接字上侦听(请称为请求)。 For input from any of x number of clients that write to that socket. 用于从x个写入该套接字的客户端中的任何一个输入。 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. TCP不是1-1确实是我在玩弄。

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. 通过TCP连接传递套接字就像尝试通过电话传递电话或尝试传真传真机一样。 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). 我有10个必须在一个套接字上侦听的服务器(让我们称之为请求)。

That's not even correct terminology. 这甚至不是正确的术语。 Servers listen at ports, not sockets, and 10 servers listening at one port is impossible. 服务器侦听端口而不是套接字,并且不可能有10个服务器侦听一个端口。 They must each have their own port. 它们必须各自具有自己的端口。

For input from any of x number of clients that write to that socket. 用于从x个写入该套接字的客户端中的任何一个输入。

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 . 如果您要编写更好的服务器,将很难击败Netty I'd recommend giving it a look. 我建议看看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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