简体   繁体   English

我是否需要在客户端使用其他套接字进行监听和发送?

[英]Do I need a different socket on client side for listening and sending?

I am new to socket programming. 我是套接字编程的新手。 The scenario is that: 该方案是:

1- I am creating a UDP socket, fd 1-我正在创建UDP套接字fd

2- Binding fd to an IP address/port 2-将fd绑定到IP地址/端口

3- recvfrom ()ing on the socket fd from destination, D 3-从目标D插入套接字fd上的() recvfrom

Now if I want to sendto() some data back to D, can I use the same socket, by passing in the sockaddr_in containing the IP address and port of D? 现在,如果我想将一些数据发送回(to sendto()发送给D,是否可以通过传递包含D的IP地址和端口的sockaddr_in来使用同一套接字? Or do I need to create a new socket and do bind() ? 还是我需要创建一个新的套接字并执行bind()

Edit: From the first answer, it seems I do not need another socket, and do not need to bind() on that socket before transmitting. 编辑:从第一个答案,似乎我不需要另一个套接字,并且不需要在传输之前在该套接字上的bind()。 Is that correct? 那是对的吗?

Yes, you can use the same UDP socket for both receiving and sending. 是的,您可以使用相同的UDP套接字进行接收和发送。 To send directly to the peer you just received from, use the sockaddr_in structure that was filled in by recvfrom . 要直接发送给刚从其接收的对等方,请使用recvfrom填充的sockaddr_in结构。

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

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