C++ - Accepting Multiple Clients

Hi guys,
I'm having a little trouble on my code.
What happens is that my Server code works GREAT! But only for one client. Which isn't what I want.
So I was wondering if I could get some help on my current code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <winsock2.h>
#include <vector>

bool gamerunning = true;
bool srvr_connect = false;
int srvr_rec = 0;

int main() {
    WSAData wsa;
    WORD Version = MAKEWORD(2, 1);

    WSAStartup(Version, &wsa);

    SOCKET Listen;
    SOCKET Connect;

    Listen = socket(AF_INET, SOCK_STREAM, 0);
    Connect = socket(AF_INET, SOCK_STREAM, 0);

    SOCKADDR_IN Server;

    Server.sin_addr.s_addr = inet_addr("192.168.2.4");
    Server.sin_family = AF_INET;
    Server.sin_port = htons(100);

    bind(Listen, (SOCKADDR*)&Server, sizeof(Server));

    listen(Listen, 4);

    int size = sizeof(Server);

    std::cout << "Your server has been started!\nConnecting...\n";

    Connect = accept(Listen, (SOCKADDR*)&Server, &size);

    while (gamerunning) {
        if (Connect != NULL) {
            srvr_connect = true;
            std::cout << "Welcome to player: " << srvr_rec << "\n";
            srvr_rec +=1;
        }
        if (srvr_connect == true) {
            if (test_frame == 0) {
                std::cout << "Connection Sent!\nConnection Has Been Breached!\nPlayers Are Now Able to Join Your Server!\n";
                test_frame +=1;
            }
        }
    }
    std::cin.get();
    return 0;
}


Please Note,
This IS my second post on this topic: The last wasn't very successful.
When a server needs to interact with multiple clients at once, you generally have two solutions:
1. Event-driven communication. Each 'tick' of your server you process requests and respond immediately. This is useful for e.g. a game server, where no large data needs to be sent and server/client interaction is generally not time consuming.
2. The dreaded Multithreadng. When a client connects the server starts a thread dedicated to being that client's indentured servant. This is useful for e.g. web/file servers where client communication is lengthy and would delay interaction with other clients.

There are probably other solutions - I'm not exceptionally experienced in networking, I just understand the concepts and have had some experience with high-level networking.
Last edited on
you may use _beginthread to start a client task:

http://msdn.microsoft.com/en-us/library/kdzttdcb%28v=vs.71%29.aspx

1
2
3
4
5
6
7
8
9
10
11
12
13
void ClientThread(void *p)
{
SOCKET *conn = (SOCKET *)p;
...
}

...

    while (gamerunning) {
        if (Connect != NULL) {
           SOCKET *client_sock = new SOCKET(accept(Listen, (SOCKADDR*)&Server, &size));
           if((*client_sock) != INVALID_SOCKET)
              _beginthread( ClientThread, 0, client_sock);
@coder777: I see,
How does this work?
Does _beginthread basically restart the thread?
Would this require me to move my server code from "int main()" to "void ClientThread()"?

Thanks, I will try this and post my results =D
@coder777: _beginthread undefined.
What does it require to use?
Okay, I got it =P
But now, here is what happens--
If they server isn't online, the client cannot connect (like usual).
But if the server IS online, the client can only connect when the server closes (before I added this, it was running fine). It will just sit there and wait for the server to close, and when it does, it runs as if connected.
However, if I have two clients and then close the server, they both connect how I want it to =D but I still have to wait for the server to close in order for both clients to connect.
Last edited on
what do you mean by 'server close'? Better show what you have right now

basically what you have to do is:
loop
{
- accept connection
- start client thread
}
in main()

this will allow an arbitrary number of clients to connect

A small change in my example:
1
2
3
4
5
6
7
8
9
10
11
12
13
void ClientThread(void *p)
{
SOCKET *conn = (SOCKET *)p;
...
}

...

    while (gamerunning) {
        if (Connect != NULL) { // this is not longer used
           SOCKET *client_sock = new SOCKET(accept(Listen, (SOCKADDR*)&Server, &size));
           if((*client_sock) != INVALID_SOCKET)
              _beginthread( ClientThread, 0, client_sock);
Still nothing =(
I'll try tweaking around with it, but I'm not having any luck..

What I mean by "Server close" is this:
-I open the server - It connects successfully!
-I open the client app - Nothing happens, the screen is black and says "Not Responding".
-I press the red 'X' on the top-right of the server app (close it).
-Client App starts working as if its connected to the server! But it is closed.

So the client app does not work until I close the server app.
It's really weird...
Last edited on
well, the whole code is only about establishing a connection. Nothing about sending/receiving

-I open the server - It connects successfully!
what das that mean? The server waits for connections...

-I open the client app - Nothing happens, the screen is black and says "Not Responding".
So what does your client do? Where does this "Not Responding" message come from?

-Client App starts working as if its connected to the server! But it is closed.
the opposite socket is closed hence the client socket doesn't do anything.

So what are you doing regarding sending/receiving?
Topic archived. No new replies allowed.