任务目标
编写Win32程序模拟实现基于WSAAsyncSelect模型的两台计算机之间的通信,要求编程实现服务器端与客户端之间双向数据传递。客户端向服务器端发送“请输出从1到1000内所有的质数”,服务器回应客户端给出结果。
效果图
核心代码
服务器端:
#include
#include
#pragma comment(lib, "WS2_32") // 链接到WS2_32.lib
#include
#define WM_SOCKET WM_USER + 101 // 自定义消息
class CInitSock
{
public:
CInitSock(BYTE minorVer = 2, BYTE majorVer = 2)
{
// 初始化WS2_32.dll
WSADATA wsaData;
WORD sockVersion = MAKEWORD(minorVer, majorVer);
if(::WSAStartup(sockVersion, &wsaData) != 0)
return;
}
~CInitSock()
{
::WSACleanup();
}
};
CInitSock theSock; //加载套接字库
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
bool isprime(int p){//判断p是否为质数
int sq=(int)sqrt(p);
bool flag=true;
int i;
for(i=2;i<=sq;i++){
if(p%i==0){
flag=false;
break;
}
}
if(!flag&&i<=sq)
return false;
else
return true;
};
char * getallprime(int n){ //将num以内的所有质数放在同一字符串中
char szprime[4096] = "质数:" ;
int len=strlen(szprime)+strlen(",");
for(int i=2 ; i <= n ; i++ ){
if(isprime(i)){
char sznum[10];
itoa(i,sznum,10);
char * sztemp=strcat(szprime,sznum);
len+=strlen(sznum);
szprime[len-1]=',';
len+=strlen(",");
}
}
//printf("%s\n",szprime);
return szprime;
};
int main()
{
char szClassName[] = "MainWClass";
WNDCLASSEX wndclass;
// 用描述主窗口的参数填充WNDCLASSEX结构
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc = WindowProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = NULL;
wndclass.hIcon = ::LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = ::LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szClassName ;
wndclass.hIconSm = NULL;
::RegisterClassEx(&wndclass);
// 创建主窗口
HWND hWnd = ::CreateWindowEx(
0,
szClassName,
"",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
NULL,
NULL);
if(hWnd == NULL)
{
::MessageBox(NULL, "创建窗口出错!", "error", MB_OK);
return -1;
}
USHORT nPort = 4567; // 此服务器监听的端口号
// 创建监听套节字
SOCKET sListen = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(nPort);
sin.sin_addr.S_un.S_addr = INADDR_ANY;
// 绑定套节字到本地机器
if(::bind(sListen, (sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR)
{
printf(" Failed bind() \n");
return -1;
}
// 将套接字设为窗口通知消息类型。
::WSAAsyncSelect(sListen, hWnd, WM_SOCKET, FD_ACCEPT|FD_CLOSE);
// 进入监听模式
::listen(sListen, 5);
// 从消息队列中取出消息
MSG msg;
while(::GetMessage(&msg, NULL, 0, 0))
{
// 转化键盘消息
::TranslateMessage(&msg);
// 将消息发送到相应的窗口函数
::DispatchMessage(&msg);
}
// 当GetMessage返回0时程序结束
return msg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_SOCKET:
{
// 取得有事件发生的套节字句柄
SOCKET s = wParam;
// 查看是否出错
if(WSAGETSELECTERROR(lParam))
{
::closesocket(s);
return 0;
}
// 处理发生的事件
switch(WSAGETSELECTEVENT(lParam))
{
case FD_ACCEPT: // 监听中的套接字检测到有连接进入
{
SOCKET client = ::accept(s, NULL, NULL);
::WSAAsyncSelect(client, hWnd, WM_SOCKET, FD_READ|FD_WRITE|FD_CLOSE);
}
break;
case FD_WRITE:
{
}
break;
case FD_READ:
{
char szText[1024] = { 0 };
if(::recv(s, szText, 1024, 0) == -1)
::closesocket(s);
else
{
printf("接收数据:%s", szText);
char * szReply=getallprime(1000); //得到1000以内的所有质数
::send(s, szReply, strlen(szReply), 0); //响应客户端,回以szReply
}
}
break;
case FD_CLOSE:
{
::closesocket(s);
}
break;
}
}
return 0;
case WM_DESTROY:
::PostQuitMessage(0) ;
return 0 ;
}
// 将我们不处理的消息交给系统做默认处理
return ::DefWindowProc(hWnd, uMsg, wParam, lParam);
}
客户端:
#include
#include
#pragma comment(lib, "WS2_32") // 链接到WS2_32.lib
class CInitSock
{
public:
CInitSock(BYTE minorVer = 2, BYTE majorVer = 2)
{
// 初始化WS2_32.dll
WSADATA wsaData;
WORD sockVersion = MAKEWORD(minorVer, majorVer);
if(::WSAStartup(sockVersion, &wsaData) != 0)
return;
}
~CInitSock()
{
::WSACleanup();
}
};
CInitSock theSock; //加载套接字库
int main()
{
// 创建套节字
SOCKET s = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(s == INVALID_SOCKET)
{
printf(" Failed socket() \n");
return 0;
}
// 也可以在这里调用bind函数绑定一个本地地址,无则系统将会自动安排
// 填写远程地址信息
sockaddr_in servAddr;
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(4567);
//要连接的服务器地址
servAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//没有联网,直接使用127.0.0.1即可
if(::connect(s, (sockaddr*)&servAddr, sizeof(servAddr)) == -1)
{
printf(" Failed connect() \n");
return 0;
}
//发送数据
char buf[] = "请输出从1到1000内所有的质数";
printf("发送数据:%s\n",buf);
send(s, buf, strlen(buf), 0);
Sleep(6);
// 接收数据
char buff[3000];
int nRecv = ::recv(s, buff, 3000, 0);
if(nRecv > 0)
{
buff[nRecv] = '\0';
printf("接收到数据:\n%s\n", buff);
}
// 关闭套节字
::closesocket(s);
return 0;
}