CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Strange problem with Windows Sockets

  1. #1
    Join Date
    Nov 2020
    Posts
    2

    Strange problem with Windows Sockets

    Hello,

    I have written a simple TCP-IP client to communicate with a microprocessor board with Ethernet
    Regardless of what the board does, to open communication it's only necessary to instantiate a socket, write ASCII code 0x15 and wait for the board to reply with ASCII code 0x06
    The board comes with a Windows application which the same, then allows to send/receive commands to/from the board

    Now, the question is:

    if I set the board IP address to a class C one (that is 192.168.100.201) it all works OK either with the board and with my program

    However, if I set the IP to a private class A one (e.g. 010.130.001.066) and adjust my NIC settings consequently:
    - I'm still able to PING the board
    - I'm still able to communicate with the original app
    - I'm not able to communicate with my program any more, 'connect' API call returns with timeout error

    I have tried to trace the communication with Wireshark. When trying with my program, it shows nothing with the class A ip, imho it means that 'connect' doesn't even try to make a connection

    I have also tried with 'Hercules setup utility' (freeware). It has exactly the same behaviour as my program, works only with class C IP

    I don't think the cause is related to the uC board, I have the same issue even with the board disconnected -- with class C IP, the program tries to connect, with class A it doesn't

    Any clues?

    I'm enclosing the source code of my client, however it's nothing different from the classic TCP server you can find on any winsock tutorial

    Thank you

    ---------------------------------------------------------------

    Code:
    #define PORT 6000
    
    //#define IP_TEX "192.168.100.201"  // OK
    
    #define IP_T "010.130.001.066"    // NOT WORKING
    
    #define BUFLEN 512
    
    void f1()
    {
        SOCKET SendingSocket;
    
        SendingSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if(SendingSocket == INVALID_SOCKET)
        {
             printf("Errore : socket() - %ld\n", WSAGetLastError());
    
             return;
        }
    
        SOCKADDR_IN ServerAddr;
        ServerAddr.sin_family = AF_INET;
        ServerAddr.sin_port = htons(PORT);
        ServerAddr.sin_addr.s_addr = inet_addr(IP_T);
    
        if (connect(SendingSocket, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr)))
        {
            printf("Errore: connect() - %ld\n", WSAGetLastError()); // connect hangs here with class A IP, PING still working
            closesocket(SendingSocket);
            return;
        }
    
        char buf[BUFLEN];
        buf[0] = 21;
        buf[1]=0;
    
        int BytesSent = send(SendingSocket, buf, strlen(buf), 0);
        if (!BytesSent)
        {
            printf("Errore: send()\n");
            return;
        }
    
        printf("%d bytes trasmessi\n", BytesSent);
        int rcLen = recv(SendingSocket, buf, BUFLEN, 0);
        printf("%d bytes ricevuti\n", rcLen);
        if (rcLen==1 && buf[0]==0x06) printf("RISPOSTA OK!\n");
    }
    
    int main(...)
    {
        WSADATA wsaData;
        WSAStartup(MAKEWORD(2,2), &wsaData);
        printf("Winsock DLL OK : %s.\n", wsaData.szSystemStatus);
    
        f1();
        if(WSACleanup() != 0) printf("Errore: WSACleanup()\n");
    
        return 0;
    }
    Last edited by VictorN; November 27th, 2020 at 07:58 AM. Reason: adding CODE tags

  2. #2
    Join Date
    Nov 2020
    Posts
    2

    Re: Strange problem with Windows Sockets

    I have tried both on Win7 and Win10 : the same
    Of course I don't have firewall (if I had, it would have blocked the original app too, not only mine)
    My compiler is MinGW but I think any Windows compiler should build this app
    I have also tried with Qt Creator and component QTcpSocket. Same problem with this, too.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,886

    Re: Strange problem with Windows Sockets

    Interesting! I tried to ping:
    Code:
    C:\Users\Victor>ping 010.130.001.066
    
    Pinging 8.130.1.54 with 32 bytes of data:
    Request timed out.
    Request timed out.
    Request timed out.
    Request timed out.
    
    Ping statistics for 8.130.1.54:
        Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
    
    C:\Users\Victor>ping 10.130.001.066
    
    Pinging 10.130.1.54 with 32 bytes of data:
    Request timed out.
    Request timed out.
    Request timed out.
    Request timed out.
    
    Ping statistics for 10.130.1.54:
        Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
    
    C:\Users\Victor>
    Last edited by VictorN; November 27th, 2020 at 01:09 PM.
    Victor Nijegorodov

  4. #4
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,148

    Re: Strange problem with Windows Sockets

    Not sure what inet_addr makes with leading zeros, but maybe that is a problem? As Victor's example shows, ping takes leading zeros as indication of a number in octal format.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,886

    Re: Strange problem with Windows Sockets

    Quote Originally Posted by Richard.J View Post
    Not sure what inet_addr makes with leading zeros, but maybe that is a problem? As Victor's example shows, ping takes leading zeros as indication of a number in octal format.
    Exactly!
    From https://docs.microsoft.com/en-us/win...rnet-addresses :
    The parts that make up an address in "." notation can be decimal, octal or hexadecimal as specified in the C language. Numbers that start with "0x" or "0X" imply hexadecimal. Numbers that start with "0" imply octal. All other numbers are interpreted as decimal.
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)