[英]Receiving UDP packets using winsock
我不斷從端口獲取UDP數據包。 以下是wireshark的日志。 如何使用Winsock編程連續接收那些數據包。 我嘗試過,但是無法接收。 在recvfrom()調用之后,它沒有寫入緩沖區。請給我一個想法,如何接收緩沖區中的每個數據包並將每個數據包寫入文本文件。 請幫我。 提前致謝...
源IP是192.168.13.25,端口號是2780(源是將連續發送UDP數據包的硬件)目的地IP是192.168.13.250,端口號是45141(目的地是我的PC)在我的代碼中,我綁定到192.168。 13.250(PC)和端口2780(硬件)。 然后我打電話給recvfrom()。 Ip和端口是否有任何不匹配? 所以我不需要從用戶那里獲得哪個IP和端口的bind()和recvfrom()嗎?
No Time Source Destination Proto Info 1 0.000000 192.168.13.25 192.168.13.250 UDP Source port: lbc-control Destination port: 45141 2 0.000416 192.168.13.25 192.168.13.250 UDP Source port: lbc-control Destination port: 45141 3 0.000846 192.168.13.25 192.168.13.250 UDP Source port: lbc-control Destination port: 45141 4 0.001281 192.168.13.25 192.168.13.250 UDP Source port: lbc-control Destination port: 45141 5 0.001716 192.168.13.25 192.168.13.250 UDP Source port: lbc-control Destination port: 45141 6 0.002152 192.168.13.25 192.168.13.250 UDP Source port: lbc-control Destination port: 45141 7 0.002589 192.168.13.25 192.168.13.250 UDP Source port: lbc-control Destination port: 45141 8 0.003025 192.168.13.25 192.168.13.250 UDP Source port: lbc-control Destination port: 45141
以下是我的代碼:
int main(void) {
SOCKET recvSockID;
WSADATA wsaData = {0};
FILE *udp;
FILE *fp ;
struct sockaddr_in sock_addr;
struct sockaddr_in cliAddr;
static int recvData;
int iResult = 0;
int sock_len = sizeof(sock_addr);
int sockCli_len = sizeof(cliAddr);
int recvResult;
static int iteration;
fp = fopen("outOfSeq.txt","a");
if((udp = fopen("udpData.txt","w")) == 0)
printf("udpData.txt not opened\n");
printf("\n Enter Destination IP Address : ");
scanf_s("%s",inputData.destIPAddr,16);
printf("\n Enter Destination port from which to receive data : ");
scanf_s("%d",&inputData.portNo,5);
printf("\n Enter No.of iterations : ");
scanf_s("%d",&inputData.noIteration,2);
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if(iResult < 0) {
printf("windows socket startup error\n");
}
recvSockID = socket(AF_INET, SOCK_DGRAM, 0);
if(recvSockID < 0) {
printf("Socket creation error\n");
WSACleanup();
}
sock_addr.sin_family = AF_INET;
sock_addr.sin_port = htons(inputData.portNo);
sock_addr.sin_addr.s_addr = inet_addr(inputData.destIPAddr);
//sock_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(recvSockID, (struct sockaddr *)&sock_addr,
sizeof(struct sockaddr)) < 0)
{
printf("bind() failed: %ld.\n", WSAGetLastError());
closesocket(recvSockID);
return 0;
}
memset(udpBuf, 0, sizeof(udpBuf));
iteration = inputData.noIteration;
recvData = 1;
while (recvData) {
printf("receiving data\n");
recvResult = recvfrom(recvSockID, udpBuf, sizeof(udpBuf),
0, (struct sockaddr *)&cliAddr, &sockCli_len);
if (recvResult <= 0) {
printf("recvResult = %d\n", recvResult);
printf("Error Code: %d",WSAGetLastError());
printf("Socket receive()- error\n");
return 0;
//break;
//goto exit;
} else
printf("Socket receive()- success\n");
printf("completed rx data\n");
fwrite(udpBuf, sizeof(udpBuf), 1, udp);
memset(udpBuf, 0, sizeof(udpBuf));
if (iteration != 0) {
iteration--;
if (iteration <= 0)
recvData = 0;
}
}
//exit:
if(udp) {
fclose(udp);
udp = 0;
}
//shutdown socket
closesocket(recvSockID);
fclose(udp);
return 0;
}
fwrite(udpBuf, sizeof(udpBuf), 1, udp)
您應該傳遞recvResult
而不是sizeof(udpBuf)
。
由於文件緩沖,您可能不會立即在文件上看到字節。 但是, close
您應該會看到數據。
如果要禁用緩沖,請使用setvbuf(udp, NULL, _IONBF, 0)
,或者在每次寫入操作后調用fflush(udp)
。
更新:
如果您根本沒有收到數據報,則可能是您綁定了錯誤的地址或端口。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.