Click here to Skip to main content
15,885,546 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Stop removal of null characters Pin
Michael Dunn25-Aug-04 10:52
sitebuilderMichael Dunn25-Aug-04 10:52 
GeneralRe: Stop removal of null characters Pin
Tom Wright25-Aug-04 11:23
Tom Wright25-Aug-04 11:23 
GeneralRe: Stop removal of null characters Pin
Michael Dunn25-Aug-04 12:02
sitebuilderMichael Dunn25-Aug-04 12:02 
GeneralRe: Stop removal of null characters Pin
Tom Wright26-Aug-04 6:14
Tom Wright26-Aug-04 6:14 
GeneralRe: Stop removal of null characters Pin
Joaquín M López Muñoz25-Aug-04 12:45
Joaquín M López Muñoz25-Aug-04 12:45 
GeneralRe: Stop removal of null characters Pin
Tom Wright26-Aug-04 6:00
Tom Wright26-Aug-04 6:00 
GeneralRe: Stop removal of null characters Pin
Abin26-Aug-04 1:21
Abin26-Aug-04 1:21 
GeneralRe: Stop removal of null characters Pin
GKarRacer26-Aug-04 8:05
GKarRacer26-Aug-04 8:05 
Neither the Send function nor the corresponding Receive function strip out any characters from the buffer (nulls or otherwise). The Send function does not care what type of data you are sending. It takes a void pointer as a parameter. The data you send does not even have to be text. I send binary data over a socket all the time.

If it helps think of your final send and receive buffers as a block of miscellaneous bytes. It can be a string, a structure, random bytes, whatever. The length value is the number of bytes in the buffer to send. This is not necessarily the same as the number of characters in the string.

There is no need to replace the NULLs with another character unless that is more convenient for the receiving side.

A couple questions to ask:
What does the spec call for? 53 bytes of 0 followed by a variable length string? Is the string null terminated as well? Must the string be ASCII encoded? Can it be Unicode? This is important because the CString type will change depending on how the program is compiled. So, it's usually better to be more specific by using CStringA or a char array.

Another thing to consider is how much data you plan to send? If there are a lot of Send calls in a row, Send can fail as there is no more room in the internal buffer. Then you have to wait and resend the data. And that may not be all of the data either as Send can send part of your data (whatever fills up the buffer). It will always send bytes in the order passed to it, though.

Any way, yet another simple Send example (assumes ASCII bytes and variable null terminated string):

This version is done with two calls to send. In a low-load app, there won't be much of a performance difference between one call and two calls to send. You can replace the const char pointer with CStringA and lstrlen with GetLength if desired.

int SendStringWithLeadingNulls( CAsyncSocket &SendSocket, const char *Str )
{
    BYTE   NullBuffer[53] = {'\0'};
    int    BytesSent;

    BytesSent = SendSocket.Send( NullBuffer, sizeof(NullBuffer) );
    if( BytesSent < sizeof(NullBuffer) )
        return(BytesSent);
    return( SendSocket.Send( Str, lstrlen(Str)+1 ) );
}


When receiving, examine the buffer as a byte array and not a string and you will see the leading bytes.

BYTE    Buffer[1024];
int     BytesReceived;
CString Str;

BytesReceived = ReceiveSocket.Receive( Buffer, sizeof(Buffer) );
if( BytesReceived > 53 )
    Str = (char *) (Buffer + 53);

GeneralWow this is a mess Pin
palbano25-Aug-04 18:55
palbano25-Aug-04 18:55 
GeneralRe: Wow this is a mess Pin
Tom Wright26-Aug-04 5:54
Tom Wright26-Aug-04 5:54 
GeneralRe: Wow this is a mess Pin
palbano26-Aug-04 7:59
palbano26-Aug-04 7:59 
GeneralWindow Position Pin
Anthony988725-Aug-04 8:54
Anthony988725-Aug-04 8:54 
GeneralRe: Window Position Pin
Maximilien25-Aug-04 9:00
Maximilien25-Aug-04 9:00 
GeneralRe: Window Position Pin
Anthony988725-Aug-04 9:07
Anthony988725-Aug-04 9:07 
QuestionSaving password to registry? Pin
mcguile25725-Aug-04 8:16
mcguile25725-Aug-04 8:16 
AnswerRe: Saving password to registry? Pin
Antony M Kancidrowski25-Aug-04 9:48
Antony M Kancidrowski25-Aug-04 9:48 
AnswerRe: Saving password to registry? Pin
David Crow25-Aug-04 10:19
David Crow25-Aug-04 10:19 
GeneralRe: Saving password to registry? Pin
darkbyte25-Aug-04 10:36
darkbyte25-Aug-04 10:36 
GeneralRe: Saving password to registry? Pin
David Crow25-Aug-04 10:52
David Crow25-Aug-04 10:52 
GeneralRe: Saving password to registry? Pin
darkbyte26-Aug-04 5:00
darkbyte26-Aug-04 5:00 
AnswerRe: Saving password to registry? Pin
cmk25-Aug-04 13:22
cmk25-Aug-04 13:22 
AnswerRe: Saving password to registry? Pin
l a u r e n25-Aug-04 15:45
l a u r e n25-Aug-04 15:45 
GeneralRe: Saving password to registry? Pin
ThatsAlok25-Aug-04 21:53
ThatsAlok25-Aug-04 21:53 
GeneralRe: Saving password to registry? Pin
darkbyte26-Aug-04 4:46
darkbyte26-Aug-04 4:46 
AnswerRe: Saving password to registry? Pin
JimmyRopes26-Aug-04 9:33
professionalJimmyRopes26-Aug-04 9:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.