Listening for connection in Winsock?

This is a discussion on Listening for connection in Winsock? within the Windows Programming forums, part of the Platform Specific Boards category; I am working on my first winsock program. How do I keep listening for incoming connections and then stop when ...

  1. #1
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question Listening for connection in Winsock?

    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: trifaze_mattu@lycos.com

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    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)

  3. #3
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Post Thanks...

    Thanks I'll try it!
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: trifaze_mattu@lycos.com

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  4. #4
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question Could you...

    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: trifaze_mattu@lycos.com

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  5. #5
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    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

  6. #6
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Post Ok, Johnnie!

    Thanks johnnie! That works.

  7. #7
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Arrow I have one problem...

    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: trifaze_mattu@lycos.com

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  8. #8
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    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

  9. #9
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Arrow Well, I'm just...

    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: trifaze_mattu@lycos.com

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  10. #10
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    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

  11. #11
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question Ok, but now...

    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: trifaze_mattu@lycos.com

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  12. #12
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock issues
    By tjpanda in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2008, 07:32 AM
  2. Winsock Messaging Program
    By Morgul in forum Windows Programming
    Replies: 13
    Last Post: 04-25-2005, 04:00 PM
  3. Where do I initialize Winsock and catch messages for it?
    By Lithorien in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2004, 11:11 AM
  4. Winsock Problem
    By Noxir in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2004, 10:50 AM
  5. winsock
    By pode in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-26-2003, 12:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21