从不同的进程通过套接字 (UDP) 回复客户端

Replying to a client over sockets (UDP) from a different process(从不同的进程通过套接字 (UDP) 回复客户端)

本文介绍了从不同的进程通过套接字 (UDP) 回复客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务器,而不是一个命令处理程序"进程.它通过 UDP 接收消息,并通过其发布的 API(无论该进程采用何种 IPC 机制)与该进程通信,从而将要做的工作委托给不同的进程.我们的系统有几个合作流程.然后将该 API 调用的结果从命令处理程序进程发送回客户端.

I have a server than is a "command handler" process. It receives messages over UDP, and delegates the work to do to a different process by communicating to that process through it's published API (whatever IPC mechanism that process employes). Our system has several cooperating processes. The result of that API call is then then sent back to the client from the command handler process.

一个命令是控制从另一个进程生成到客户端的数据流(连接"消息).

One command is to control a data stream that is generated from another process to the client (a "connect" message).

这应该有效吗?我将客户端的 IP 地址和端口号发送到另一个进程,该进程创建一个新套接字,并执行发送...我已经跟踪了代码,一切看起来都很好,但是客户端仍然被阻止接收...我知道如果我从命令处理程序发送到,它会得到响应,但不是来自新套接字.

Should this work? I send the IP address and port number of the client to the other process, that process creates a new socket, and does a sendto...I've traced through the code and everything looks good, but the client is still blocked on the receive...I know if I do a sendto from the command handler, it gets the response, but not from the new socket.

下面是一些示例代码:

#define LEN 10000
char buffer[LEN];
int sfd, nsfd, bread, addrsize;
struct sockaddr_in addr;
addrsize = sizeof (struct sockaddr_in);
server.sin_family = AF_INET;
server.sin_port = htons(5200);
server.sin_addr.s_addr = INADDR_ANY;
sfd = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
bind (sfd, (struct sockaddr*)&server, addrsize);
bread = recvfrom(sfd, buffer, LEN, 0, &addr, &addrsize);

/* send IP address and port number through the API to another process */
/* in that other process, I do something like this...addr has the IP
 * address and port in it from above: */

nsfd = socket (AF_INET, SOCK_DGRAM, IPROTO_UDP);
sendto(nsfd, buff, bread, 0, &addr, sizeof(addr));

所以请帮忙!

推荐答案

您的客户端(或介于两者之间的防火墙)可能会因为从不同的源端口接收响应而不是发送请求而感到困惑(因为操作系统会只需为新套接字选择一个随机源端口).

Your client (or a firewall in between) will likely get confused by receiving the response from a different source port than it sent the request to (as the OS will just pick a random source port for the new socket).

解决此问题的一种方法是在服务器中使用 SO_REUSEADDR 套接字选项,并在发送回复时显式绑定到正确的端口.但这也会产生不受欢迎的副作用,即 UDP 请求可能会被重定向到其他进程之一,而不是服务器.

One way around this is to use the SO_REUSEADDR socket option in the server and explicitely bind to the correct port when sending the reply. But that would also have the undesireable side-effect that UDP requests might be redirected to one of the other processes instead of the server.

另一个选项(如果您使用 Unix/Linux)是通过 unix 域套接字(通过 sendmsg 和辅助数据)将服务器套接字传递给其他进程.

Another option (if you are using Unix/Linux) is to pass the server socket to the other processes via a unix domain socket (via sendmsg and ancillary data).

这篇关于从不同的进程通过套接字 (UDP) 回复客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从不同的进程通过套接字 (UDP) 回复客户端

基础教程推荐