【发布时间】:2018-01-25 13:49:55
【问题描述】:
我正在尝试使用 TCP 套接字在 C++ 中进行多客户端聊天。 我已经从this site下载了socket实现的源代码。
问题是当我尝试从客户端向服务器发送消息时, 我从服务器收到的“ecko”是无穷无尽的空格字符串。
我尝试调试客户端代码,客户端正确读取输入。 在前几条消息中,服务器将他的消息发送回客户端, 但经过几条消息后,客户端会返回无尽的空间。 我尝试使用 memset 进行无效化(在所有数组中置零),但更糟糕的是,服务器根本不接收消息。
不胜感激(:
这是服务器端:
#include "PracticalSocket.h"
#include <stdio.h>
#include <process.h>
#include <Windows.h>
using namespace std;
#pragma comment(lib,"ws2_32.lib")
TCPSocket* MyClients[20];
int ClientCount = 0;
void connectCLient(void* pValue){
int nI,Flag;
char st[1024];
//memset(st,0,1024); // doing problems
TCPSocket* pServerClient = (TCPSocket*)pValue;
MyClients[ClientCount] = pServerClient;
ClientCount++;
try{
while (true)
{
Flag = pServerClient->recv(st,strlen(st));
if(Flag>1){
printf("%s\n",st);
for(nI = 0; nI< ClientCount ; nI++){
MyClients[nI]->send(st,strlen(st)+1);
}
}
}
}
catch(...){
puts("one client lefttt");
}
}
int main(int argc, char* argv[])
{
TCPServerSocket* pServer = new TCPServerSocket(8546);
int nClientCounter = 0;
printf("Start TCP Server ... on Port %d\n", 8546);
try{
while(true)
{
printf("Wait for new TCP Clients ... \n");
TCPSocket* pClient = pServer->accept();
_beginthread(connectCLient,0,(void*)pClient);
printf("Client %d Connected ... \n", ++nClientCounter);
}
}
catch(...){
puts("one client left");
}
return 0;
}
这是客户端:
#include "PracticalSocket.h"
#include <stdio.h>
#include <process.h>
#include <Windows.h>
using namespace std;
#pragma comment (lib, "ws2_32.lib")
void ReciveMessages(void * pValue ){
char recvM[1024];
TCPSocket* pClient = (TCPSocket*)pValue;
while(true){
pClient->recv(recvM,strlen(recvM));
printf("%s\n",recvM);
}
}
int main(int argc, char* argv[])
{
try
{
TCPSocket * cClient = new TCPSocket();
cClient->connect("127.0.0.1",8546);
_beginthread(ReciveMessages,0,(void*)cClient);
char st[1024];
memset(st,0,1024);
while(true)
{
printf("Press Text -->");
fgets(st, sizeof st, stdin);
cClient->send(st,strlen(st)+2);
}
}
catch(...)
{
printf("Socket Error..!");
system("pause");//run cmd comment - stop the system
}
return 0;
}
【问题讨论】:
-
recv(st,strlen(st));字符串可能有多长? -
字符串的大小加1,(最后是0)
-
strlen给出字符串的当前大小,而不是最大大小。recv也没有添加换行符或空终止符。