Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Winsock2 help please!

Winsock2 help please!

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LizardWiz
    wrote on last edited by
    #1

    I wrote a winsock service that spawns a new thread on the "accept" function. Sometimes it seems that when I "recv" the client doesn't seem to have closed the connection gracefully? The service continues to peg the CPU at 100% (even though I have checked for SOCKET_ERROR and byte size of 0 returned from "recv"). My question is.....(code is "pseudo-pseudocode") Can I rely on the following to work properly considering the problems that I have found with closing the socket on the client end? int nTimeout = 1000; nError = setsockopt(client, SOL_SOCKET, SO_RCVTIMEO, (char*)&nTimeout, sizeof(nTimeout)); Or, is something like this going to be more reliable (albeit pretty ugly...), where the "accept" calls a timer thread which in turn calls the client thread until it times out(as follows)? UINT ClientTimerThread(LPVOID pParam) { CWinThread* thread; LPDWORD exitvalue; if (WaitForSingleObjectEx(thread = AfxBeginThread(ClientThread,pParam), 10000, false) == WAIT_TIMEOUT) { socket s = (SOCKET)pParam; closesocket(s); GetExitCodeThread(thread, exitvalue); TerminateThread(thread, (DWORD) exitvalue); } return 0; } Any advice would be greatly appreciated... ~LizardWiz()

    J 1 Reply Last reply
    0
    • L LizardWiz

      I wrote a winsock service that spawns a new thread on the "accept" function. Sometimes it seems that when I "recv" the client doesn't seem to have closed the connection gracefully? The service continues to peg the CPU at 100% (even though I have checked for SOCKET_ERROR and byte size of 0 returned from "recv"). My question is.....(code is "pseudo-pseudocode") Can I rely on the following to work properly considering the problems that I have found with closing the socket on the client end? int nTimeout = 1000; nError = setsockopt(client, SOL_SOCKET, SO_RCVTIMEO, (char*)&nTimeout, sizeof(nTimeout)); Or, is something like this going to be more reliable (albeit pretty ugly...), where the "accept" calls a timer thread which in turn calls the client thread until it times out(as follows)? UINT ClientTimerThread(LPVOID pParam) { CWinThread* thread; LPDWORD exitvalue; if (WaitForSingleObjectEx(thread = AfxBeginThread(ClientThread,pParam), 10000, false) == WAIT_TIMEOUT) { socket s = (SOCKET)pParam; closesocket(s); GetExitCodeThread(thread, exitvalue); TerminateThread(thread, (DWORD) exitvalue); } return 0; } Any advice would be greatly appreciated... ~LizardWiz()

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      Please do not even consider applying your second solution: TerminateThread is not to be used unless under catastrophic conditions. In Winsock2 SO_RCVTIMEO works fine (you sure is Winsock2 and not 1.1 what you're using?) Nevertheless, even if it didn't work that wouldn't justify the CPU peak. Can you post more of the reading code? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups