🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Newbie question: Sockets: Sending and Receiving

Started by
3 comments, last by d_s_lin 22 years, 10 months ago
Hi all, I have a little question. I have a set of simple client and server applications. Client only sends, and server only receives. However, it seems like the server is dropping exactly half the packets, or the client is only sending exactly half the packets. In any case, I have the client send the same packet twice for the server to receive, and I don''t understand why this has to be. Can somebody please take a look and tell me what I can do? Any help would be greatly appreciated. Thanks!! By the way, source code at: http://members.home.net/dennislin2 Dennis
Advertisement
Hi,

I have looked at your code (i must be insane .. im not a C programmer at heart ) and i have come to the conclusion after only looking at the server code, that its because your reading from the circular array for holding incomming data is basically naff :-p

Quote :
in SocketObject::vGetPacket
iBytesWaiting = (stReceive.iWritePos - stReceive.iReadPos);
thats not really correct ... what if iWritePos was 50000 and u read 20000 bytes.. iReadPos = 50000, iWritePos = 6000 so
iBytesWaiting = (6000 - 50000);
is not correct.. something like (ill try to write in C although i have never even used a C compiler lol.. i have picked a bit up by reading it though)
if(stReceive.iWritePos < stReceive.iReadPos)
{
iBytesWaiting = (64000 - stReceive.iReadPos)+ stReceive.iWritePos;
}else
{
iBytesWaiting = (stReceive.iWritePos - stReceive.iReadPos);
}

Also this is very wrong ..
iBytesReceived = (stHeader.iLength+sizeof(stHeader));

// Check if reading too far
if( stReceive.iReadPos >= 64000 ) {
stReceive.iReadPos = 0;
}

it should be
iBytesReceived = (stHeader.iLength+sizeof(stHeader));

// Check if reading too far
if( stReceive.iReadPos >= 64000 ) {
stReceive.iReadPos -= 64000;
}

or better still (i think this code is correct.. i assume % does a mod opperation:
iBytesReceived = (stHeader.iLength+sizeof(stHeader))%64000;


Hope that fixes it.. check for anything else you have done with the circular array ;-) and i havent check the client but let me know if that works.. or somone correct me if im totally wrong :-p

Tim
~ Tim
Thanks for the reply.

If you look at my client and server code (not in SocketObject.cpp), you will find that I don''t call vGetPacket(). The only functions I call are the wrapper functions to recv() and send(), which are part of the standard Winsock API.


Dennis
Ugh ! thats the last time i spent time fafing arround in somone elses C code :-p i didnt even look at the right file lol

the only thing i note at a glance is that you are sending 2 bytes (a character byte and a null byte) and recieving 3..

as far as i can see u only need send 1 byte and recieve 1 byte ! and if u need it add the null char on the end of the recieved data.

duno if that will help :-p but its a point

Tim
~ Tim
Hi Wrathgame,

Many thanks again for your advice. It still doesn''t work if I change the number of bytes on both sides to be the same. Here''s something kind of funny that I noticed over the weekend. If I''m running both the client and server programs on one computer, then I need to send the data exactly twice for the server to get one copy of the data. However, if the client and server programs are on different computers, I may need to send mutiple times. Would you happen to know if I''m missing checking/reading something at all?


Dennis

This topic is closed to new replies.

Advertisement