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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Setting timeout on socket connect

Setting timeout on socket connect

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
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.
  • R Offline
    R Offline
    Rein Hillmann
    wrote on last edited by
    #1

    Does anyone know how I can set a timeout value for the connect() method of a socket? If it can't connect within about 200ms then I know that it never will. At the moment the connect is waiting for about 5-10 seconds before it returns. My code below: target.sin_family = AF_INET; target.sin_addr.s_addr = *(long *)(host->h_addr); target.sin_port = htons(port); s=socket(AF_INET,SOCK_STREAM,0); if (s<0) exit(printf("Error creating socket.\n")); if (!connect(s,(struct sockaddr *)&target, sizeof(target))) { // ....... do my thing here .... } if (closesocket(s)) exit(printf("error closing socket.\n"));

    J 1 Reply Last reply
    0
    • R Rein Hillmann

      Does anyone know how I can set a timeout value for the connect() method of a socket? If it can't connect within about 200ms then I know that it never will. At the moment the connect is waiting for about 5-10 seconds before it returns. My code below: target.sin_family = AF_INET; target.sin_addr.s_addr = *(long *)(host->h_addr); target.sin_port = htons(port); s=socket(AF_INET,SOCK_STREAM,0); if (s<0) exit(printf("Error creating socket.\n")); if (!connect(s,(struct sockaddr *)&target, sizeof(target))) { // ....... do my thing here .... } if (closesocket(s)) exit(printf("error closing socket.\n"));

      J Offline
      J Offline
      JohnnyG
      wrote on last edited by
      #2

      I think you can use Winsock's "select" statement. Take a look at that along with the TIMEVAL structure. Oh yeah, that only works on Winsock 2.0

      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