I am working on my first winsock program. How do I keep listening for incoming connections and then stop when one comes?
I am working on my first winsock program. How do I keep listening for incoming connections and then stop when one comes?
Website(s): http://www16.brinkster.com/trifaze/
E-mail: [email protected]
---------------------------------
C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
DirectX Version: 9.0b
DX SDK: DirectX 8.1 SDK
You can keep it on recv() inside a while(1) loop, recieve what you want and then process it, and then break when the return from recv() is SOCKET ERROR or 0 (for a graceful close)
Thanks I'll try it!
Website(s): http://www16.brinkster.com/trifaze/
E-mail: [email protected]
---------------------------------
C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
DirectX Version: 9.0b
DX SDK: DirectX 8.1 SDK
Could you give me an example, because I want to receive it when it comes in, stop listening, and put the received info in my EDIT box. So, if you would, give a small example of what you're talking about.
Website(s): http://www16.brinkster.com/trifaze/
E-mail: [email protected]
---------------------------------
C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
DirectX Version: 9.0b
DX SDK: DirectX 8.1 SDK
If you want to keep listening for incoming connections, all you need to do is make one call to accept() if you're dealing with blocking sockets (default). This will alert the system to wait until a request comes in, and proceed to the next block of code once the connection is made. You could then put recv() in a loop, read all the data you want, and then ship it off to your edit:
SOCKET socket;
SOCKET incomingConnection;
char data[256];
// socket(), bind(), etc.
listen(socket, ...);
incomingConnection = accept(socket, ...);
recv(incomingConnection, data, ...);
// send the data to the edit control
Visit http://www.hal-pc.org/~johnnie2/winsock.html for a small primer.
"Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C
Thanks johnnie! That works.
When I open the one that receives, it already has the text in the EDIT box and a buch of weird characters. What do I do?
Website(s): http://www16.brinkster.com/trifaze/
E-mail: [email protected]
---------------------------------
C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
DirectX Version: 9.0b
DX SDK: DirectX 8.1 SDK
What exactly are you doing so that you already have data when the socket connects?
"Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C
I am creating the listening socket, and then:
recv(listening, example, strlen(example), 0);
What do I do to receive ONLY when something comes in?
Website(s): http://www16.brinkster.com/trifaze/
E-mail: [email protected]
---------------------------------
C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
DirectX Version: 9.0b
DX SDK: DirectX 8.1 SDK
Well, if you know exactly when data will come in, you can manually schedule the recv's. If not, use asynchronous sockets. They give you a notification via your window procedure when data is available to be read. You could also launch a thread that runs in an endless loop calling recv(); when data comes in, the thread puts the data into some type of global storage. The rest of the program could then check to see if data has come in, and if it has, use it. That process is exactly what happens with asynchronous sockets though, so you might really want to look into that concept if you have no idea when data will be sent.
"Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C
I am working on the Asyncronous sockets right now, but do you have an example of code I could try using these?
Website(s): http://www16.brinkster.com/trifaze/
E-mail: [email protected]
---------------------------------
C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
DirectX Version: 9.0b
DX SDK: DirectX 8.1 SDK
There's a tutorial on gamedev that provides a couple code samples along with explanations.
"Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C