Hi,
How do I empty a socket buffer?
I am using sendto() function.
Many Thanks,
John
Printable View
Hi,
How do I empty a socket buffer?
I am using sendto() function.
Many Thanks,
John
I'm not sure I understand your question. Do you mean writing over the content of your send buffer? In that case, you can do:
Was that it?Code:char buf[100];
/* ... */
memset(buf, 0, sizeof buf); /* clears the buffer */
/* ... */
what do You mean with "empty" the buffer? Do You mean how to read You sent data?
You can receive Your data with recvfrom.
Mikey
Hi J0nas,
My code is to do with client/server communication.
There are 2 steps to be done,
step 1 would be to get authenticated by the server
once authenticated, I have to send the actual data.
So when I do the first sendto() it goes through correctly.
However, when I execute the second sendto() it goes with some junk data.
In order to get the second sendto() to send correct data, I need to empty this buffer.
[CODE]
======================================
FOR THE AUTHENTICATION
sendto(sd,"<REQ><LOGINID>8888</LOGINID>\n",30,0,(struct sockaddr *)&saServer,sizeof(saServer));
FOR THE ACTUAL DATA SENDING
sendto(sd,"<REQ><MESSAGE>HELLO</MESSAGE>\n",32,0,(struct sockaddr *)&saServer,sizeof(saServer));
==========================================
It is at the second sendto() that I have a problem.
On another note, if I send the second sendto() prior to the first sendto() then it works. So i presume it has got to do with emptying the buffer contents.
Kindly help!!!
John
Quote:
Originally posted by j0nas
I'm not sure I understand your question. Do you mean writing over the content of your send buffer? In that case, you can do:
Was that it?Code:char buf[100];
/* ... */
memset(buf, 0, sizeof buf); /* clears the buffer */
/* ... */
Hi Mikey,
My code is to do with client/server communication.
There are 2 steps to be done,
step 1 would be to get authenticated by the server
once authenticated, I have to send the actual data.
So when I do the first sendto() it goes through correctly.
However, when I execute the second sendto() it goes with some junk data.
In order to get the second sendto() to send correct data, I need to empty this buffer.
[CODE]
======================================
FOR THE AUTHENTICATION
sendto(sd,"<REQ><LOGINID>8888</LOGINID>\n",30,0,(struct sockaddr *)&saServer,sizeof(saServer));
FOR THE ACTUAL DATA SENDING
sendto(sd,"<REQ><MESSAGE>HELLO</MESSAGE>\n",32,0,(struct sockaddr *)&saServer,sizeof(saServer));
==========================================
[CODE]
It is at the second sendto() that I have a problem.
On another note, if I send the second sendto() prior to the first sendto() then it works. So i presume it has got to do with emptying the buffer contents.
Kindly help!!!
John
Quote:
Originally posted by Mikey
what do You mean with "empty" the buffer? Do You mean how to read You sent data?
You can receive Your data with recvfrom.
Mikey
have You checked the socket-state with select()? It's important, to find out if You are allowed to write (and read)!
Mikey
Yes Mikey, everything is fine that end.Quote:
Originally posted by Mikey
have You checked the socket-state with select()? It's important, to find out if You are allowed to write (and read)!
Mikey
John
Are you sure the amount of data you stated in sendto was the right one? I mean, the string length seems to be different from these 30-32 values.
Hi,Quote:
Originally posted by sgenie
Are you sure the amount of data you stated in sendto was the right one? I mean, the string length seems to be different from these 30-32 values.
Yes the string lengths are okay, the very fact that the first sendto() works perfectly eliminates this doubt.
Secondly, If i send the second sendto() prior to the first one, it also goes thru correctly without any problem.
It's 2 sendto()'s one after the other is a problem!!!
Thanks,
John
Most probably there is no problem with the sento calls and there is no need to empty any buffers. Are you sure the server receives the junk data from the same client ? Are you sure your server doesn't have some other bug that causes data corruption ?
Is it only the data that looks strange or the lenght is also changed ?
Can you please post some server code so we can get more details ?
Thanks,
Dan