开发者

My buffer contains elements, but aren't being printed

Sorry scratch my last post, it's way to late =S But basically I'm having problems sending out the buffer I created. Just need to know where I'm going wrong =( or if theres a better way.

------ Client Sending Username -------

int bufferSize = 32;


char messageBuffer[bufferSize];


char* message;


if (userName.size() > 8)
{
    cout << "Invalid username : Greater than 8 characters" << endl;
}
else
{
    switch(regType)
    {
        case(REGISTER):
        {
            cout << "Registered name: " << userName << endl;
            messageBuffer[0] = 1;
            messageBuffer[1] = 0;
            for (int i = 2; i < (userName.size() + 2); i++)
            {
                messageBuffer[i] = userName[(i - 2)];
            }

            for (int i = 0; i < (userName.size() + 2); i++)
            {
                cout << messageBuffer[i];
            }
            cout << "<<<< messageBuffer " << endl;

            message = &messageBuffer[0];
            cout << messageBuffer << endl;
            //message[(userName.size() + 2)] = '\0';
            int messageLen = userName.size() + 2;
            if (send(sock, messageBuffer, messageLen, 0) != messageLen)
                DieWithError("send() send an invalid name");



        }break;
        case(UNREGISTER):
        {
        }break;
    }

}

----------- Server (Receiver)------------ char msgRcvBuffer[RCVBUFSIZE];

int recvMsgSize;

if ((recvMsgSize = recv(clntSocket, msgRcvBuffer, RCVBUFSIZE, 0)) < 0) DieWithError("recv() failed");

msgRcvBuffer[recvMsgSize] = '\0';

string msgType( msgRcvBuffer );

cout << "Message Type " << msgType << endl; <<<<<< Nothing appears w开发者_如何学运维hen printed

void handleReg(string message, int socket, string ipAddr) {

// Remove the Prefix
int startIndex = 2;
// Get the username from the message

string userName = message.substr(startIndex, message.size() - startIndex);
cout << "Username " << userName << endl;

For some reason my message string is just 1... =S What i'm trying to do is just get the message from what was sent from client. I'm just tryin to remove the '1' and '0' from the beginning of the buffer. 1 and 0 aren't characters.

Thanks so much for everyones help =)


The conversion from char* to string treats the string as null-terminated. This doesn’t seem to be the case here – in particular, your char array appears to contain 0 characters, so the string will be cut off at this position.

To circumvent this, you need to pass the valid range of characters to the string constructor, in place of only the start pointer:

msgType = string(msgRcvBuffer, msgRcvBuffer + recvMsgSize);


If msgRcvBuffer is of size RCVBUFSIZE then msgRcvBuffer[recvMsgSize] = '\0'; is going to be writing beyond the end of the buffer I think.


Use the std::string constructor that takes a buffer and buffer size parameter:

msgType = std::string(msgRcvBuffer, recvMsgSize);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜