//服务端 server.cpp
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<winsock2.h>
#include<windows.h>
class CServer
{
private:
int port;
public:
CServer();
virtual ~CServer();
f();
};
CServer::CServer(){
port=5150;
}
CServer::~CServer(){}
CServer::f(){
int err;int p;
WORD wVer;
WSADATA wsaData;
wVer=MAKEWORD(1,1);
err=WSAStartup(wVer,&wsaData);//判断Windows sockets dll版本
if(err!=0)
return(0);
SOCKET sListen,sAccept;
struct sockaddr_in tcpaddr;
int port=5150;
// 取得本地地址
sListen=WSASocket(AF_INET,SOCK_STREAM,0,NULL,0,WSA_FLAG_OVERLAPPED);
if(sListen==INVALID_SOCKET)
{printf("错误:Failed to get a socket%d\n",WSAGetLastError());
WSACleanup();
return 0;
}
tcpaddr.sin_family=AF_INET;
tcpaddr.sin_port=htons(port);
tcpaddr.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(sListen,(PSOCKADDR)&tcpaddr,sizeof(tcpaddr))==SOCKET_ERROR)
{printf("错误:bind() failed with error %d\n", WSAGetLastError());
return 0;
}
//服务端监听
if (listen(sListen, SOMAXCONN))
{printf("错误:listen() failed with error %d\n", WSAGetLastError());
return 0;
}
printf("Ftpserver已经启动 \n");
printf("Ftpserver开始侦听 \n");
//接收客户端连接
if ((sAccept = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0,
WSA_FLAG_OVERLAPPED)) == INVALID_SOCKET)
{
printf("错误:Failed to get a socket %d\n", WSAGetLastError());
return 0;
}
}
void main(){
CServer s;
s.f();
}