-
November 27th, 2020, 05:21 AM
#1
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|