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. multithreaded server, block on recv(), how do i send at any moment?

multithreaded server, block on recv(), how do i send at any moment?

Scheduled Pinned Locked Moved C / C++ / MFC
questionsysadminjsonhelplearning
6 Posts 4 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.
  • K Offline
    K Offline
    Kuniva
    wrote on last edited by
    #1

    I made a multithreaded tcp server which loops on a listening socket which accepts the connections on their own socket and spins them off in their own thread. In the thread, it just loops on the recv() call. But the recv() call doesnt return unless something is read. Now i need to be able to send data to any of the sockets or all sockets at any given time, i'm not very experienced with threads so i really didn't know if you could call send() while a recv() is waiting (which would be the case if one socket would receive something and i would call send() on all the sockets to broadcast it back) but i read somewhere that u couldn't read if it was writing and couldnt write if it was reading or something. So how do i handle this?? I've already tried to come up with a number of (be it somewhat rediculous) solutions like a main buffer in which all the receives are put and a counter for each socket keeps track of how many lines each socket needs to get out of the buffer to send, but then u get another problem because then the sockets will only send when they have received something.. :confused: Of course if it is safe to call recv() without knowing if somewhere else send() is being called then pls let me know, because i thought i read somewhere in the winsock API that it isnt but i looked again and couldnt find it anymore. Thanks Kuniva --------------------------------------------

    J N I 3 Replies Last reply
    0
    • K Kuniva

      I made a multithreaded tcp server which loops on a listening socket which accepts the connections on their own socket and spins them off in their own thread. In the thread, it just loops on the recv() call. But the recv() call doesnt return unless something is read. Now i need to be able to send data to any of the sockets or all sockets at any given time, i'm not very experienced with threads so i really didn't know if you could call send() while a recv() is waiting (which would be the case if one socket would receive something and i would call send() on all the sockets to broadcast it back) but i read somewhere that u couldn't read if it was writing and couldnt write if it was reading or something. So how do i handle this?? I've already tried to come up with a number of (be it somewhat rediculous) solutions like a main buffer in which all the receives are put and a counter for each socket keeps track of how many lines each socket needs to get out of the buffer to send, but then u get another problem because then the sockets will only send when they have received something.. :confused: Of course if it is safe to call recv() without knowing if somewhere else send() is being called then pls let me know, because i thought i read somewhere in the winsock API that it isnt but i looked again and couldnt find it anymore. Thanks Kuniva --------------------------------------------

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

      You can safely do a send and recv on the same socket at the same time, so no need to start inventing all manner of problems for yourself!

      1 Reply Last reply
      0
      • K Kuniva

        I made a multithreaded tcp server which loops on a listening socket which accepts the connections on their own socket and spins them off in their own thread. In the thread, it just loops on the recv() call. But the recv() call doesnt return unless something is read. Now i need to be able to send data to any of the sockets or all sockets at any given time, i'm not very experienced with threads so i really didn't know if you could call send() while a recv() is waiting (which would be the case if one socket would receive something and i would call send() on all the sockets to broadcast it back) but i read somewhere that u couldn't read if it was writing and couldnt write if it was reading or something. So how do i handle this?? I've already tried to come up with a number of (be it somewhat rediculous) solutions like a main buffer in which all the receives are put and a counter for each socket keeps track of how many lines each socket needs to get out of the buffer to send, but then u get another problem because then the sockets will only send when they have received something.. :confused: Of course if it is safe to call recv() without knowing if somewhere else send() is being called then pls let me know, because i thought i read somewhere in the winsock API that it isnt but i looked again and couldnt find it anymore. Thanks Kuniva --------------------------------------------

        N Offline
        N Offline
        Norman Bates
        wrote on last edited by
        #3

        there is some very good information on your subject here on codeproject: http://www.codeproject.com/internet/ also, take a look at the "reusable, high performance, socket server class"... nb

        K 1 Reply Last reply
        0
        • N Norman Bates

          there is some very good information on your subject here on codeproject: http://www.codeproject.com/internet/ also, take a look at the "reusable, high performance, socket server class"... nb

          K Offline
          K Offline
          Kuniva
          wrote on last edited by
          #4

          wow thanks! being able to call it at the same time makes it easier hehe. I already looked on CP though and also at that reusable class but that uses IOCP and i am targetting a win98 machine so that wouldnt work.. :( Kuniva --------------------------------------------

          1 Reply Last reply
          0
          • K Kuniva

            I made a multithreaded tcp server which loops on a listening socket which accepts the connections on their own socket and spins them off in their own thread. In the thread, it just loops on the recv() call. But the recv() call doesnt return unless something is read. Now i need to be able to send data to any of the sockets or all sockets at any given time, i'm not very experienced with threads so i really didn't know if you could call send() while a recv() is waiting (which would be the case if one socket would receive something and i would call send() on all the sockets to broadcast it back) but i read somewhere that u couldn't read if it was writing and couldnt write if it was reading or something. So how do i handle this?? I've already tried to come up with a number of (be it somewhat rediculous) solutions like a main buffer in which all the receives are put and a counter for each socket keeps track of how many lines each socket needs to get out of the buffer to send, but then u get another problem because then the sockets will only send when they have received something.. :confused: Of course if it is safe to call recv() without knowing if somewhere else send() is being called then pls let me know, because i thought i read somewhere in the winsock API that it isnt but i looked again and couldnt find it anymore. Thanks Kuniva --------------------------------------------

            I Offline
            I Offline
            igor1960
            wrote on last edited by
            #5

            You can use select() and specify maxtime your recv() will be waiting. When time expires recv() will return even if no data arrived. So, you can create arriving loop and inside of it you will be able to send(). Regards "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

            K 1 Reply Last reply
            0
            • I igor1960

              You can use select() and specify maxtime your recv() will be waiting. When time expires recv() will return even if no data arrived. So, you can create arriving loop and inside of it you will be able to send(). Regards "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

              K Offline
              K Offline
              Kuniva
              wrote on last edited by
              #6

              Oh thanks, i was just wondering about something like that since i wanted to put in something to check for keystrokes. Thanks :) Kuniva --------------------------------------------

              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