MichaeldeJong

Members
  • Content count

    20
  • Joined

  • Last visited

Community Reputation

100 Neutral

About MichaeldeJong

  • Rank
    Member
  1. time synchronization on server & client

    Thanks. I got it working now.
  2. Hello there. I've got a float variable that increments every frame on the server. How do I synchronize this value with the timer on the client side since the client needs this? Thanks.
  3. Clientside prediction & correction

    Anyone? ...
  4. Hello. I'm creating a 2d sidescroller mmorpg and I wanted to ask how to program client-side prediction & correction? Well especially the correction, because prediction is basically just running the physics code you have running on the server. The server sends corrections about every two seconds. This messages contains X & Y positions and X & Y velocity and the Input like left 0, right 1, jump 1... Edit: Is it ok that I do the same for other clients? So instead of sending snapshots of position and velocity, I only send input that has changed, and the local client will predict where the other clients are moving to. The server sends corrections every 2-3 seconds. Thanks a lot!
  5. winsock tcp lag *UPDATE* Corrupted messages

    [quote name='Madhed' timestamp='1324853943' post='4897349'] Might fix it. There could be other bugs of course. IMHO the whole method of extracting packets from TCP streams is a lost cause. Might as well just implement a simple reliability layer on top of UDP in the mean time. [/quote] Thanks a lot! It fixed the problem!
  6. winsock tcp lag *UPDATE* Corrupted messages

    [quote name='Madhed' timestamp='1324852563' post='4897342'] Ok let's see: [code] char buffer[1024]; memset(buffer, 0, 1024); int bytesReceived = 0; bytesReceived = recv(m_sConnect, buffer, 1024, 0); if(bytesReceived > 0 && strlen(buffer) > 0) m_ReceivedMessage += buffer; [/code] Send a 1024 char string: 1024 characters + a NULL to terminate the string. the whole buffer is filled with charcters but no NULL terminator -> strlen will search for it, going way beyond the buffer until it hits a NULL -> program [b]might[/b] crash when strlen hits protected memory. Send a string with NULL as first character eg: "\0P1,2,3". strlen will return 0 -> the whole received data will be thrown away. [/quote] Thanks, I'll look at it again later (it's kinda late now) and fix it. Edit: What if I fixed these two things, would it fix the corrupted messages problem that occur sometimes?
  7. winsock tcp lag *UPDATE* Corrupted messages

    I'll change the system and only send input state of the players to the client, so the client will simulate the physics, and the server will send the real positions once in a while. This means less messages are sent.
  8. winsock tcp lag *UPDATE* Corrupted messages

    [quote name='Madhed' timestamp='1324846891' post='4897326'] There's a potential buffer overflow in Network::Recevie(). Hint: what happens when you send a string that is longer than 1023 characters? Don't know if that explains the error though [/quote] It means it can only receive 1023 characters at a time. If you send something bigger than 1023, you'd need to wait atleast 2 frames to receive it. [quote name='Madhed' timestamp='1324846891' post='4897326'] Edit: another question: what happens when the first received byte is a '\0'? [/quote] I tested it, and nothing unusual happened...
  9. winsock tcp lag *UPDATE* Corrupted messages

    Thank you so much for your time! I really appreciate it. I'm not using threads on the client side. This is what my position update message looks like: [b]P,ID,X,Y[/b]; P = Prefix for position update message ID = Player ID X = X Position Y = Y Position ' , ' (comma) = terminator ' ; ' (semicolon) = message terminator So something like this is send to the server: Let's assume that there are 3 clients that need to be updated. The server will send something like this: [b]P,0,5,2;P,1,7,3;P,2,5,2;[/b] The client will receive this message and parse it. This is what I'm using for [b]receiving/parsing[/b]: (There's nothing special about sending the messages, you just need to put a semicolon after every command) [code] void World::CheckNetwork(Local *local) { //#######RECEIVE MESSAGES####### std::string message; int receivedMessages = 0; int maxMessages = 7; //receive a maximum of 7 messages per frame while(true) { if(receivedMessages >= maxMessages) break; message = m_Network->Receive(); if(message.length() > 0) //if succesfully received message (0 bytes = no message) { if(m_Network->MessageParse(message, 0) == "P" && m_Network->MessageParse(message, 1) == "-1") //this client's own position correction from the server local->ServerCorrection(message); else if(m_Network->MessageParse(message, 0) == "P") //regular client position update UpdateClients(message); if(m_Network->MessageParse(message, 0) == "R") //remove client RemoveClients(message); receivedMessages++; } else break; } } [/code] [code] string Network::Receive() { if(m_ReceivedMessage.length() > 2048) m_ReceivedMessage.clear(); string message = ""; char buffer[1024]; memset(buffer, 0, 1024); int bytesReceived = 0; bytesReceived = recv(m_sConnect, buffer, 1024, 0); if(bytesReceived > 0 && strlen(buffer) > 0) m_ReceivedMessage += buffer; int term = m_ReceivedMessage.find(';'); if(term >= 0) { message = m_ReceivedMessage.substr(0, term); m_ReceivedMessage.erase(0, term+1); } if(message.length() > 0)cout << message << endl; return message; } [/code] TCP is a stream based networking protocol. This means that the client sometimes receive only some bits of a message: [b]sent mesage: P,0,5,2[/b]; Client receives: [b]P,0[/b] Client receives a frame later: [b],5,2[/b]; So I just wrote something that saves these bits of a message in a buffer which is max 2048 bytes long. And on every frame the client checks if a finished message is ready. If it can find one it will remove this message from the buffer and parse & read it. [code] std::string Network::MessageParse(std::string m, unsigned int p) { vector<int> pos; for(unsigned int i = 0; i < m.length(); i++) { if(m[i] == ',') pos.push_back(i); } if(pos.size() > 0) { if(p == 0) return m.substr(0, pos[p]); else if(p == pos.size()) return m.substr(pos[p-1]+1, m.find('\0')-1); else if(p > 0 && p < pos.size()) return m.substr(pos[p-1]+1, pos[p]-2); } return ""; } [/code] Thanks a lot for your time!
  10. winsock tcp lag *UPDATE* Corrupted messages

    Sometimes I get these kind of corrupted messages, though: [img]http://speedmodeling.org/smcfiles/Howker_corruptedmessage.PNG[/img]
  11. winsock tcp lag *UPDATE* Corrupted messages

    Thanks for your help. It's working now. I disabled nagle's.
  12. winsock tcp lag *UPDATE* Corrupted messages

    [quote name='ApochPiQ' timestamp='1324775124' post='4897190'] What's likely going on is that you have confused TCP for a packet-oriented protocol. TCP is a [i]stream protocol[/i]. You put stuff in, you take stuff out - there is no guarantee that what you put in will be the same size as what you get out. All TCP guarantees is that (A) everything will arrive and (B) it will arrive in the same order it was sent. See the Multiplayer and Network Programming forum FAQ for a lot of solutions to this. [/quote] Thanks, I think the problem is still TCP_NODELAY. Let's see how I can turn it off the right way. I'm not sure which socket tho.
  13. winsock tcp lag *UPDATE* Corrupted messages

    [quote name='TheUnbeliever' timestamp='1324758190' post='4897144'] I'm going to guess these messages are pretty small? TCP will merge messages for a number of reasons, including to ensure that packets are reasonably large, minimizing overhead. Locally, there's no reason for the driver to do this, so you get the updates essentially immediately (1/17th second is ~58 ms) but as soon as you're actually broadcasting over a network, you get delays while the transmission buffer fills up. [/quote] I think you are right, because it's like receiving 3 messages at once and then having a delay, etc. So how can I solve this problem?
  14. winsock tcp lag *UPDATE* Corrupted messages

    I turned off nagle's algorithm but still the same problem.works perfectly on local host ( server-side). Only receives a few of those 17 messages per second on another pc on my LAN.
  15. Hello, I'm programming an online RPG using c++ and winsock (TCP-only / client-server based). I tested it on my LAN network and it worked fine on my own computer. It sends like 1 position update message every 60 ms. But when I'm testing this on another PC, there is a huge lag. It is only receiving a message every 500 ms or so. So it's really slow when youre testing on another computer that is not running the server. (it's still LAN) Is it actually a good idea to send position updates (17 times a second) using TCP? Thanks a lot.