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