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