ldom.net.gif
  • Home
  • Contact
  • REPETS
  • Code

Winsock problem: connect() works on the client side but accept() blocks on the server side

yo | 29 01 2008

While writing a proof-of-concept Winsock program, I run into an issue that is worth sharing, especially with Winsock beginners. I hadn’t done socket programming for a while, so forgive my stupidity.

Basically, I had the server code to do this :

  1. while (true)
  2. {
  3.     //…
  4.     serverSocket = socket(…);
  5.     setsockopt(serverSocket, SO_REUSEADDR, …);
  6.     bind(serverSocket, …);
  7.     listen(serverSocket, portNumber);
  8.     clientSocket = accept(…);
  9.     // do something with the client
  10. }

This was working for the first 2 calls of the client, but at the 3rd call, the client’s connect() would work and the server would be stuck on the accept().

Foolish me! After a little investigation, I realized you don’t put the listen() call inside the server loop…

Here’s the proper code:

  1. //…
  2. serverSocket = socket(…);
  3. setsockopt(serverSocket, SO_REUSEADDR, …);
  4. bind(serverSocket, …);
  5. listen(serverSocket, portNumber);
  6. while (true)
  7. {
  8.     clientSocket = accept(…);
  9.     // do something with the client
  10. }

Hope this helps…

Categories
Code
Comments rss
Comments rss
Trackback
Trackback

« Esclaffades 2007 Console shelves »

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

complétude

  • Everything2
  • ffffound
  • Halfbakery
  • Wikipedia
  • Wordcount

explorations

  • Bradype Ludique, sa vie, son oeuvre.
  • Digital Photography Review
  • DVXuser.com
  • Gizmodo
  • Journal d’un avocat
  • Kopikol
  • Kuro5hin
  • Lafraise
  • MacBidouille
  • Made in Tokyo
  • New urban legends
  • Seth Godin
  • Tech dirt
  • Tokyo Times
  • [daily dose of imagery]

del.icio.us

rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox