用C写最简单功能的TCP Server收数据,每次打印出来的数据每隔几个就会出现不正常的,不正常的表现就是数据前面多了很多FFFFFFFF,有没有人帮看看这是为什么?附图是数据对比
程序可能出错的部分如下:
SOCKET sClient;
sockaddr_in remoteAddr;
int nAddrlen = sizeof(remoteAddr);
char revData[255];
while (true)
{
printf(“等待连接…\n”);
sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);
if(sClient == INVALID_SOCKET)
{
printf(“accept error !”);
continue;
}
printf(“接受到一个连接:%s \r\n”, inet_ntoa(remoteAddr.sin_addr)); //打印收到的连接信息
//接收数据
ZeroMemory(revData,255);
int ret = recv(sClient, revData, 255, 0);
if(ret>0)
{
for (int i=0;i<ret;i++)
printf(“%.2X “,revData[i]);
}
closesocket(sClient);
}
程序可能出错的部分如下:
SOCKET sClient;
sockaddr_in remoteAddr;
int nAddrlen = sizeof(remoteAddr);
char revData[255];
while (true)
{
printf(“等待连接…\n”);
sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);
if(sClient == INVALID_SOCKET)
{
printf(“accept error !”);
continue;
}
printf(“接受到一个连接:%s \r\n”, inet_ntoa(remoteAddr.sin_addr)); //打印收到的连接信息
//接收数据
ZeroMemory(revData,255);
int ret = recv(sClient, revData, 255, 0);
if(ret>0)
{
for (int i=0;i<ret;i++)
printf(“%.2X “,revData[i]);
}
closesocket(sClient);
}
解决方案
100
printf("%02X ", static_cast<unsigned char>(revData[i]));