📄 server.cpp
字号:
#pragma comment(lib,"ws2_32")
#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
#include <stdio.h>
#include "time.h"
#include "winsock2.h"
#include "ws2tcpip.h"
#define udpport 1200
int main()
{
SOCKET tserver;
SOCKET userver;
//WSADATA is a struct that is filled up by the call
//to WSAStartup
//这是一个结构体,用于调用函数WSAStartup时作为参数
WSADATA wsaData;
//The sockaddr_in specifies the address of the socket
//for TCP/IP sockets. Other protocols use similar structures.
//存放ip地址的结构体
sockaddr_in tlocal;
sockaddr_in ulocal;
//WSAStartup initializes the program for calling WinSock.
//The first parameter specifies the highest version of the
//WinSock specification, the program is allowed to use.
memset(&tlocal,0x0,sizeof(struct sockaddr_in *));
memset(&ulocal,0x0,sizeof(struct sockaddr_in *));
//初始化winsock,每次必须最先调用
int wsaret=WSAStartup(0x101,&wsaData);
//WSAStartup returns zero on success.
//If it fails we exit.
//测试初始化是否成功
if(wsaret!=0)
{
cout<<"WSAStartup failed!\n";
return 0;
}
//Now we populate the sockaddr_in structure
//填充服务器的ip地址和端口号
tlocal.sin_family=AF_INET; //Address family
tlocal.sin_addr.s_addr=INADDR_ANY; //Wild card IP address
tlocal.sin_port=htons(8007); //port to use geiding%%%%%%%%%%%%%*argv[1]
ulocal.sin_family=AF_INET; //Address family
ulocal.sin_addr.s_addr=INADDR_ANY; //Wild card IP address
ulocal.sin_port=htons((u_short)udpport); //port to use
//the socket function creates our SOCKET
//建立一个tcp socket
tserver=socket(AF_INET,SOCK_STREAM,0);
userver=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
//If the socket() function fails we exit
if(tserver==INVALID_SOCKET)
{
cout<<"creat tcpsocket failed!\n";
return 0;
}
if(userver==INVALID_SOCKET)
{
cout<<"creat udpsocket failed!\n";
return 0;
}
//bind links the socket we just created with the sockaddr_in
//structure. Basically it connects the socket with
//the local address and a specified port.
//If it returns non-zero quit, as this indicates error
//绑定服务器的ip地址和端口号
if(bind(tserver,(sockaddr*)&tlocal,sizeof(tlocal))!=0)
{
cout<<"bind tcpsocket failed!\n";
return 0;
}
if(bind(userver,(sockaddr*)&ulocal,sizeof(ulocal))!=0)
{
cout<<"bind udpsocket failed!\n";
return 0;
}
u_long mode=1;
ioctlsocket(userver,FIONBIO,(unsigned long*)&mode);//设置Userver为Nonblocking模式
ioctlsocket(tserver,FIONBIO,(unsigned long*)&mode);
//we will need variables to hold the client socket.
//thus we declare them here.
//接受请求后,实际同客户端socket进行交互的SOCKET client
SOCKET client;
sockaddr_in tfrom;
sockaddr_in ufrom;
memset(&tfrom,0x0,sizeof(struct sockaddr_in *));
memset(&ufrom,0x0,sizeof(struct sockaddr_in *));
int tfromlen=sizeof(tfrom);
int ufromlen=sizeof(ufrom);
//struct tm *local;
//time_t t;
//char hour,min,second;
char temp[100],time[6];
char tsend[2];
char recvbuffer[100];
int recvlen;
static char st1[30]="GET UDP PORT";
static char st2[30]="GET CUR TIME";
tsend[0]='a';//(unsigned char)udpport>>8;
tsend[1]='b';
//(unsigned char)udpport&0x00ff;
while(true)//we are looping endlessly
{
//listen instructs the socket to listen for incoming
//connections from clients. The second arg is the backlog
//监听客户端请求,最大同时连接数10
if(listen(tserver,10)!=0)
{
cout<<"listen failed!\n";
return 0;
}
//accept() will accept an incoming
//client connection
//接受一个连接请求,并返回一个同客户端交互的socket给变量client
client=accept(tserver,(struct sockaddr*)&tfrom,&tfromlen);
if(client==SOCKET_ERROR)
continue;//QQQQQQQQQQQQQQ{cout<<"accept failed!\n";return 0;}
else break;}
cout<<"Connection from TCP client"<<inet_ntoa(tfrom.sin_addr)<<":"<<htons(tfrom.sin_port)<<"accepted!\n";
//t=time(NUL);
//local=localtime(&t);
//hour=(unsigned char)(local->tm_hour+'0');
//min=(unsigned char)(local->tm_min+'0');
//second=(unsigned char)(local->tm_sec+'0');
time[0]='a';
time[1]=':';
time[2]='b';
time[3]=':';
time[4]='3';
int k,l;
for(k=0;k<10;k++)
{for(l=0;l<10;l++){if(recv(client,temp,sizeof(temp),0)!=SOCKET_ERROR)
{cout<<temp;
l=k=80;}
}}
if(k==10)
cout<<WSAGetLastError();
if(strcmp(st1,temp)==0)
{if(send(client,tsend,sizeof(tsend),0)==SOCKET_ERROR)
{cout<<"send fail"<<WSAGetLastError();
return 0;}
cout<<"send1!!!!!!"<<tsend;}//udpprot%%%%%%%%%%%%%%%%
//if(strcmp(st2,temp)==0)
//{if(send(client,time,sizeof(time),0)==SOCKET_ERROR)
//{cout<<"send fail"<<WSAGetLastError();
//return 0;}
//cout<<"send2!!!!!!";}}
//cout<<time;
if(recvlen=recvfrom(userver,recvbuffer,100,0,(struct sockaddr*)&ufrom,&ufromlen)==SOCKET_ERROR)
//cout<<recvbuffer<<"\n";
return 0;
if(recvlen)
{
if(sendto(userver,recvbuffer,recvlen,0,(struct sockaddr*)&ufrom,sizeof(ufrom))!=recvlen)
{cout<<"sendto failed!";
closesocket(userver);
}}
//close the client socket
//关闭连接
closesocket(client);
//closesocket() closes the socket and releases the socket descriptor
//释放socket资源
closesocket(tserver);
closesocket(userver);
//originally this function probably had some use
//currently this is just for backward compatibility
//but it is safer to call it as I still believe some
//implementations use this to terminate use of WS2_32.DLL
//必须作的最后的事
WSACleanup();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -