简体   繁体   English

C:UDP套接字编程

[英]C : UDP socket programing

I want to pass message between two devices, for an example devices are PCs. 我想在两个设备之间传递消息,例如一个设备是PC。 Currently I am using UDPServer.c and UDPClient.c for reference. 目前我使用UDPServer.cUDPClient.c以供参考。 By using this reference, I can pass messages, now I want to implement both side listen and send, also I want to create an API for sending message, that API can be used by other functions. 通过使用此引用,我可以传递消息,现在我想同时实现侧面监听和发送,还想创建一个用于发送消息的API,该API可以被其他功能使用。 May I need to use two different port for sending message and receiving message?? 我可以使用两个不同的端口来发送消息和接收消息吗? what is the best way to set up UDP socket programming for message passing?? 为消息传递设置UDP套接字编程的最佳方法是什么?

From your description, it doesn't look like you need any more than what sendto() / recvfrom() already do. 从您的描述来看,您似乎不需要的东西已经超过sendto() / recvfrom()需要的。 You might as well treat them as your "API" for message passing. 您不妨将它们视为传递消息的“ API”。 Once you set up/open the socket, just send/recv as needed. 设置/打开套接字后,只需根据需要发送/接收。 You don't need to worry about different ports for sending/receiving; 您无需担心用于发送/接收的不同端口。 your example is fine. 你的例子很好。 FYI, you can sendto/recvfrom on the same socket. 仅供参考,您可以在同一套接字上发送/接收。

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

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