简体   繁体   English

如何在TCP / IP套接字编程中对IP地址写入响应

[英]How to write response on IP address in TCP/IP socket programming

I have written a simple server to accept TCP connections and to read data from clients. 我编写了一个简单的服务器来接受TCP连接并从客户端读取数据。

My question is: I have multiple clients with different IP addresses sending data on the same port. 我的问题是:我有多个具有不同IP地址的客户端在同一端口上发送数据。 How can I write data to only one specific client by using its IP address? 如何使用其IP地址仅将数据写入一个特定的客户端?

I am assuming you have the file descriptors for the connected TCP sockets. 我假设您具有连接的TCP套接字的文件描述符。 It is easy to retrieve the addresses of the remote hosts. 检索远程主机的地址很容易。

The key is the getpeername() system call, which allows you to find out the address of the remote end of a socket. 关键是getpeername()系统调用,它使您可以找到套接字的远程端的地址。

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

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