CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2002
    Posts
    22

    Empty Socket Buffer

    Hi,
    How do I empty a socket buffer?

    I am using sendto() function.

    Many Thanks,
    John

  2. #2
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664
    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:
    Code:
    char buf[100];
    /* ... */
    memset(buf, 0, sizeof buf);  /* clears the buffer */
    /* ... */
    Was that it?

  3. #3
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    166
    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

  4. #4
    Join Date
    Oct 2002
    Posts
    22
    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

    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:
    Code:
    char buf[100];
    /* ... */
    memset(buf, 0, sizeof buf);  /* clears the buffer */
    /* ... */
    Was that it?

  5. #5
    Join Date
    Oct 2002
    Posts
    22
    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


    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

  6. #6
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    166
    have You checked the socket-state with select()? It's important, to find out if You are allowed to write (and read)!

    Mikey

  7. #7
    Join Date
    Oct 2002
    Posts
    22
    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
    Yes Mikey, everything is fine that end.
    John

  8. #8
    Join Date
    Nov 2002
    Posts
    30
    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.

  9. #9
    Join Date
    Oct 2002
    Posts
    22
    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.
    Hi,
    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

  10. #10
    Join Date
    Dec 2002
    Posts
    287
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured