紧急求救:一段c++代码,为什么我的SOCKET 客户端接收不到服务器返回的信息 100
WSADATAwsaData;//用于填充套接字库版本的有关信息SOCKETServerSocket=NULL;//服务器套接字(用于同服务器IPv4地址绑定)SOCKA...
WSADATA wsaData; //用于填充套接字库版本的有关信息
SOCKET ServerSocket = NULL;//服务器套接字(用于同服务器IPv4地址绑定)
SOCKADDR_IN ServerSocketAddr; //服务器的IPv4地址
SOCKET NewConnection = NULL; //
int port = 9999; //要连接服务器的9999端口
char buf[1024]=""; //存储消息用
int bufLen = 0; //返回的消息长度
char Serverip[12];
printf("please input the server computer ip: ");
scanf("%s", Serverip);
printf("please input the message you want to send: ");
scanf("%s", buf);
//加载Winsock 2.2版本
if(WSAStartup(MAKEWORD(2,2),&wsaData) !=0)
{
printf("WSAStartup failed");
return 1;
}
//创建套接字
if((ServerSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) == INVALID_SOCKET)
printf("Create ServerSocket failed with error");
//填写服务器IPv4信息
ServerSocketAddr.sin_family = AF_INET;
ServerSocketAddr.sin_port = htons(port);
ServerSocketAddr.sin_addr.s_addr = inet_addr(Serverip);
//连接服务器
if(( NewConnection = connect(ServerSocket,(SOCKADDR*)&ServerSocketAddr,sizeof(ServerSocketAddr))) ==SOCKET_ERROR)
printf("Connecting failed with error");
else
printf("Connect successing!");
//发送数据
if((bufLen = send(ServerSocket,buf,strlen(buf),0)) == INVALID_SOCKET)
printf("Send data failed with error");
else
printf("Send MessageLen byte datas");
//接收数据
if((bufLen = recv(ServerSocket,buf,sizeof(buf),0))=INVALID_SOCKET)
printf("receive failed with error");
else
printf(buf);
closesocket(ServerSocket);
WSACleanup();
return 0;
我的客户端能发送出去,服务端已能接受和发送信息,但客户端就是接收不到,帮我看哈。
//接收数据
if((bufLen = recv(ServerSocket,buf,sizeof(buf),0))=INVALID_SOCKET)
printf("receive failed with error");
else
printf(buf);
就是这段代码有问题,看下该怎么写............ 展开
SOCKET ServerSocket = NULL;//服务器套接字(用于同服务器IPv4地址绑定)
SOCKADDR_IN ServerSocketAddr; //服务器的IPv4地址
SOCKET NewConnection = NULL; //
int port = 9999; //要连接服务器的9999端口
char buf[1024]=""; //存储消息用
int bufLen = 0; //返回的消息长度
char Serverip[12];
printf("please input the server computer ip: ");
scanf("%s", Serverip);
printf("please input the message you want to send: ");
scanf("%s", buf);
//加载Winsock 2.2版本
if(WSAStartup(MAKEWORD(2,2),&wsaData) !=0)
{
printf("WSAStartup failed");
return 1;
}
//创建套接字
if((ServerSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) == INVALID_SOCKET)
printf("Create ServerSocket failed with error");
//填写服务器IPv4信息
ServerSocketAddr.sin_family = AF_INET;
ServerSocketAddr.sin_port = htons(port);
ServerSocketAddr.sin_addr.s_addr = inet_addr(Serverip);
//连接服务器
if(( NewConnection = connect(ServerSocket,(SOCKADDR*)&ServerSocketAddr,sizeof(ServerSocketAddr))) ==SOCKET_ERROR)
printf("Connecting failed with error");
else
printf("Connect successing!");
//发送数据
if((bufLen = send(ServerSocket,buf,strlen(buf),0)) == INVALID_SOCKET)
printf("Send data failed with error");
else
printf("Send MessageLen byte datas");
//接收数据
if((bufLen = recv(ServerSocket,buf,sizeof(buf),0))=INVALID_SOCKET)
printf("receive failed with error");
else
printf(buf);
closesocket(ServerSocket);
WSACleanup();
return 0;
我的客户端能发送出去,服务端已能接受和发送信息,但客户端就是接收不到,帮我看哈。
//接收数据
if((bufLen = recv(ServerSocket,buf,sizeof(buf),0))=INVALID_SOCKET)
printf("receive failed with error");
else
printf(buf);
就是这段代码有问题,看下该怎么写............ 展开
2个回答
展开全部
//接收数据
if((bufLen = recv(ServerSocket,buf,sizeof(buf),0))=INVALID_SOCKET)
printf("receive failed with error");
else
printf(buf);
第一行第二个"="换成"=="
if((bufLen = recv(ServerSocket,buf,sizeof(buf),0))=INVALID_SOCKET)
printf("receive failed with error");
else
printf(buf);
第一行第二个"="换成"=="
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询