在进程之间传递套接字对象、TCP、Java
好吧,我发现套接字不可序列化...所以我无法通过 TCP 传递它们...
我的问题是我有一项家庭作业,其中我有 10 台服务器必须侦听一个套接字(让我们称之为请求)。用于来自写入该套接字的 x 个客户端中的任意一个的输入。然后,在其中一个服务器进程从请求中读取消息后,它必须通过自己的套接字与该客户端进行通信...
我尝试在服务器端创建每个服务器套接字和请求套接字,然后在客户端连接时将它们传递给客户端服务器...但这不起作用...
有关我如何执行此操作的任何提示? TCP 不是 1-1 真的是在玩弄我。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过 TCP 连接传递套接字就像尝试通过电话呼叫传递电话或尝试向传真机发送传真一样。您需要做的是在有关各方之间组织另一个联系。
编辑:事实上,您所说的作业甚至没有意义:
这甚至不是正确的术语。服务器监听端口,而不是套接字,10 个服务器监听一个端口是不可能的。他们每个人都必须有自己的端口。
见上文。客户端不会写入“那个套接字”。他们创建自己的连接到服务器端口的套接字,然后对其进行写入。
如果服务器已收到来自客户端的连接,则它已经有一个代表该连接端点的套接字。因此,服务器所要做的就是将响应写回到它读取请求的同一个套接字。
简而言之,您遇到了主要的术语问题,但根本没有软件问题。
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:
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.
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.
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.
传递套接字对我来说似乎很疯狂。如果您想编写一个更好的服务器,那么您将很难击败 Netty。我建议看一下。
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.