#include <iostream>
#include <WinSock2.h>
#include <Windows.h>
using namespace std;
SOCKET listener,connector;
SOCKADDR_IN myAddress,remoteAddress;
int main()
{
初始化网络窗口
WSAData wdata;
WSAStartup(MAKEWORD(2,2),&wdata);
初始化监听器
ZeroMemory(&listener,sizeof(listener));
ZeroMemory(&connector,sizeof(connector));
listener = socket(AF_INET,SOCK_STREAM,0);
为 struct SOCKADDR_IN myAddress 赋值
myAddress.sin_addr.s_addr = INADDR_ANY; //accept any ip
myAddress.sin_family = AF_INET;//tcp
myAddress.sin_port = htons(7000);//changing 7000 port to string with htons method
绑定侦听器套接字和地址以指示侦听器在哪里查找
if(bind(listener,(LPSOCKADDR)&myAddress,sizeof(listener)))
{
cout<< "bound";
}
//listen method
if(listen(listener,1));
{
cout<<"Waiting";
}
int Size = sizeof(remoteAddress);
初始化无限循环以继续收听
while(true)
{
connector = accept(listener,(LPSOCKADDR)&remoteAddress,&Size);
//read connection request
if(sizeof(connector)< 0)
{
cout <<"noinfo\n";
}
}
}
即使没有连接客户端,代码也不会显示“无信息”:O 我不知道我做错了:(请帮助我