Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: There is a 'correct' way to send data with sockets inside gameloop? [C#]  (Read 2817 times)

0 Members and 1 Guest are viewing this topic.

kazyamof

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
I'm doing my first game with network implementation using C#.
Here is my pseudocode:

CLIENT
Start a Game Client
Instantiate the client_socket
Connect the client_socket with server
Instantiate objects (player, enemy, etc..)
Instantiate a new Thread for send/receive over TCP
Inside this new thread, a infinite loop runs. Inside this loop:
  1.1 get the player position as string, convert to byte[] and then send async through client_socket (client_socket.BeginSend) - OK
  1.2 initiate a receive async (client_socket.BeginReceive) to print what Server said to client (NOT OK, receive the same count of bytes but all '\0')
Start the new thread
Enter on gameloop and update objects and render sprites...


SERVER

Instantiate a list of client Socket
Bind the IPEndPoint to client connect
Start a new thread to listen connections
On the new thread, iterates a Infinite loop
  1.1 accept new client
  1.2 start a new thread for the new client, dedicated to receive/send data SYNC
  On this new thread,
  2.1 another infinite loop receiving data and printing on console (OK, prints the player position)
  2.2 sending back a response (OK, send a message)

Is this right? There's a real world example of a simple game, receiving and sending data continuously inside a gameloop?  I see a lot of chats, but doesn't looks like a real world exampe to be used in game...
A chat waits for a event to send... but if I'm running at 60fps, I should send and receive data every frame? The multithread approach is really necessary? Why I got the response on client side with same number of bytes that server sends, but the message is all filled with \0?

Thanks

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SFML.Net has no network module, so what are you talking about?
Laurent Gomila - SFML developer

kazyamof

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Well, I wrote the topic trying not to be about a language, but anyway...

SFML.NET doesn't have, but C# does. System.Net.Sockets.

But my question goes further than language.
I'm talking about a general way of how to send/receive data inside a gameloop. I only see chat examples on the comunity. I'm looking for a real world example of sockets inside a sfml game, doesn't matter if is .net, c++, java...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
This is the SFML forum, we try to focus on SFML specific questions. There are many other (and better) places to ask such questions. If you read the description of this sub-forum, you can even see "Help with the network package (sfml-network)" ;)
Laurent Gomila - SFML developer