Advertisement

Sockets questions :?

Started by January 02, 2004 02:07 PM
6 comments, last by hdc_1024b 21 years, 1 month ago
Hmmm, i''ve got a problem, when i try to recieve using the recv function, the socket hangs there unstill it recieves something and in consecuence hangs my program, i could use threads to avoid this but, is there any other way arround this? Thanks.
Lookup nonblocking threads. That means that if there is no data when you call the recv function it keeps going on through your code. You are currently using blocking sockets, which means that the code will wait on the recv until it gets something.

~Graham

----
while (your_engine >= my_engine)
my_engine++;
Advertisement
quote:
Original post by gwihlidal
Lookup nonblocking threads


You mean sockets right?
Ya, I''m a little tired =)

"Nonblocking sockets"

~Graham

----
while (your_engine >= my_engine)
my_engine++;
or you could check the socket for activity and only read from the Socket when there is activity.

if theres nothing you dont call recv().

i used it that way in SDL_net, not sure if it works in your project of course.

Lazzar

---------------------------------------------------------
if god gave us the source code, we could change the world!
---------------------------------------------------------if god gave us the source code, we could change the world!
Another option you can use (assuming your using Windows, not sure about other platforms) is asynchronous sockets. With this, you register a message for Windows to send you every time there is data for you to recieve. This way, you can go on with your normal code, and recieve the data only when it''s there.
Advertisement
if you''re using linux then look into fcntl() to set the socket in a non-blocking state then simply use select() to check it.

- Damage Inc.
quote:
Original post by Anonymous Poster
if you''re using linux then look into fcntl() to set the socket in a non-blocking state then simply use select() to check it.

- Damage Inc.


i''m using that method in windows, its just called another name
ioctlsocket, look that one up in msdn and google for select and you should get going, working fine for me at least.

could share a bit from my socket-class

bool CSocket::setBlocking(SOCKET& socktoset,bool isblocking /* = true */)
{
unsigned long* arg = new unsigned long;
*arg = (int)!isblocking;
if(ioctlsocket(socktoset,FIONBIO,arg) == 0) //zero = successfull
{
yay, we''ve got a nonblocking socket
}
else
{
crap, didn''t work
}
}
reality is only an option

This topic is closed to new replies.

Advertisement