本人初学者,水平较低,在测试上位机是否可以接受数据时,出现些问题,下位机发送的数据是ff,上位机接收的情况如图,会出现f0或者其他数据,还有就是数据显示的很慢,并且需要不断抽拔与WiFi模块RX接口相连的线才能有数据显示,不知道是什么原因,程序是在网上找的,请大神们帮看一下,谢谢了。
#include <stdio.h>
#include <Winsock2.h>
#include <string>
#include <process.h>
#include <iostream>
using namespace std;
#pragma comment (lib, "WS2_32.lib")
void main()
{
WORD wVersionRequested;
WSADATA wsaData;//加载套接字库,创建套接字
int err;
wVersionRequested = MAKEWORD( 1,1);
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
WSACleanup( );
return;
}
SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);//TCP 构建通讯socket,流式socket
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr("192.168.1.102");
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(2001);
//connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));//向服务器发送连接请求
if (0 != connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR)))
{
std::cout << "连接服务器失败\n";
}
//send(sockClient,"hello",strlen("hello")+1,0);//和服务器进行通信
char recvBuf[500]={0};
int nLen;
while (1)
{
std::cout<<"C617";
/*recv(sockClient,recvBuf,500,0);0
printf("%s\n",recvBuf);*/
nLen=recv(sockClient,recvBuf,1,0);
std::cout<<"nLen= "<<nLen<<endl;
//std::cout<<"C617"<<std::endl;
//recvBuf[nLen-1] = '\0';
printf("%x\n",recvBuf[0]);
//cout<<recvBuf[2]<<endl;
/*if (nLen>0)
{
recvBuf[nLen-1] = '\0';
printf("%s\n",recvBuf);
}*/
}
closesocket(sockClient);//关闭套接字,关闭加载套接字库
WSACleanup();
system("pause");
}
|