简体   繁体   English

服务器可以从其他进程和套接字发送响应吗?

[英]Can server send response from a different process and socket?

After server, which is listening on port X, receives a request from the client, can it send response from a different process and a socket with sendto call. 在正在端口X上侦听的服务器接收到来自客户端的请求之后,它是否可以从其他进程和带有sendto调用的套接字发送响应。 Please note that the client also sends request with sendto (server address) call. 请注意,客户端还会通过sendto(服务器地址)呼叫发送请求。

Here are the steps: 步骤如下:

  1. server opens a socket and binds it to IP address (abcd) and port (x) 服务器打开一个套接字并将其绑定到IP地址(abcd)和端口(x)
  2. client opens a socket and binds it to IP address (klmn) and port (z) and sends request to server with sendto call. 客户端打开一个套接字,并将其绑定到IP地址(klmn)和端口(z),然后使用sendto调用将请求发送到服务器。
  3. Server (in different process) opens a socket and binds it to IP address (abcd) and port (y) 服务器(以不同的过程)打开一个套接字并将其绑定到IP地址(abcd)和端口(y)
  4. server sends response from above socket to client with sendto 服务器使用sendto从上面的套接字向客户端发送响应

Both client and server knows each other ip addresses and port numbers. 客户端和服务器都知道彼此的IP地址和端口号。

I am noticing that client does not receive response when server sends response from a different process. 我注意到服务器从其他进程发送响应时,客户端未收到响应。 Otherwise, it does. 否则,它会。

This can be done but there are some major caveats. 可以这样做,但有一些主要警告。

  1. The client must bind the socket and use sendto() and recvfrom() rather than connect() 'ing the socket and using send() and recv() . 客户端必须绑定套接字并使用sendto()recvfrom()而不是connect()插入套接字并使用send()recv() This allows it to receive incoming packets from anywhere as long as they are directed to the IP/Port that recvfrom() is reading on. 这使它可以从任何地方接收传入的数据包,只要它们被定向到recvfrom()正在读取的IP /端口即可。

  2. If there is a firewall or NAT in the path between client and server, it is likely to block the response since the sending IP/Port does not match the IP/Port that the client originally sent its request to. 如果客户端和服务器之间的路径中存在防火墙或NAT,则可能会阻止响应,因为发送IP /端口与客户端最初向其发送请求的IP /端口不匹配。

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

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