Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3042

Winsock - Accepting multiple clients on a server?

$
0
0
Hi!
I've been having this problem for a VERY VERY VERY long time!
And I can't seem to figure it out=(
Which is why I'm here to ask for help! =D
So, Hello CG viewers!

My problem is that I've created a Client and Server program in which they communicate.
The Client is an SDL Application that allows you to play as a movable character IF YOU ARE CONNECTED TO THE SERVER.
If not, You're unable to play.
THIS WORKS!
However, Only one client is able to play on my server??
Anyone elses' window freezes and they are not allowed to play (As if not connected to the server).

Looking for people to help me out!!!

Here is my server code.
Code:

#include <iostream>
#include <winsock2.h>
#include <vector>
#include <process.h>
#include "Included/pthread.h"

#define MAX_THREADS 5

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

void *ClientThread(void *threadid) {
    std::cout << "Players: " << i << "\n";
}
             

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("MY IP ADDRESS IS HERE");
    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";
   
    //int testxd = 100;
   
    //char testx = testxd;
   
    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];
   
    while (gamerunning) {
          Connect = accept(Listen, (SOCKADDR*)&Server, &size);
          if (i >= 0 && i < MAX_THREADS) {
              rc = pthread_create(&threads[i], NULL, ClientThread, (void *)i);
              std::cout << "Threads: " << i << "\n";
              i +=1;
          }
          if (Connect != NULL) {
              std::cout << "Connection Accepted\n";
              srvr_connect = true;
          }
          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";
              send(Connect, hey, sizeof(hey), 0);
              //send(Connect, (char *)testx, sizeof(testx), 0);
              std::cout << "\n\nSent Data: " << hey << "\n";
              test_frame +=1;
            }
            recv(Connect, test, sizeof(test), 0);
            std::cout << test << "\n";
          }
    }
    std::cin.get();
    return 0;
}

I'm having troubles allowing more than one client to play on the server.

Any help would be great!
I'd also like to see communication going on so I can understand my problems and hopefully get some learning experience in with this.

Thank You Guys!

-Lunar

Viewing all articles
Browse latest Browse all 3042

Trending Articles