8 Replies - 505 Views - Last Post: 17 June 2013 - 06:17 PM Rate Topic: -----

#1 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Winsock2 - Online Play?

Posted 14 June 2013 - 12:19 PM

Hello, DreamInCode!

Let me just start off explaining my game (So far).

I've got a program written in C++ which is just basic SDL programming.
It displays a character, and a background behind the player.
The player is moveable (player_x,player_y) by using the left, right, up, and down arrow keys.

Now, Here's the problem:

I'm trying to make it an ONLINE MULTIPLAYER game.
Currently, I watched a video and multiple tutorials on Winsock2.
So out of this, I created a 'Server' program, and the game with the character and background as the 'Client' program.
Right now, All that happens is the client sends the player position (player_x,player_y) to the server and the server recieves it.

However

The game only works once?
As in when I open the server program, if I open the client program once; it works. But if I open a second client; it does the whole "Not Responding" thing in a frozen screen.

How can I make it so that multiple people can join the game at once?

Is This A Good Question/Topic? 0
  • +

Replies To: Winsock2 - Online Play?

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3467
  • View blog
  • Posts: 10,688
  • Joined: 05-May 12

Re: Winsock2 - Online Play?

Posted 14 June 2013 - 03:25 PM

Without seeing the code for your server, and the relevant networking code for your client, we are hard pressed to help you. We can make all kind of assumptions, but inadvertently lead you down the wrong path. So in short, post your code, or if not your code, a detailed description along with pseudo code of your server and client pieces.
Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1311
  • View blog
  • Posts: 4,503
  • Joined: 19-February 09

Re: Winsock2 - Online Play?

Posted 15 June 2013 - 02:00 PM

Here's a start.

Winsock Multi-Client Servers
Was This Post Helpful? 0
  • +
  • -

#4 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Re: Winsock2 - Online Play?

Posted 16 June 2013 - 11:51 AM

I'm going to try to read that link you replied, define.

But for the purpose of figuring my own code/how to fix it (Educational for me!) I'll post my code. Maybe some of you can help me understand a little more about what I can do to help myself with this in the near future?

NOTE: These are pseudo codes, As Skydiver had suggested (Detailed).

-------Server-------
//Included files are here. In this case:
#include <iostream>
#include <winsock2.h>
#include <pthread.h>

//Next I have my thread. This thread currently does nothing!
//Q: Do I need to add my 'Server Code' here?

//I then have my main.
int main() {
     //This is where I add my Server and Client connections using sockets.
     //Just in case something is wrong here, I'll post it!
     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 LISTED HERE");
     Server.sin_family = AF_INET;
     Server.sin_port = htons(100);
    
     bind(Listen, (SOCKADDR*)&Server, sizeof(Server));
    
     listen(Listen, 4);
    
     int size = sizeof(Server);
     //JUST A NOTE: I did not make this code by myself. I watched a YouTube video demonstrating how to do this!
     
     //Here is where I placed my threading (Or what you want to call it)
     pthread_t threads[MAX_THREADS]; //Q: Is this the correct use?

     //I then added a forever loop here to do the following:
          Connect = accept(Listen, (SOCKADDR*)&Server, &size); //Again; I found this on the YouTube video
          
          //I then added a loop to re-thread as long as the amount of players does not exceed the max amount of threading (MAX_THREADS)
          
          //Next I asked the server if the connecting worked.
          //It suceeded!

          //I added anything else using send and recv here that I needed! As long as the server has connected properly.



Although it is merely a detailed description of my server, Is this enough to understand where I went wrong?

I know it lies within my threading; Concidering it doesn't do anything but count the number of players connected.
Counting players works dearly! This is off concidering only one player can actually play.
(If 2 players join, the first player can play the game. The second cannot and obtains a frozen window. HOWEVER, It still counts that 2 people have connected).

Let me know if you need extra detail(s)!
I know this may not be all that great for an example..

By the way, define, did you make your profile image? It's quite beautiful to look at=)
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3467
  • View blog
  • Posts: 10,688
  • Joined: 05-May 12

Re: Winsock2 - Online Play?

Posted 16 June 2013 - 07:55 PM

If your server is showing the "not responding" message on the window chrome, you can attach a debugger to the program and break into it on see where it is in the processing loop. Additionally, you can examine the variables state to see what is causing the infinite loop.

Personally, I'm a bit leery of what you described in on line 43 of your pseudo code. You should not ever need to reset threads or any other custom thread management other than start up a thread to handled the accepted socket. When a client eventually disconnects, you merely remove them from the threads list. This would be a lot easier if you used a vector<> instead of an array.
Was This Post Helpful? 0
  • +
  • -

#6 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Re: Winsock2 - Online Play?

Posted 16 June 2013 - 09:11 PM

View PostSkydiver, on 16 June 2013 - 07:55 PM, said:

If your server is showing the "not responding" message on the window chrome, you can attach a debugger to the program and break into it on see where it is in the processing loop. Additionally, you can examine the variables state to see what is causing the infinite loop.

Personally, I'm a bit leery of what you described in on line 43 of your pseudo code. You should not ever need to reset threads or any other custom thread management other than start up a thread to handled the accepted socket. When a client eventually disconnects, you merely remove them from the threads list. This would be a lot easier if you used a vector<> instead of an array.

Sorry, I didn't describe it well enough!
Line 43 is this:
rc = pthread_create(&threads[i], NULL, ClientThread, (void *)i);

rc is defined as an 'int', btw.

Now, I understand what you mean by removing them. I can see how that would effect a lot.
But I can't remove what's not there! People can't join=(

I also am not sure what I'm supposed to look for here, on the debugger.
Everything is working properly, Including the threading.
It just won't accept multiple players?

I am going to honestly say i'm not the best with sockets. In fact, I'm still quite new to it and learning.
What do I need to put INSIDE my thread?
All it does right now is counts the total number of players connected - This works.
Everything else is like this:

Thread:
-Counts players (+1 on connect)

Main:
-No Loop:
--Server Info
--Loop:
---Connection Accept
---If connection accept, loop threading on player join
---This is also where I place sending and recving data
Was This Post Helpful? 0
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1311
  • View blog
  • Posts: 4,503
  • Joined: 19-February 09

Re: Winsock2 - Online Play?

Posted 16 June 2013 - 09:32 PM

Hi, fysez, the link I gave was for a single-threaded program so is different form what you are trying to do.

For a multiple-client server

How To Code A Multi-client Server In C++ Using Threads
TCP client and server

The CreateThread function creates a thread using the user defined function (UDF) specified. The UDF will contain the client.

Checking function return values can help.
Does your server report that a second client is connected?

Getting Started with Winsock

Ahh, the avatar was from Portrait Avatar Maker
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3467
  • View blog
  • Posts: 10,688
  • Joined: 05-May 12

Re: Winsock2 - Online Play?

Posted 16 June 2013 - 09:41 PM

You say that line 43 in your pseudo-code translates to just creating a thread in real code, but your pseudo code still talks about "loop threading on player join". Where is the loop in the code:
rc = pthread_create(&threads[i], NULL, ClientThread, (void *)i);



You might as well post the entire code for your server, because we are running into a communications barrier here.
Was This Post Helpful? 0
  • +
  • -

#9 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Re: Winsock2 - Online Play?

Posted 17 June 2013 - 06:17 PM

Thank you guys for your amazing help!
Thanks, define, for your awesome links!
I read the links you gave me and I fixed up my codes a bit!
They work PERFECTLY now!
It seems I didn't set up my threading correctly and long-story-short,
What happened was that it was trying to thread the same client over and over during the loop (bad move!), So I just re-routed it (Did I use that word correctly?) and made it count and send buffers to each client.
This ended out working GREAT and now I can hold as many clients together at once!(That is, unless there's a limit I don't know about!)

I haven't got it working where my SDL program can see each player, But I did test this by making it so that the first connected player controlls everyone else =D
Now whenever player 1 moves, player 2(Or anyone after 2) follows.

This is awesome, I love you guys<3

-Fysez

Oh, Btw define, I clicked that avatar link and made my own.
I'm using it on facebook now =P
I'd show you my avatar but I have no idea how to place images in here (Little new to DIC, still figuring this stuff out)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1