Search code examples
csocketscygwin

Beej's server client socket examples don't work in cygwin


I'm trying to build a basic client server, so I can modify it and then do analysis in IDA. I was pointed to Beej's socket programming examples as the definitive guide:

http://beej.us/guide/bgnet/output/html/multipage/clientserver.html

I typically use Cygwin as my working environment for programming and such. When I compile these programs, the server program does not work under Cygwin, although the client program does.

When the exact same server code is compiled and run under linux, there is no issue, it runs fine.

I had someone else try it the same way, and they get the same results. When attempting to connect to the server, the client closes and displays:

client: connect: connection timed out
client: failed to connect

and a packet capture shows that TCP SYN packets are sent to the server, but it never responds.

Can anyone explain to me the reason why this fails under Cygwin, and propose a solution?


Solution

  • I found out that the program as given by Beej was defaulting to IPv6 on the server. The solution was to either address the server address in the client using it's IPv6 address, or to change the server to use IPv4 instead. This can be accomplished by changing the server code from

    hints.ai_family = AF_UNSPEC;
    

    to

    hints.ai_family = AF_INET;
    

    Hope this helps someone.