6 Replies - 692 Views - Last Post: 27 March 2013 - 03:37 PM Rate Topic: -----

#1 LunaRebirth  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Multiplayer Application

Posted 21 March 2013 - 07:19 AM

Hey guys, So I had this question for a while now, but thought I could work on it on my own. Turns out, it's a little complicated for me to learn this!
I made a small App on Windows (Using C++) that has a pixel character able to move around (up, down, left, right). And I thought before I go any further, I might as well make my app online now so it's less difficult in the future.
How can I do this?
Currently, I have a server app, when it's open, the player can join. When it's not open, the player cannot join.

BUT the problem is, only one person is allowed to join 0_0?
So my main question is.. How can I get the server to connect multiple clients?
I read online I need a multithread, but I'm not sure how this works..

Can someone help explain this whole process to me?
Maybe a few examples would be nice, too!

If you need more information, leave me a comment ASAP:)
Thanks!

P.S. Currently, one server can be open, and one player can join. I want to allow a second player as well to play together (show eachother moving around as they do).

Is This A Good Question/Topic? 0
  • +

Replies To: Multiplayer Application

#2 LunaRebirth  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Multiplayer Application

Posted 21 March 2013 - 07:28 AM

I'm so sorry for the extra post on this: I'm at work, and the Internet is being really terrible today. I pressed refresh and it apparently posted it twice.
Please use this one instead!:)
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9215
  • View blog
  • Posts: 34,612
  • Joined: 12-June 08

Re: Multiplayer Application

Posted 21 March 2013 - 07:49 AM

Perhaps read up on the topic.

Examples:
What every programmer needs to know about game networking

Multiplayer Math

One way to tackle online multiplayer in platforming games

SDL_Net and C++ server

Moving to game programming..
Was This Post Helpful? 2
  • +
  • -

#4 LunaRebirth  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Multiplayer Application

Posted 22 March 2013 - 07:42 AM

View Postmodi123_1, on 21 March 2013 - 07:49 AM, said:



Thanks so much for these awesome links

View Postmodi123_1, on 21 March 2013 - 07:49 AM, said:



For some reason it isn't showing my text. I really hate this work Internet, lol. So again, I'm sorry for double-posting (if it happens again).
Thanks for the awesome links =D
But I've already got a server-client system.
I have the server that needs to be open, then the client connects, and it send the players position to the server.
The only thing I need now is how to connect multiple people.
Just kind of like one client sending their position on the screen to the server, then the server sends it to everyone else connected.
So my question is, are you wanting me to completely start over and use SDL_Net?
Or is there another acception to what I'm currently using (WinSock2)?

Because I'd really hate to start all over.

Thank you again for your awesome links, I loved them =D
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9215
  • View blog
  • Posts: 34,612
  • Joined: 12-June 08

Re: Multiplayer Application

Posted 22 March 2013 - 08:00 AM

Quote

The only thing I need now is how to connect multiple people.

... and the articles would indicate that is not necessarily that easy. Are each clients supposed to pause the game play and wait for people to update? Do you need to implement some sort of predictable movement on the client to approximate what the other people maybe doing before they do it? What about people with different latencies and so forth? Those articles sort of explain how that should be factored.

Quote

So my question is, are you wanting me to completely start over and use SDL_Net?

Do what you want. Just going off what you told me I am merely trying to point you to where the puck is going to be and not where it is.

Quote

Or is there another acception to what I'm currently using (WinSock2)?

I have no idea what that means, or what your code context looks like. It's your project.

http://www.codeproje...-a-Simple-C-Imp
http://citeseerx.ist...p=rep1&type=pdf
http://www.dre.vande...report-col4.pdf
Was This Post Helpful? 0
  • +
  • -

#6 LunaRebirth  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Multiplayer Application

Posted 27 March 2013 - 03:20 PM

View Postmodi123_1, on 22 March 2013 - 08:00 AM, said:

Quote

The only thing I need now is how to connect multiple people.

... and the articles would indicate that is not necessarily that easy. Are each clients supposed to pause the game play and wait for people to update? Do you need to implement some sort of predictable movement on the client to approximate what the other people maybe doing before they do it? What about people with different latencies and so forth? Those articles sort of explain how that should be factored.

Quote

So my question is, are you wanting me to completely start over and use SDL_Net?

Do what you want. Just going off what you told me I am merely trying to point you to where the puck is going to be and not where it is.

Quote

Or is there another acception to what I'm currently using (WinSock2)?

I have no idea what that means, or what your code context looks like. It's your project.

http://www.codeproje...-a-Simple-C-Imp
http://citeseerx.ist...p=rep1&type=pdf
http://www.dre.vande...report-col4.pdf


Here are my server and client codes:
//Server
#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;

void *ClientThread(void *threadid) {
     std::cout << "Trying to connect players:\n     Thread ID: ClientThread\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("192.168.2.11");
    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];
    int i = 0;
    
    while (gamerunning) {
          Connect = accept(Listen, (SOCKADDR*)&Server, &size);
          if (i >= 0 && i < MAX_THREADS) {
               rc = pthread_create(&threads[i], NULL, ClientThread, (void *)i);
               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 << " (This is strictly for binding connections)\n";
               test_frame +=1;
            }
            recv(Connect, test, sizeof(test), 0);
            std::cout << test << "\n";
          }
    }
    std::cin.get();
    return 0;
}


And my client code:

//Client
#include <sdl.h>
#include <winsock2.h>

SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;
SDL_Surface *player = NULL;
SDL_Surface *connect_issue = NULL;

int player_count;
int player_x = 0;
int player_y = 20;

int Counter = 0;

bool testnum_recieved = false;
bool connected_srvr = false;
int send_connect = 0;

SOCKADDR_IN addr;
int addrlen = sizeof(addr);

bool gamerunning = true;
bool keys[323] = {false};

void add_image(int x, int y,
               int srcX, int srcY,
               int width, int height,
               SDL_Surface* image, SDL_Surface* destination) {
     SDL_Rect src;
     src.x = srcX;
     src.y = srcY;
     src.w = width;
     src.h = height;
     
     SDL_Rect dst;
     dst.x = x;
     dst.y = y;
     dst.w = width;
     dst.h = height;
     
     SDL_BlitSurface(image,NULL,destination,&dst);
}

void set_trans(SDL_Surface* image,int r,int g,int B)/> {
     SDL_SetColorKey(image,SDL_SRCCOLORKEY,SDL_MapRGB(image->format, r, g, B)/>);
}

int main(int argc, char* argv[]) {
    SDL_Init(SDL_INIT_VIDEO);
    
    SDL_WM_SetCaption("Client Test",NULL);
    
    screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
    background = SDL_LoadBMP("background.bmp");
    player = SDL_LoadBMP("player.bmp");
    connect_issue = SDL_LoadBMP("connect_issue.bmp");
    
    set_trans(player, 0, 0, 0);
    //Server Stuff
    WSAData wsa;
    WORD Version = MAKEWORD(2, 1);
    
    WSAStartup(Version, &wsa);
    
    SOCKADDR_IN Client;
    
    Client.sin_addr.s_addr = inet_addr("192.168.2.11");
    Client.sin_family = AF_INET;
    Client.sin_port = htons(100);
    
    SOCKET Connect = socket(AF_INET, SOCK_STREAM, 0);
    //Server Stuff End
    char testnum[100];
    int num;
    int num_frm = 0;
    
    char* test;
    char test2[103];
    int num2;
    
    while (gamerunning) {
          if (connected_srvr == false) {
               if (connect(Connect, (SOCKADDR*)&Client, sizeof(Client))) {
                    connected_srvr = false;
               } else {
                    connected_srvr = true;
               }
          }
          if (connected_srvr == true) {
            if (num_frm == 0) {
               recv(Connect, testnum, sizeof(testnum), 0);
               num=atoi(testnum);
               num_frm = 1;
            }
            itoa(player_y, test, 10);
            send(Connect, test, sizeof(test), 0);
            player_x = atoi(test);
          }
          
          SDL_Event event;
          
          if (SDL_PollEvent(&event)) {
               if (event.type == SDL_QUIT) {
                    gamerunning = false;
               }
               if (event.type == SDL_KEYDOWN) {
                    keys[event.key.keysym.sym] = true;
               }
               if (event.type == SDL_KEYUP) {
                    keys[event.key.keysym.sym] = false;
               }
          }
          
          if (keys[SDLK_RIGHT]) {
               player_x +=1;
          }
          if (keys[SDLK_LEFT]) {
               player_x -=1;
          }
          if (keys[SDLK_UP]) {
               player_y -=1;
          }
          if (keys[SDLK_DOWN]) {
               player_y +=1;
          }
          if (connected_srvr == true) {
               add_image(0,0,0,0,800,600,background,screen);
               add_image(player_x,player_y,0,0,32,32,player,screen);
          } else {
               add_image(0,0,0,0,640,480,connect_issue,screen);
          }
          
          SDL_Flip(screen);
    }
    SDL_Quit();
    return 0;
}


(Yes, there are a few testing code lines in there, If you notice=P).
As you can see, The client and the Server connect perfectly fine!
The problem is, Only one person can even play..
When the server is not open, The client recieves a message saying that the server is not open(The connect_issue).
When the server IS open, the player can play his small application (Hooray!).
BUT, when any additional players try to join, they cannot see anything. Just a black screen. No connect issue screen, no game, nothing. Just a blank black screen. And it freezes the app (Not responding).

Although your links were GREAT at letting me know what's coming, I think i'd much rather appreciate a person-to-person conttact help (that is, If you're willing to) to help me understand my problems on this, since it does seem like it's going to be a huge pain (I'm up for the challenge=P).

So IF you're willing to help, I'd like to go question-by-question rather than all at once.
So my first question --
Before making players connect Client to Client (From server, of course), how do I make it so more than one player is able to actually play while the server is running? (Remember, Currently, their screens go black and don't responde).
So how can I stop this so they can also play? (even if it means not with others; yet).

Sorry for rushing this message. Little funny story, actually.
I had gotten done typing up a better reply than this one, and I pressed "Add" and it sent me to some "Windows is not Responding" page with all those suggestions. So I had to retype everything. Sorry, it's not the same=(
I rushed because I have a dinner to go to, And i'm late xD
Sorry for that!

Anywho, Any help is seriously appreciated!!!
Thanks
Was This Post Helpful? 0
  • +
  • -

#7 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Re: Multiplayer Application

Posted 27 March 2013 - 03:37 PM

Hey,

I don't really know if this would help, but I looked up on creating a lan game in C++, since multiple people can play lan games, I am guessing if you analyze the code, you will be able to find your problem.

http://www.codeproje...Cplusplus-and-W
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1