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. Other Discussions
  3. IT & Infrastructure
  4. Socket problem with listen()

Socket problem with listen()

Scheduled Pinned Locked Moved IT & Infrastructure
helpsysadminquestion
4 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.
  • J Offline
    J Offline
    jpyp
    wrote on last edited by
    #1

    I am having a problem that I cannot find a solution from the documentation or the web. I want to restrict my server to accept only one connection but my listen() statement does not behave the way I'm expecting. ... iServerSocket = socket(AF_INET, SOCK_STREAM, 0); if (iServerSocket != ERROR) { stServerAddr.sin_family = AF_INET; stServerAddr.sin_port = htons(TCP_PORT_CTRL); stServerAddr.sin_addr.s_addr = htonl(INADDR_ANY); iStatus = bind(iServerSocket, (struct sockaddr *) &stServerAddr, iSockAddrSize); if (iStatus == ERROR) { ... } else { iStatus = listen(iServerSocket, 1); if (iStatus == ERROR) { ... } else { if (taskSpawn("TCPServer", 90, 0, 20000, (FUNCPTR)TCPServer, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) == ERROR) { ... } } } } void TCPServer (void) { ... while (!bTerminating) { iClientSocket = accept(iServerSocket, (struct sockaddr *) &stClientAddr, &iClientSockAddrSize); ... (_starting client threads here_) } } What happens is that when I use 1 in the linten() call, I can connect 3 times before I get a "Connection refused" from the server. If I use 2, I can connect 4 times. If I use 3, I can connect 5 times. If I use 0, I can connect twice. It seems to be always 2 more connections than I want. Anybody have seen this before? jpyp

    L 1 Reply Last reply
    0
    • J jpyp

      I am having a problem that I cannot find a solution from the documentation or the web. I want to restrict my server to accept only one connection but my listen() statement does not behave the way I'm expecting. ... iServerSocket = socket(AF_INET, SOCK_STREAM, 0); if (iServerSocket != ERROR) { stServerAddr.sin_family = AF_INET; stServerAddr.sin_port = htons(TCP_PORT_CTRL); stServerAddr.sin_addr.s_addr = htonl(INADDR_ANY); iStatus = bind(iServerSocket, (struct sockaddr *) &stServerAddr, iSockAddrSize); if (iStatus == ERROR) { ... } else { iStatus = listen(iServerSocket, 1); if (iStatus == ERROR) { ... } else { if (taskSpawn("TCPServer", 90, 0, 20000, (FUNCPTR)TCPServer, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) == ERROR) { ... } } } } void TCPServer (void) { ... while (!bTerminating) { iClientSocket = accept(iServerSocket, (struct sockaddr *) &stClientAddr, &iClientSockAddrSize); ... (_starting client threads here_) } } What happens is that when I use 1 in the linten() call, I can connect 3 times before I get a "Connection refused" from the server. If I use 2, I can connect 4 times. If I use 3, I can connect 5 times. If I use 0, I can connect twice. It seems to be always 2 more connections than I want. Anybody have seen this before? jpyp

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      jpyp wrote:

      I want to restrict my server to accept only one connection

      Simple, process the connection in the same thread as the Socket.listen() using a blocking recv().

      led mike

      J 1 Reply Last reply
      0
      • L led mike

        jpyp wrote:

        I want to restrict my server to accept only one connection

        Simple, process the connection in the same thread as the Socket.listen() using a blocking recv().

        led mike

        J Offline
        J Offline
        jpyp
        wrote on last edited by
        #3

        I did what you recommended and I still have the same problem. My accept statement is no longer in a loop and the application is still accepting another connection. How can the app accept another connection if I am not going back to the accept statement? The client app is reporting that the server accepted the 2nd and 3rd connections with no problem and then refuses the 4th. Samething happens if I put a 2 instead of a 1 in the listen() call except that the server accepts 4 connections and refuses the 5th one.

        jpyp

        L 1 Reply Last reply
        0
        • J jpyp

          I did what you recommended and I still have the same problem. My accept statement is no longer in a loop and the application is still accepting another connection. How can the app accept another connection if I am not going back to the accept statement? The client app is reporting that the server accepted the 2nd and 3rd connections with no problem and then refuses the 4th. Samething happens if I put a 2 instead of a 1 in the listen() call except that the server accepts 4 connections and refuses the 5th one.

          jpyp

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          jpyp wrote:

          How can the app accept another connection if I am not going back to the accept statement?

          It can't. Do you have logging implemented? Do you do testing?

          jpyp wrote:

          Samething happens if I put a 2 instead of a 1 in the listen() call except that the server accepts 4 connections and refuses the 5th one.

          Your code implements the number of simultaneous connections the application allows. The "backlog" parameter to listen() only effects the stacks connection request queue size not your applications handling of accept().

          led mike

          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