Sending and receiving structs (I read FAQ)

Started by
11 comments, last by dpadam450 12 years, 11 months ago

I'm using non blocking because I'm sending game data. If select is what I need then I will look into it. Thanks!

*edit*
I have a new problem. The program works great in terms of successfully sending information. However, there is about a 10 second lag when I'm trying to send realtime information. This is being sent over local 127.0.0.1. The server takes a long time to send the data after it has been received. and the client takes a long time to send the data to the server.


One cause may be if you're sending many small messages over TCP, they may not actually be transmitted until the outgoing stream buffer is of a certain size.

setsocketopt - Check out TCP_NODELAY
- Teach a programmer an answer, he can code for a day. Show a programmer the documentation, he can code for a lifetime.
Advertisement

[quote name='rnw159' timestamp='1304566321' post='4806732']
I'm using non blocking because I'm sending game data. If select is what I need then I will look into it. Thanks!

*edit*
I have a new problem. The program works great in terms of successfully sending information. However, there is about a 10 second lag when I'm trying to send realtime information. This is being sent over local 127.0.0.1. The server takes a long time to send the data after it has been received. and the client takes a long time to send the data to the server.


One cause may be if you're sending many small messages over TCP, they may not actually be transmitted until the outgoing stream buffer is of a certain size.

setsocketopt - Check out TCP_NODELAY
[/quote]

Thanks this helped a lot! The problem I have now is that if I continually send data the lag gets greater and greater until I stop sending data for a few seconds. CAn you offer some help on this?

*edit*
Also it gets twice as laggy for every client I add. Any suggestions?
Quote:Original post by rnw159Screw game maker.There's your sig quote for the forums :D[/quote]

Thanks this helped a lot! The problem I have now is that if I continually send data the lag gets greater and greater until I stop sending data for a few seconds. CAn you offer some help on this?


The server needs to drain all available incoming data for each iteration. This may include more than one packet from each client.
Also, because it's TCP, you may receive half packets -- see the FAQ.
Typically, you will create a buffer of some number of kilobytes per client, and then try to recv() that buffer full in each call to recv(). Then copy data out of there for your struct.

struct ClientBuf {
size_t count;
char buf[8192];

void poll(int socket) {
if (count == 8192) { return; } // already full
int size = 8192-count;

int r = recv(socket, &buf[count], size, 0);
if (r > 0) { count += r; }
}

bool consume_data(void *dst, size_t size) {
if (count < size) { return false; }
memcpy(dst, buf, size);
memmove(buf, &buf[size], count-size);
count -= size;
return true;
}
};


This code assumes that the socket is non-blocking, or that select() has told you there is data to receive. Call poll() once per frame per socket that's readable (or non-blocking). Then, call consume_data() to consume data from the incoming connection, assuming sufficient data is available. Remember: There may be more than one packet in the buffer.
enum Bool { True, False, FileNotFound };
The lag is caused by 2 things. 1 sending packets over and over. Put a time like 15 ms between sending game data. So only send 1 packet per person every 15 ms.

2. Make sure that you call receive until you have no more data on the network. If not, you will get 2 messages, and read 1. next frame you get 2 more messages (3 in your 'inbox). Read one and get to more you now have 5 messages. So just make sure to loop until there is no more data.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

This topic is closed to new replies.

Advertisement