Log Class with Winsock
Hi, I''m new to winsock and am having a little problem:
I have a log class which can output log info to a remote client. Unfortunately, if I send the packet (512bytes) over the network, only some show up. Is this because:
1) I''m sending packets to quickly before the remote client has a chance to recieve them?
2) I need to have a delay to ensure the packet arrives before I send out new ones?
3) I''ve f**ked up badly somewhere??
I know I havn''t given many details here, but any ideas?
Thanks
My guess is no. 3.
But you say that when you send THE message (indicating that you send one or few packets).
In question 1 you wonder if you are sending too many messages at once...
So how many do you send per second ?
But you say that when you send THE message (indicating that you send one or few packets).
In question 1 you wonder if you are sending too many messages at once...
So how many do you send per second ?
-------------Ban KalvinB !
Well, you see, its like this. Because I''m logging stuff, its like every other line of code could be a potential log:
So as you can see, it can be many times a second... maybe i''m just expecting to be able to fire and forget output to much in to little time...? in that case, is a file the only realistic logging method?
Thanks for the help though!
Log.Output(LP_SERIOUS,"Doing something...");SomeFunc();Log.Output(LP_SERIOUS,"Doing something else...");memcpy(blah blah);Log.Output(LP_SERIOUS,"Doing something different...");FictionalStruct.dwMemberVar = 34334;
So as you can see, it can be many times a second... maybe i''m just expecting to be able to fire and forget output to much in to little time...? in that case, is a file the only realistic logging method?
Thanks for the help though!
Are you using UDP? UDP doesn''t gaurantee delivery, so packets are allowed to be dropped if network traffic is high or whatever. Either switch to TCP, or write your own gauranteed delivery method (it''s not that hard).
codeka.com - Just click it.
codeka.com - Just click it.
April 09, 2002 01:46 AM
(Assuming you''re using TCP..)
IT''s possible that in your network code, you aren''t checking how many bytes have actually been sent when you use send() ??
You should check this, and buffer any data that wasn''t sent, to be resent again later (even immediately...)
IT''s possible that in your network code, you aren''t checking how many bytes have actually been sent when you use send() ??
You should check this, and buffer any data that wasn''t sent, to be resent again later (even immediately...)
AnonP - that''d be my guess too (if he is using TCP)
Check to make sure you are sending *all* the information.
Dire Wolf
www.digitalfiends.com
Check to make sure you are sending *all* the information.
Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
www.digitalfiends.com
Thanks for the feedback but I got it sorted in the end ![](wink.gif)
It was pretty much as you guys described it, so I found a solution in Multiplayer Game Programming (ISBN:0-7615-3298-6, 2001 Prima Publishing):
[edited by - ferrit on April 11, 2002 1:40:27 PM]
![](wink.gif)
It was pretty much as you guys described it, so I found a solution in Multiplayer Game Programming (ISBN:0-7615-3298-6, 2001 Prima Publishing):
int SocketObject::vGetPacket(char *szbuffer){ int iBytesReceived = 0; int iBytesWaiting = 0; stPacketHeader stHeader; // Check if write pos moved if( stReceive.iWritePos != stReceive.iReadPos ) { // // Pull packet header from buffer // iBytesWaiting = (stReceive.iWritePos - stReceive.iReadPos); // Make sure a full size header is present if( iBytesWaiting < sizeof(stHeader) ) { return(0); } // Copy the header in memcpy(&stHeader,&stReceive.szBuffer[stReceive.iReadPos],sizeof(stHeader)); // Check the checksum if( ((stHeader.iType+stHeader.iLength+stHeader.iID)) != stHeader.iCheckSum ) { // Skip the first bad byte in an attempt to find a good packet stReceive.iReadPos++; if( stReceive.iReadPos >= 64000 ) { stReceive.iReadPos = 0; } // Try again for a good packet vGetPacket(szbuffer); } else { } // // Pull the body of the packet according to the size // // Make sure enough data is waiting, if not leave and try again later if( (iBytesWaiting-sizeof(stHeader)) < (unsigned)stHeader.iLength ) { return(0); } // Copy into the return buffer memcpy(szbuffer,&stHeader,sizeof(stHeader)); memcpy(&szbuffer[sizeof(stHeader)],&stReceive.szBuffer[stReceive.iReadPos+sizeof(stHeader)],stHeader.iLength); // Update Read Position & Return Values stReceive.iReadPos += (stHeader.iLength+sizeof(stHeader)); iBytesReceived = (stHeader.iLength+sizeof(stHeader)); // Check if reading too far if( stReceive.iReadPos >= 64000 ) { stReceive.iReadPos = 0; } } return(iBytesReceived);}
[edited by - ferrit on April 11, 2002 1:40:27 PM]
A couple of things to remember when using sockets.
If you''re using UDP then you can''t expect all packets to arrive as some will get lost. Packets sent that are larger than the MTU will be fragmented/lost/ignored etc. From memory the minimum MTU is 576 bytes. For safety I never send packets that are larger than 512 bytes with UDP.
If using TCP don''t forget that TCP is a stream and can merge and fragment packets as it sees fit. For example if you do two separate sends then sometimes you will get one packet that contains the two sends appended together. If you really want separate packets then included your own packet header information with size information to enable you to extract these from the stream.
When sending data via TCP or UDP remember to check errors. It is possible that the stack has run out of internal workspcae and has failed to send the packet this time around.
Martin Piper
Argonaut Games plc.
If you''re using UDP then you can''t expect all packets to arrive as some will get lost. Packets sent that are larger than the MTU will be fragmented/lost/ignored etc. From memory the minimum MTU is 576 bytes. For safety I never send packets that are larger than 512 bytes with UDP.
If using TCP don''t forget that TCP is a stream and can merge and fragment packets as it sees fit. For example if you do two separate sends then sometimes you will get one packet that contains the two sends appended together. If you really want separate packets then included your own packet header information with size information to enable you to extract these from the stream.
When sending data via TCP or UDP remember to check errors. It is possible that the stack has run out of internal workspcae and has failed to send the packet this time around.
Martin Piper
Argonaut Games plc.
Martin Piper
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement