我正在尝试侦听特定端口(6053),并且我想打印通过该端口的所有数据。但我似乎无法让我的程序正确运行,它输出错误数据/错误数据,因为它与来自线鲨的数据不匹配。如果有人对我应该前进的方向有任何意见或建议,将不胜感激!
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <WinSock2.h>
#include <winsock.h>
#include <string.h>
#include <sys/types.h>
#include <assert.h>
int main(void) {
sockaddr_in si_me, si_other;
int s;
assert((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) );//!= -1
int port = 6053;
int broadcast = 1;
setsockopt(s, SOL_SOCKET, SO_BROADCAST,
(const char * )&broadcast, sizeof broadcast);
memset(&si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(port);
si_me.sin_addr.s_addr = INADDR_ANY;
assert(::bind(s, (sockaddr *)&si_me, sizeof(sockaddr)) );//!= -1
while (1)
{
char buf[10000];
int slen = sizeof(sockaddr);
recvfrom(s, buf, sizeof(buf) - 1, 0, (sockaddr *)&si_other, &slen);
printf("recv: %x\n", buf);
}
}