求助:代码中服务端的设计思路用尽量文字描述。
// qiu.cpp : 定义控制台应用程序的入口点。//
#include "stdafx.h"
#include <stdio.h>
#include <winsock2.h>
#include <conio.h> //标准IO
#include <windows.h> //调用Sleep()
#include <process.h>
#include <stdlib.h>
#include <tchar.h>
#include "s_help.h"
#pragma comment( lib, "ws2_32.lib" )
#define SOCKET_MAX_CONNECT 10//最大连接数
#define Port 7600
//#define nHOST_IP "172.26.2.48"
unsigned __stdcall ListenThread(LPVOID pParam);//函数声明,没有用类
unsigned __stdcall ClientThread(LPVOID pParam);
char nHOST_IP[20];
int _tmain(int argc, _TCHAR* argv[])
{
LPVOID pParam=NULL; //传递给线程的参数
u_short nTCP_Port= Port; //服务器端口号
pParam =&nTCP_Port;
unsigned dwThreadId;//开启的线程ID
HANDLE hThread = (HANDLE)_beginthreadex( NULL, 0, &ListenThread, pParam, 0, &dwThreadId );
printf("Press ESCAPE to STOP SERVER!!\r\n");
strcpy(nHOST_IP,"172.26.2.48");
GetLocalIP(nHOST_IP);
printf("\n%s\n",nHOST_IP);
while(_getch()!=27);
CloseHandle( hThread );
return 0;
}
unsigned __stdcall ListenThread(LPVOID pParam)
{
//取得参数
int nRetCode;
u_short nTCP_Port;
nTCP_Port= *((u_short*)pParam);
//调用WSAStartup初始化WINSOCK API.
WSADATA wsaData;
WORD wVersionRequested=MAKEWORD(2,2);//WINSOCK版本2.2
nRetCode=WSAStartup(wVersionRequested,&wsaData);
if(nRetCode!=0) //init failure
{
return nRetCode;
}
printf("Starting up TCP server\r\n");
SOCKET listenSocket;
listenSocket=socket(AF_INET,SOCK_STREAM,0);
if(listenSocket==INVALID_SOCKET) //create socket failure
{
return 0;
}
SOCKADDR_IN serverAddr;//绑定到服务器的网卡和端口
serverAddr.sin_family = AF_INET; //Address family
serverAddr.sin_port = htons(nTCP_Port);
serverAddr.sin_addr.s_addr = inet_addr(nHOST_IP);
if (bind(listenSocket, (SOCKADDR*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR)
{
printf("绑定端口失败!\n");
return -1;
}
else
{
printf("绑定端口成功!\n");
}
if (listen(listenSocket, 5) == 0)
{
printf("已启动监听...\n");
}
else
{
printf("监听失败...\n");
return -1;
}
while(true)//一直循环
{
SOCKET * pClientSocket;
SOCKADDR_IN from;
int fromlen=sizeof(from);
//accept()等待连接,程序在此等待
pClientSocket = new SOCKET();
*pClientSocket=accept(listenSocket, (struct sockaddr*)&from,&fromlen);
//为每个连接开一个新线程,把Socket传递给新线程
unsigned dwThreadId;
//HANDLE hThread=CreateThread(NULL,0,ClientThread,(LPVOID)pClientSocket,0,&dwThreadId);
HANDLE hThread = (HANDLE)_beginthreadex( NULL, 0, &ClientThread,(LPVOID)pClientSocket, 0, &dwThreadId );
//在服务器端console输出客户的ip。
printf("Connection from %s \n",inet_ntoa(from.sin_addr));
}
//关闭服务器的socket
closesocket(listenSocket);
//清除winsock
WSACleanup();
_endthreadex(0);
return 0;
}
unsigned __stdcall ClientThread(LPVOID pParam)//与客户交互的线程
{
SOCKET * pClientSocket;//取得socket
pClientSocket = (SOCKET*)pParam;
const int BUF_SIZE = 512;
char buf[BUF_SIZE] = {0};
bool checkAdmin = false;
while(true)
{
//send(*pClientSocket,buf,(int)strlen(buf),0);
//rev
if(checkAdmin == false)
{
int ret = recv(*pClientSocket, buf, sizeof(buf)-1, 0);
if(!strcmp(buf,"123abc"))
{
checkAdmin = true;
send(*pClientSocket, "成功\0", 10, 0);
}
else
send(*pClientSocket, "失败", 10, 0);
}
else
{
dateprocesser(*pClientSocket);
}
}
//在线程里关闭对话,释放资源
closesocket(*pClientSocket);
delete pClientSocket;
_endthreadex(0);
return 0;
}