抱歉,我的上一篇文章已经过时了,太晚了 =S 但基本上我在发送我创建的缓冲区时遇到了问题。只需要知道我哪里出错了=(或者是否有更好的方法。
------ 客户端发送用户名 --------
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;
}
}
------------ 服务器(接收方)------------ char msgRcvBuffer[RCVBUFSIZE];
int recvMsgSize;
if ((recvMsgSize = recv(clntSocket, msgRcvBuffer, RCVBUFSIZE, 0)) < 0) DieWithError("recv() failed");
msgRcvBuffer[recvMsgSize] = '\0';
字符串 msgType(msgRcvBuffer);
cout << "消息类型" << msgType << endl; <<<<<< 打印时什么都不显示
void handleReg(字符串消息,int 套接字,字符串 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;
出于某种原因,我的消息字符串只是 1... =S 我想要做的只是从客户端发送的消息中获取消息。我只是想从缓冲区的开头删除“1”和“0”。1 和 0 不是字符。
非常感谢大家的帮助 =)