在回答客户端在从一个不同的进程套接字(UDP) [英] Replying to a client over sockets (UDP) from a different process

查看:100
本文介绍了在回答客户端在从一个不同的进程套接字(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地址和客户端的其他进程的端口号,这个过程创建一个新的socket,并做了SENDTO ..​​.我已经通过code追查,一切看起来不错,但客户端仍挡在了接收......我知道,如果我从命令处理程序做的sendto,它得到响应,而不是从一个新的socket。

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.

下面是一些例子code:

Here's some example code:

#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));

,请帮助!

推荐答案

您的客户端(或之间有防火墙)可能会感到困惑,通过接收来自不同的源端口的响应比它发出的请求(如操作系统将随便挑了一个新的socket随机的源端口)。

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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆