![](http://www.darkomenstudios.com/doslogo.jpg)
Winsock/C++: Accept() Question
Is it possible to tell whether or not Accept() actually accepted a connection?
For example, I only want a certain portion of code to run IF a new connection was accepted, otherwise it won''t run that code.
example:
listen();
if(accept())
{
run this code;
}
Also, is there any way to make it not wait at every call to accept? Should I only be accepting if listen gets a new request?
Thanks,
Michael Bartman
![](http://www.darkomenstudios.com/doslogo.jpg)
Michael BartmanLead ProgrammerDark Omen Studios
accept() will wait until a connection is established (sorta like getch() waits for a character.
look into the event-driven (asynchronous) stuff available for whichever API you are using... you will be able to set up a callback for accept that only gets called when a connection is being made.
The only ones I've ever used are WSAAsyncSelect (Windows Sockets) and BeginAccept/Begin(whatever) (.NET Sockets)
IIRC, select() is the simplest one that lets you check to see what a socket is up to...
[edited by - Nypyren on March 8, 2003 11:52:07 PM]
look into the event-driven (asynchronous) stuff available for whichever API you are using... you will be able to set up a callback for accept that only gets called when a connection is being made.
The only ones I've ever used are WSAAsyncSelect (Windows Sockets) and BeginAccept/Begin(whatever) (.NET Sockets)
IIRC, select() is the simplest one that lets you check to see what a socket is up to...
[edited by - Nypyren on March 8, 2003 11:52:07 PM]
Hey,
Thanks for your help.
I have a few more questions:
Would I use select() after I make a call to listen() on a given socket to see if it is connecting? And then if it is, call accept()?
Also, would it be better to make seperate threads for the listen/accept stuff?
Another thing is that recv() causes the program to halt until it recieve''s something. In the threads case, I would need another thread for recieving data as well.
I will sort through my msdn cd''s for information on WSAAsyncSelect().
I am trying to make a multi user text game, if that sheds some light on my problem.
Thanks,
Michael Bartman
Thanks for your help.
I have a few more questions:
Would I use select() after I make a call to listen() on a given socket to see if it is connecting? And then if it is, call accept()?
Also, would it be better to make seperate threads for the listen/accept stuff?
Another thing is that recv() causes the program to halt until it recieve''s something. In the threads case, I would need another thread for recieving data as well.
I will sort through my msdn cd''s for information on WSAAsyncSelect().
I am trying to make a multi user text game, if that sheds some light on my problem.
Thanks,
Michael Bartman
![](http://www.darkomenstudios.com/doslogo.jpg)
Michael BartmanLead ProgrammerDark Omen Studios
I think you can use ... what was it... ioctlsocket() ... it's something like that... to find out if there is data available to read from a socket.
You can either do it using multiple threads, or polling (checking repeatedly, but allowing for other things to happen between checks)
Personally, I have totally moved over into the event-driven networking world. You can set it up so that you get a specific windows message any time an accept, read, close happens and handle it on-the-spot. Right now I'm using C#, which has glorious support for event-driven design.
I *MIGHT* have a pretty sweet C++ WM_USER-ish event-driven class still lying around if you're interested. I'll go look for it right now...
(EDIT) Well, I found the project, but I left-off in the middle of trying to implement UDP, and the testing code doesn't work properly. But you should be able to understand it at least for how I decided to do things:
WSATest Zip file
You will DEFINATELY need to read the MSDN to understand all of it.
[edited by - Nypyren on March 9, 2003 3:23:24 AM]
You can either do it using multiple threads, or polling (checking repeatedly, but allowing for other things to happen between checks)
Personally, I have totally moved over into the event-driven networking world. You can set it up so that you get a specific windows message any time an accept, read, close happens and handle it on-the-spot. Right now I'm using C#, which has glorious support for event-driven design.
I *MIGHT* have a pretty sweet C++ WM_USER-ish event-driven class still lying around if you're interested. I'll go look for it right now...
(EDIT) Well, I found the project, but I left-off in the middle of trying to implement UDP, and the testing code doesn't work properly. But you should be able to understand it at least for how I decided to do things:
WSATest Zip file
You will DEFINATELY need to read the MSDN to understand all of it.
[edited by - Nypyren on March 9, 2003 3:23:24 AM]
Hey,
Thanks for that code; it should be helpful.
I think I will look into polling, because I want to avoid threads.
Thanks,
Michael Bartman
Thanks for that code; it should be helpful.
I think I will look into polling, because I want to avoid threads.
Thanks,
Michael Bartman
![](http://www.darkomenstudios.com/doslogo.jpg)
Michael BartmanLead ProgrammerDark Omen Studios
You might want to take a look at the select()-based server in the WinSock Programmer''s FAQ. The code is a little bit tough to digest, but it manages multiple connections and doesn''t block on accept().
Hey,
I re-found an old article I had been using to learn blocking sockets long ago. It is called Beej''s Guide to Networking.
It teaches non-blocking as well.
Thanks,
Michael Bartman
I re-found an old article I had been using to learn blocking sockets long ago. It is called Beej''s Guide to Networking.
It teaches non-blocking as well.
Thanks,
Michael Bartman
![](http://www.darkomenstudios.com/doslogo.jpg)
Michael BartmanLead ProgrammerDark Omen Studios
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement