C++ 如何设置 Winsock UDP 套接字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/679145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set up a Winsock UDP socket?
提问by axs6791
I want to create a Winsock UDP socket that only sends data to a client. I want the kernel to choose an available port for me. On the other hand, I want to indicate which local IP to use, since I'm running a few nics.
我想创建一个只向客户端发送数据的 Winsock UDP 套接字。我希望内核为我选择一个可用的端口。另一方面,我想指出要使用哪个本地 IP,因为我正在运行一些网卡。
I've tried combing through the maze of socket options, as well as binding with the port in the socket address set to 0 to no avail.
我已经尝试通过套接字选项的迷宫进行梳理,以及将套接字地址中的端口绑定设置为 0 无济于事。
My code is in Win32 C++.
我的代码在 Win32 C++ 中。
回答by Graeme Perrow
Please excuse the lack of error checking:
请原谅缺乏错误检查:
char pkt[...];
size_t pkt_length = ...;
sockaddr_in dest;
sockaddr_in local;
WSAData data;
WSAStartup( MAKEWORD( 2, 2 ), &data );
local.sin_family = AF_INET;
local.sin_addr.s_addr = inet_addr( <source IP address> );
local.sin_port = 0; // choose any
dest.sin_family = AF_INET;
dest.sin_addr.s_addr = inet_addr( <destination IP address> );
dest.sin_port = htons( <destination port number> );
// create the socket
SOCKET s = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
// bind to the local address
bind( s, (sockaddr *)&local, sizeof(local) );
// send the pkt
int ret = sendto( s, pkt, pkt_length, 0, (sockaddr *)&dest, sizeof(dest) );
回答by Chuque
The answer of Graeme Perrow doesn't work anymore because inet_addr is deprecated. Use inet_pton instead like this:
Graeme Perrow 的答案不再起作用,因为 inet_addr 已被弃用。像这样使用 inet_pton 代替:
#include <string>
#include <WinSock2.h>
#include <Ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main() {
const char* pkt = "Message to be sent";
const char* srcIP = < source IP address >;
const char* destIP = < destination IP address >;
sockaddr_in dest;
sockaddr_in local;
WSAData data;
WSAStartup(MAKEWORD(2, 2), &data);
local.sin_family = AF_INET;
inet_pton(AF_INET, srcIP, &local.sin_addr.s_addr);
local.sin_port = htons(0);
dest.sin_family = AF_INET;
inet_pton(AF_INET, destIP, &dest.sin_addr.s_addr);
dest.sin_port = htons(< destination port number >);
SOCKET s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
bind(s, (sockaddr *)&local, sizeof(local));
sendto(s, pkt, strlen(pkt), 0, (sockaddr *)&dest, sizeof(dest));
closesocket(s);
WSACleanup();
return 0;
}
回答by Bruce Ikin
Not a direct "HowTo", but I have been using an open source library called "ACE (Adaptive Communication Environment" for all my TCP and UDP socket programming and found it very useful and powerful. It takes a "software pattens" approach to providing building blocks to solve your particular problem. I was able to use their UDP encapsulation to connect to a given port and have replies sent to a free port chosen by the system. Alternatively you can specify the return port if you wish. ACE is available here: ACE Homepage
不是直接的“HowTo”,但我一直在为我所有的 TCP 和 UDP 套接字编程使用一个名为“ACE(自适应通信环境”)的开源库,发现它非常有用和强大。它采用“软件模式”方法来提供构建块来解决您的特定问题。我能够使用他们的 UDP 封装连接到给定的端口,并将回复发送到系统选择的空闲端口。或者,您可以根据需要指定返回端口。ACE 可在此处获得: ACE主页
回答by Andrew Edgecombe
When you say "I want to indicate which local IP to use, since I'm running a few nics", do you mean that you want to specify the ip address, or do you want to specify the nic and use the associated ip address?
当你说“我要指明使用哪个本地IP,因为我运行了几个网卡”,你的意思是要指定IP地址,还是要指定网卡并使用关联的IP地址?
If you are trying to specify the nic, this questionshould be relevant.
如果您试图指定网卡,这个问题应该是相关的。