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. socket question (listen, accept)

socket question (listen, accept)

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++sysadmin
5 Posts 3 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.
  • D Offline
    D Offline
    Daniel Strigl
    wrote on last edited by
    #1

    I have a MFC (server) application that works with sockets. It should do the following: 1. Waits for an incomming client connection request (only 1). 2. Accept that connection request. 3. Send something to the client. 4. Receive something from the client. 5. Close the accepted connection. 6. ... go to 1. (wait for incomming client connection request) Only 1 client can send something to the server. So, I tried to imeplement it like this:

    WSAStartup(...);

    server = socket(...);

    bind(server, ...);

    listen(server, ...);

    client = accept(server, ...);

    send(client, ...);

    recv(client, ...);

    closesocket(client);

    ...

    So... my question! This code runs in a own thread and should never ends (only when the user terminates the app). After ther closesocket(client); when I want to re-listen to some incomming connection request, have I to call only client = accept(server,...); or listen(server,...); client = accept(server,...);? What's the wright way:

    WSAStartup(...);

    server = socket(...);

    bind(server, ...);

    place while (TRUE) { here ???

    listen(server, ...);

    or place while (TRUE) { here ???

    client = accept(server, ...);

    send(client, ...);

    recv(client, ...);

    closesocket(client);

    } // while (TRUE) ...

    `Daniel ;) --------------------------- **Never change a running system!**`

    J O 2 Replies Last reply
    0
    • D Daniel Strigl

      I have a MFC (server) application that works with sockets. It should do the following: 1. Waits for an incomming client connection request (only 1). 2. Accept that connection request. 3. Send something to the client. 4. Receive something from the client. 5. Close the accepted connection. 6. ... go to 1. (wait for incomming client connection request) Only 1 client can send something to the server. So, I tried to imeplement it like this:

      WSAStartup(...);

      server = socket(...);

      bind(server, ...);

      listen(server, ...);

      client = accept(server, ...);

      send(client, ...);

      recv(client, ...);

      closesocket(client);

      ...

      So... my question! This code runs in a own thread and should never ends (only when the user terminates the app). After ther closesocket(client); when I want to re-listen to some incomming connection request, have I to call only client = accept(server,...); or listen(server,...); client = accept(server,...);? What's the wright way:

      WSAStartup(...);

      server = socket(...);

      bind(server, ...);

      place while (TRUE) { here ???

      listen(server, ...);

      or place while (TRUE) { here ???

      client = accept(server, ...);

      send(client, ...);

      recv(client, ...);

      closesocket(client);

      } // while (TRUE) ...

      `Daniel ;) --------------------------- **Never change a running system!**`

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

      The second place, between listen and accept. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      D 1 Reply Last reply
      0
      • J Joaquin M Lopez Munoz

        The second place, between listen and accept. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

        D Offline
        D Offline
        Daniel Strigl
        wrote on last edited by
        #3

        Thanks! Daniel ;) --------------------------- Never change a running system!

        1 Reply Last reply
        0
        • D Daniel Strigl

          I have a MFC (server) application that works with sockets. It should do the following: 1. Waits for an incomming client connection request (only 1). 2. Accept that connection request. 3. Send something to the client. 4. Receive something from the client. 5. Close the accepted connection. 6. ... go to 1. (wait for incomming client connection request) Only 1 client can send something to the server. So, I tried to imeplement it like this:

          WSAStartup(...);

          server = socket(...);

          bind(server, ...);

          listen(server, ...);

          client = accept(server, ...);

          send(client, ...);

          recv(client, ...);

          closesocket(client);

          ...

          So... my question! This code runs in a own thread and should never ends (only when the user terminates the app). After ther closesocket(client); when I want to re-listen to some incomming connection request, have I to call only client = accept(server,...); or listen(server,...); client = accept(server,...);? What's the wright way:

          WSAStartup(...);

          server = socket(...);

          bind(server, ...);

          place while (TRUE) { here ???

          listen(server, ...);

          or place while (TRUE) { here ???

          client = accept(server, ...);

          send(client, ...);

          recv(client, ...);

          closesocket(client);

          } // while (TRUE) ...

          `Daniel ;) --------------------------- **Never change a running system!**`

          O Offline
          O Offline
          Olli
          wrote on last edited by
          #4

          Hi Daniel, the usual way is the following: First, start with CREATE a socket object. Then Call BIND (if you use MFC, CAsyncSocket::Create() will do that for you) After this, set your ServerSocket LISTEN. (and here is your loop...) Then comes the connection handling, that means you have to implement each acceptet connection in a own thread, if you want to have a multi connection server. After accepting (and creating your connection thread) go back. Hope this helps a little.. Cheers, Olli!

          Olli Make it idiot proof and someone will make a better idiot......
          :suss: :rolleyes: :suss:

          D 1 Reply Last reply
          0
          • O Olli

            Hi Daniel, the usual way is the following: First, start with CREATE a socket object. Then Call BIND (if you use MFC, CAsyncSocket::Create() will do that for you) After this, set your ServerSocket LISTEN. (and here is your loop...) Then comes the connection handling, that means you have to implement each acceptet connection in a own thread, if you want to have a multi connection server. After accepting (and creating your connection thread) go back. Hope this helps a little.. Cheers, Olli!

            Olli Make it idiot proof and someone will make a better idiot......
            :suss: :rolleyes: :suss:

            D Offline
            D Offline
            Daniel Strigl
            wrote on last edited by
            #5

            Thanks, this will help! Daniel ;) --------------------------- Never change a running system!

            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