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

发布于 2024-12-07 13:57:57 字数 284 浏览 0 评论 0原文

好吧,我发现套接字不可序列化...所以我无法通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天涯沦落人 2024-12-14 13:57:57

通过 TCP 连接传递套接字就像尝试通过电话呼叫传递电话或尝试向传真机发送传真一样。您需要做的是在有关各方之间组织另一个联系

编辑:事实上,您所说的作业甚至没有意义:

我有 10 台服务器,必须在一个套接字上侦听(让我们称其为
请求)。

这甚至不是正确的术语。服务器监听端口,而不是套接字,10 个服务器监听一个端口是不可能的。他们每个人都必须有自己的端口。

用于来自写入该套接字的 x 个客户端中的任何一个的输入。

见上文。客户端不会写入“那个套接字”。他们创建自己的连接到服务器端口的套接字,然后对其进行写入。

然后,在其中一个服务器进程从请求中读取消息后
必须通过自己的套接字与该客户端通信

如果服务器已收到来自客户端的连接,则它已经有一个代表该连接端点的套接字。因此,服务器所要做的就是将响应写回到它读取请求的同一个套接字。

简而言之,您遇到了主要的术语问题,但根本没有软件问题。

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.

悍妇囚夫 2024-12-14 13:57:57

传递套接字对我来说似乎很疯狂。如果您想编写一个更好的服务器,那么您将很难击败 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文