最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c - Winsocket UDP : works only when wireshark launched - Stack Overflow

programmeradmin1浏览0评论

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
  • Wireshark puts the NIC in promiscuous mode. Are you sure the packets are being sent to your MAC address? – Barmar Commented Mar 12 at 17:34
  • @Barmar I added picture: can see that when I launch Wireshark my PC sends arp-request and my FPGA answers with its MAC, then UDP packets start going just fine – MrLeyt1125 Commented Mar 12 at 18:04
  • I don't see anything wrong with your sending code. However, when reading, sizeof(sockaddr_in) is 16 not 32, so you are lying to recvfrom() about the size of your sckt_addr. Not that it really matters, but you should do the right thing and use int sckt_addr_size = sizeof(sckt_addr);. More importantly, your error handling of recvfrom() is wrong and should be checking rc for failure before then checking WSAGetLastError(). – Remy Lebeau Commented Mar 12 at 21:14
Add a comment  | 

1 Answer 1

Reset to default 0

Finally 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!

发布评论

评论列表(0)

  1. 暂无评论