admin管理员组文章数量:1389762
I have FPGA device which is waiting for UDP packet "FFFF" on port 1000, then answers also with UDP "FFFF". This code works fine, I see incoming "FFFF" packets on my JTAG, but only when Wireshark is launched. If Wireshark is off all I see on JTAG is arp packets, to which I respond, and then - nothing. My C project is on Codeblocks, i had writed "-lwsock32" on other linker options of linker settings.
What am I doing wrong, I want my code work without Wireshark...I guess Wirshark launches some server and everything starts working
MAC of my device is 34:34:45:79:34:54, IP is 192.168.150.111 (can be found in code) IP of my PC is in same network - 192.168.150.5
#include <stdio.h>
#include <stdbool.h>
#include <windows.h>
#include <conio.h>
int main(int argc, char* argv[])
{
SOCKET sckt;
struct sockaddr_in sckt_addr;
DWORD timeout;
WSADATA WSAData;
int rc = WSAStartup (0x202, &WSAData);
if(rc != 0)
{
printf("Ошибка инициализация версии Windows Sockets");
return -1;
}
sckt = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(sckt == INVALID_SOCKET)
{
printf("Ошибка инициализация версии Windows Sockets");
return -1;
}
sckt_addr.sin_family = AF_INET;
sckt_addr.sin_addr.s_addr = INADDR_ANY;
sckt_addr.sin_port = htons(1000);
if(bind (sckt , (LPSOCKADDR) &sckt_addr, sizeof(sckt_addr)) == SOCKET_ERROR)
{
printf("Error while initializing Windows Sockets");
closesocket (sckt);
return -1;
}
sckt_addr.sin_addr.s_addr = inet_addr("192.168.150.111");
char o_data[2];
o_data[0] = o_data[1] = 0xFF;
const int i_data_len = 7;
char i_data [7];
int sckt_addr_size = 32;
while(true)
{
rc = sendto (sckt, o_data, 2, 0, (SOCKADDR *)&sckt_addr, sizeof(sckt_addr));
if (rc != 2)
{
printf ("Socket error");
return 1;
}
rc = recvfrom (sckt, i_data, i_data_len, 0, (SOCKADDR *) &sckt_addr, &sckt_addr_size);
if (WSAGetLastError() == WSAETIMEDOUT)
{
printf ("No response from FPGA");
continue;
}
else
{
if (((unsigned char)i_data[0] << 8) + (unsigned char)i_data[1] == 0xFFFF)
printf("\n\nDevice answered FFFF\n");
else
{
printf("\n\nReceived wrong response. ");
return 2;
}
}
}
closesocket (sckt);
WSACleanup ();
return 0;
}
The moment I run wireshark I see that my FPGA answers with ARP-packet and UDP packets going fine:
I have FPGA device which is waiting for UDP packet "FFFF" on port 1000, then answers also with UDP "FFFF". This code works fine, I see incoming "FFFF" packets on my JTAG, but only when Wireshark is launched. If Wireshark is off all I see on JTAG is arp packets, to which I respond, and then - nothing. My C project is on Codeblocks, i had writed "-lwsock32" on other linker options of linker settings.
What am I doing wrong, I want my code work without Wireshark...I guess Wirshark launches some server and everything starts working
MAC of my device is 34:34:45:79:34:54, IP is 192.168.150.111 (can be found in code) IP of my PC is in same network - 192.168.150.5
#include <stdio.h>
#include <stdbool.h>
#include <windows.h>
#include <conio.h>
int main(int argc, char* argv[])
{
SOCKET sckt;
struct sockaddr_in sckt_addr;
DWORD timeout;
WSADATA WSAData;
int rc = WSAStartup (0x202, &WSAData);
if(rc != 0)
{
printf("Ошибка инициализация версии Windows Sockets");
return -1;
}
sckt = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(sckt == INVALID_SOCKET)
{
printf("Ошибка инициализация версии Windows Sockets");
return -1;
}
sckt_addr.sin_family = AF_INET;
sckt_addr.sin_addr.s_addr = INADDR_ANY;
sckt_addr.sin_port = htons(1000);
if(bind (sckt , (LPSOCKADDR) &sckt_addr, sizeof(sckt_addr)) == SOCKET_ERROR)
{
printf("Error while initializing Windows Sockets");
closesocket (sckt);
return -1;
}
sckt_addr.sin_addr.s_addr = inet_addr("192.168.150.111");
char o_data[2];
o_data[0] = o_data[1] = 0xFF;
const int i_data_len = 7;
char i_data [7];
int sckt_addr_size = 32;
while(true)
{
rc = sendto (sckt, o_data, 2, 0, (SOCKADDR *)&sckt_addr, sizeof(sckt_addr));
if (rc != 2)
{
printf ("Socket error");
return 1;
}
rc = recvfrom (sckt, i_data, i_data_len, 0, (SOCKADDR *) &sckt_addr, &sckt_addr_size);
if (WSAGetLastError() == WSAETIMEDOUT)
{
printf ("No response from FPGA");
continue;
}
else
{
if (((unsigned char)i_data[0] << 8) + (unsigned char)i_data[1] == 0xFFFF)
printf("\n\nDevice answered FFFF\n");
else
{
printf("\n\nReceived wrong response. ");
return 2;
}
}
}
closesocket (sckt);
WSACleanup ();
return 0;
}
The moment I run wireshark I see that my FPGA answers with ARP-packet and UDP packets going fine:
Share Improve this question edited Mar 12 at 17:59 MrLeyt1125 asked Mar 12 at 16:43 MrLeyt1125MrLeyt1125 154 bronze badges 3 |1 Answer
Reset to default 0Finally I got it!
My ARP responce was too short - 42 bytes (ARP-request from C++ on wireshark shown as only 42 bytes). I had to add 18 zeroes to my ARP responce to make it 64 bytes (min length).
Apparently from C++ to network card something is extending ARP-request, but this happens after Wireshark, and it doesn't show correct length of 64 bytes
Probably my UDP packets too short too.
But running Wireshark ignores these errors and everything works. It doesnt even mark it as problem!
本文标签: cWinsocket UDPworks only when wireshark launchedStack Overflow
版权声明:本文标题:c - Winsocket UDP : works only when wireshark launched - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744739551a2622528.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
sizeof(sockaddr_in)
is 16 not 32, so you are lying torecvfrom()
about the size of yoursckt_addr
. Not that it really matters, but you should do the right thing and useint sckt_addr_size = sizeof(sckt_addr);
. More importantly, your error handling ofrecvfrom()
is wrong and should be checkingrc
for failure before then checkingWSAGetLastError()
. – Remy Lebeau Commented Mar 12 at 21:14