我需要连接到端口500并打印出来的数据。有人能指引我朝着正确的方向吗?也许一个样本程序..收听UDP端口
错过了一部分..使用升压ASIO ..
我用C/C++用下面的代码:
int z;
struct sockaddr_in portList;
int len_inet;
int s;
char dgram[512];
time_t td;
struct tm tm;
s = socket(AF_INET,SOCK_DGRAM,0);
if (s == -1) {
strerror(errno);
}
memset(&portList,0,sizeof portList);
portList.sin_family = AF_INET;
portList.sin_port = htons(500);
portList.sin_addr.s_addr = inet_addr("127.0.0.1");
if (portList.sin_addr.s_addr == INADDR_NONE) {
strerror(errno);
}
len_inet = sizeof portList;
z = bind(s, (struct sockaddr *)&portList, len_inet);
if (z == -1) {
strerror(errno);
}
for (;;) {
z = recv(s, dgram, sizeof dgram, 0);
if (z < 0) {
strerror(errno);
}
std::cout << dgram << std::endl;
}
但我得到的是垃圾值一样:
����҉
我做错了什么?
谷歌 “C++ UDP教程”? – NPE 2013-03-15 12:15:36
[你有什么尝试?](http://mattgemmell.com/2008/12/08/what-have-you-tried/)另外请注意,1024以下的端口是有特权的,可能需要你运行你的程序作为管理员或根。 – 2013-03-15 12:16:22
欢迎!请花几分钟阅读[faq](http://stackoverflow.com/faq)。如果你全部阅读,你会得到一个徽章(c: – 2013-03-15 12:17:00