C++ Winsock2 & pthreads: Accepting multiple clients?

Hey! So I'm still having troubles connecting multiple clients to my server.

To break down what I have going:
I have a server that is supposed to accept multiple clients..
Currently, It works with one person.
While the server is running, One person is allowed to play the game (While connected to the server). If the server is down, the player cannot play (This shows that the server and client responde and work).

However, While the server is running and the client joins, the first player is allowed to play. Everyone else's window goes black and says "Not Responding" (Like any other game in which isn't working).
While these players are in "Not Responding", It still says the client has connected onto the server (But they're not able to play?).

My client code is working perfectly, But i'm having troubles accepting multiple clients onto my server.

Anyone who is willing to help is welcome! Just know that i'm not an expert (Or close) with pthreads or winsock2. Please help fix up my current server 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <winsock2.h>
#include <vector>
#include <process.h>
#include "Included/pthread.h"
//Some things i'm using or will be using in the future

#define MAX_THREADS 5
//Maximum allowed clients

bool gamerunning = true;
//Tells that the app (Server) is running while oppened
bool srvr_connect = false;
//Is the server connected to the internet? (changes later)
int rc;
//Helps create the thread (I think?)

void *ClientThread(void *threadid) {
     std::cout << "Trying to connect players:\n     Thread: ClientThread\n";
     //Checks to see if the client has connected (works)
}
              

int main() {
    //Below is the server info (ends at '//end')
    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("My IP is here");
    //'My IP is here' is not actually the quote. My actual IP is there
    Server.sin_family = AF_INET;
    Server.sin_port = htons(100);
    
    bind(Listen, (SOCKADDR*)&Server, sizeof(Server));
    
    listen(Listen, 4);
    
    int size = sizeof(Server);
    //End (Ended the; '//Below is the server info')
    
    std::cout << "Your server has been started!\nConnecting...\n";

    //The following are parts of a connecting test (Just to be sure!)
    char* hey = "620";
    char player_y_cr[101];
    int player_y;
    
    char test[102];
    int test_frame = 0;
    char *test2;
    
    pthread_t threads[MAX_THREADS];
    int i = 0;
    //i is used for determining how many players have connected.
    
    while (gamerunning) {
          Connect = accept(Listen, (SOCKADDR*)&Server, &size);
          //Accept the client
          if (i >= 0 && i < MAX_THREADS) { //Tell that the client cannot connect passed the set amount
               rc = pthread_create(&threads[i], NULL, ClientThread, (void *)i);
               std::cout << "Threads: " << i << "\n";
               i +=1;
          }
          if (Connect != NULL) { //If the connection was successful
               std::cout << "Connection Accepted\n";
               srvr_connect = true;
          }
          if (srvr_connect == true) { //If the server is connected to the internet
            if (test_frame == 0) { //Do this only once (Since the '0' changes later)
               std::cout << "Connection Sent!\nConnection Has Been Breached!\nPlayers Are Now Able to Join Your Server!\n";
               send(Connect, hey, sizeof(hey), 0);
               std::cout << "\n\nSent Data: " << hey << " (This is strictly for binding connections)\n";
               test_frame +=1; //Stops the loop of this (as said above)
            }
            recv(Connect, test, sizeof(test), 0);
            std::cout << test << "\n";
          }
    }
    std::cin.get();
    return 0;
}


I had set notes by things to tell what they do!
Hopefully I can get some more info on how to do this.

Maybe fix up my code? xP
Thanks, Guys!
I will try my best on understanding most of your replies on this.

-LunarB
This is still a problem!
Guys, I'm still having a problem on this...
Does anyone know how I can do this?

I basically just want to connect multiple clients to one server.
Step-By-Step would be nice.
Examples? Whatever else.

Haven't really got a reply on how to do this yet >.<
short of any other solution, I would try taking a look at some documentation of winsock, if you haven't done that already: http://www.google.com/search?output=search&sclient=psy-ab&q=winsock+documentation&btnK=

unfortunately, I have no idea about it myself, so I can't really help you at the mom, but, since no one else posted, I thought Id try and point you in a direction
You still need to service the client connections after they're accepted by the server.

The most natural way to manage this is to create a new thread to handle each client, while the server's main thread goes back to listening for more connections. I know you already know that from the title of the post.

Pthreads is the POSIX thread standard. But you're on Windows, so just use native Windows threads, by and large, they predate and are more flexible than POSIX.

You start a Windows thread in a C program the _beginthreadex().
http://msdn.microsoft.com/en-us/library/kdzttdcb%28v=vs.71%29.aspx

Pass in the socket as the parameter.

If you're stuck, let us know.
Well, I'm a little stuck here; yes, I'm not entirely sure on what I'm supposed to place IN the thread?
You said to "handle each client", But i'm not entirely sure on what that includes to add into the thread?

I think what I do understand is that the Server needs to add a new socket for each and every new connection, therefore: each new client.
I'm not positive on what to add INTO the socket I have going.

Thanks for the reply, Kind of hard to fix this problem with little to no replies xP
You said to "handle each client", But i'm not entirely sure on what that includes to add into the thread?
It's your code. I have no idea what your handler should do. So far you have:
1
2
3
4
void *ClientThread(void *threadid) {
     std::cout << "Trying to connect players:\n     Thread: ClientThread\n";
     //Checks to see if the client has connected (works)
}


Was was going to post a sample TCP Server, but I've posted so many relevant sample on this forum over the years (that I can't find) that I'll not do it here. I'll post it as an Article sometime soon.

In the mean time, please read this.
http://www.wangafu.net/~nickm/libevent-book/01_intro.html
thanks to all this good and helpful type Sharing.... because i needed these...
I just still have no idea what to do. That was a great link, kbw, except I don't have those included files they do in their program.
I'm trying to just stick with what I currently have included other than several i don't HAVE to have.
What my handler should do: I'm not even sure myself.
It's really just here because someone said I needed a thread, but I was never actually told what for.
Or what to add to it to accept multiple clients.

Anyone else feel like helping me out?
This is still a huge problem I feel like quitting on (though I can't). I'm ready to give up
closed account (G309216C)
Hi,

It seems you do not know programming Multi-Threaded Application. Take a Look at Multi-Threading.

Sample Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<windows.h>
#include<iostream>
using namespace std;

DWORD MultiThread()
{
cout<<"This MultiThreading";
return 0;
}

int main()
{
CreateThread(0,0,(LPTHREAD_START_ROUTINE)MultiThread,0,0,0);
cin.get();
return 0;
}
//Hopefully you can turn this useless multithreaded application into something useful 
Last edited on
How would I use that to accept multiple clients, though?
My problem is that I have no idea what to put INSIDE the thread..
Topic archived. No new replies allowed.