network logic problem (pong)

s m a t
Unregistered
 
Post: #1
Ok, created one application that can act as a host or client. If it's used as a host and a connection comes in, it morphs into a client, so that now two clients are connected and can exchange with each other.

For my game to exchange information I use cmd's like "x456y456" to send coordinates or "i"(request accepted) and "n"(request denied). I have planned that one client first makes a request if it's now okay to send information, waits for the confirmation of the other client and then sends the information.

now comes the problem, in order for both machines to run at fullspeed/best fps I marked the sockets non-blocking, so far no problem, but when now both clients make a request at the same time and wait for the confirmation, they get a wrong confirmation but a request.

client 1: send request (3 bytes), wait for answer (confirmation expected 1 byte)

client 2: send request (3 bytes), wait for answer (confirmation expected 1 byte)

client 1: recv answer/data (1 byte from a request) 2 bytes remain in queue and it thinks the request was not accepted, so dont send anything

client 2: makes the same as client one and also dont sends anything.

lool, nice - noone is sending information. how can I recv such information in a secure way? I need to exchange also other cmd's over this socket.
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #2
You need to use "Synchronous I/O Multiplexing", its a much preferred over non-blocking sockets, just read the sockets if they have something to be read. You can also check where one of several sockets have information to be read from them.

This tutorial is very good, and may help explain the specifics
http://beej.us/guide/bgnet/output/html/a...l#blocking
http://beej.us/guide/bgnet/output/html/a...tml#select

Also there is an example of a networked pong game if you want too see some source code from a working example:
http://www.gamewiki.evolpenguin.com/inde...title=Pong

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
s m a t
Unregistered
 
Post: #3
thx, the links are very good stuff even if the pong doens't work out-of-the-box.

the funny thing is that I used select AND non-blocking sockets Smile

okay, let's re-think this;
even if I use blocking sockets with select() it can happen that client 1 sends stuff and client 2 does this also so that noone of both geht their confirmation byte but one byte from a request. I think I have to re-work this, perhaps dont use the confirmation cuz it needs time:

client 1 sends coords
client 2 waits for data, if data > reads it and answers with own coords -> this client does never send coords without prior recv of coords

this way it is for sure that the epxtected data arrives and doesn't get confused.
perhaps I use another mechanism - but let's see ... Smile
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #4
The syntax highlighting has broken my code Blink
If you go to edit, and copy it all and paste it into a new document
and uncomment line 4 '#define TESTING_LOCALLY'
(bear in mind you can't use two listening UDP sockets or two sending UDP sockets on the same port)
compiling with
gcc pong.c -framework SDL -framework Cocoa -framework OpenGL -framework GLUT -lSDLmain -o pong
should do it.

Then you can run the server using ./pong , then open two new terminal's and ./pong 127.0.0.1 in each
That should start a networked game between the two.

Using two clients makes little sense, I think you should either go for this approach, server and two clients, or make one client and the other client will be a hybrid which also acts as a server.

not too sure if select'ing a non-blocking socket is a good idea, my program works without.

s m a t Wrote:even if I use blocking sockets with select() it can happen that client 1 sends stuff and client 2 does this also so that noone of both geht their confirmation byte but one byte from a request.
Not quite sure what you mean here but you can definitly send lots of packets across both ways and read all the ones that make it (which will be 100% of them when testing locally).

s m a t Wrote:perhaps dont use the confirmation cuz it needs time
there's no packet loss handeling with my Pong example, it will be perceived as lag but dealing with packet loss is tricky so you should only implement it once you have the basics done.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
s m a t
Unregistered
 
Post: #5
yup works great your pong.

I found a solution for my problem thanks to the links you provided.

I ask me if it's normal that sdl is so slow? Or may this be because I use tcp?
How many times is udp faster than tcp?


And even if I disable networking my simple SDL example(fresh sdl project, background and one surface is drawn, surface moves as mouse moves) is very slow and laggy on my eMac 900MHz ans 32MB graphics. When I think that other complex 3D games run better as my little simple 2D game even without network I begin wondering ... ???
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #6
UDP is a hellavafuqload faster than TCP, my first mistake was making a realtime networked game with TCP, I got quite worried at the speed of it.
TCP has all the packet loss handeling and so on done for you whereas UDP is less reliable but faster but a long long way, definitly use UDP for realtime networking.

SDL isn't generally slow, its probably somthing slightly wrong happening down the line, you can definitly write a 3D networked games on your computer, the only test you need is can you play and already existing one Smile

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
s m a t
Unregistered
 
Post: #7
great, thx for your help Smile

btw, can you explain how you did the angle calculations when the ball is colliding? they look very simple, but for yet I didn't understand them(had only a short look).
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #8
no angle calculations,
reflect the y velocity or x velocity depending on if you're reflecting against a horizontal or vertical wall.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2009.08
Post: #9
Hi there,
Hi unknown,
Thanks for help, but anyone have a example using UDP ?
I have some example using TCP but when I changed to use UDP, it doesn't work !!!

thanks
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  java network api San_Andreas 1 3,240 May 1, 2008 08:05 AM
Last Post: AndyKorth
  Getting network connection for a specified PID Florian 6 5,061 Dec 23, 2006 03:46 AM
Last Post: Florian
  Ways to Network. BinarySpike 28 12,666 May 10, 2005 07:44 PM
Last Post: kodex
  Network play David 15 7,687 Apr 10, 2003 05:56 AM
Last Post: rvangaal
  Network Programming henryj 9 5,493 Jan 6, 2003 07:42 PM
Last Post: henryj