初级会员
- 积分
- 99
- 金钱
- 99
- 注册时间
- 2017-4-1
- 在线时间
- 16 小时
|
各位大神,我在电脑上写了一个简单的服务器程序,目的是获得单片机的数据
但连接不到WiFi模块,老是显示等待客户机连接,求指点
服务器程序如下:
/*阻塞式套接字服务器编程*/
#include <winsock2.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
int main()
{
WSADATA wsaData;
if(WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
{
std::cout<<"WinSock服务初始化失败"<<std::endl;
WSACleanup();
system("PAUSE");
return 0;
}
SOCKET socket1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(socket1 == INVALID_SOCKET)
{
std::cout<<"WinSock服务初始化失败"<<std::endl;
WSACleanup();
system("PAUSE");
return 0;
}
SOCKADDR_IN serverInf;
serverInf.sin_port = htons(12500);
serverInf.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
serverInf.sin_family = AF_INET;
if(bind(socket1, (SOCKADDR*)&serverInf, sizeof(serverInf)) == SOCKET_ERROR)
{
std::cout<<"不能绑定地址信息到套接字"<<std::endl;
WSACleanup();
system("PAUSE");
return 0;
}
listen(socket1,1);
SOCKET TempSock = SOCKET_ERROR;
while(TempSock == SOCKET_ERROR)
{
std::cout<<"服务器:正在等待来自客户机的连接...\n"<<std::endl;
TempSock = accept(socket1, NULL, NULL);
}
socket1 = TempSock;
std::cout<<"服务器:有客户机连接到达"<<std::endl;
char recvBuf[100];
recv(socket1, recvBuf, 100, 0);
std::cout<<"%s\n"<<recvBuf<<std::endl;
closesocket(socket1);
WSACleanup();
system("PAUSE");
return 0;
}
结果:
|
|