实验一:基于Winsock完成简单的网络程序开发
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验一:基于Winsock完成简单的网络程序开发相关的知识,希望对你有一定的参考价值。
第一部分:简答的UDP网络通信程序
1 // UDP5555.cpp : Defines the entry point for the application. 2 //========================================================================= 3 //陈家琪,计算机网络课程,传输层与应用层协议-用户数据报协议UDP 4 //说明:在窗口里点击鼠标左键,则发送信息到远程机。 5 //========================================================================= 6 //VC 6.0 add WSOCK32.LIB in Project -> Settings... -> Link 7 #include <winsock.h> 8 //-------------------------------------------------------------- 9 // Global Variables: 10 HINSTANCE hInst; // 实例句柄,方便各程序访问 11 HWND hWnd; // 主窗口句柄,方便各程序访问 12 char Title[]="UDP[5555]"; // 窗口标题 The title bar text 13 int WinX0=200,WinY0=100,WinW=200,WinH=500; // 窗口位置、大小 14 15 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 16 //-------------------------------------------------------------- 17 int APIENTRY WinMain(HINSTANCE hI,HINSTANCE hP,LPSTR lp,int nC) 18 { 19 MSG msg; 20 hInst = hI; // Store instance handle in our global variable 21 WNDCLASS wc; 22 23 memset(&wc,0,sizeof(WNDCLASS)); 24 25 wc.lpfnWndProc = (WNDPROC)WndProc; 26 wc.hInstance = hI; 27 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); 28 wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1); 29 wc.lpszClassName = "Win32API"; 30 RegisterClass(&wc); 31 //特殊窗口1,始终在顶层,任务栏显示。 32 hWnd=CreateWindowEx(WS_EX_TOPMOST,"Win32API",Title, 33 WS_DLGFRAME|WS_SYSMENU, 34 WinX0,WinY0,WinW,WinH, // 窗口位置、大小 35 NULL, NULL, hI, NULL); 36 37 if (!hWnd) return FALSE; 38 39 ShowWindow(hWnd, nC); 40 // Main message loop: 41 while (GetMessage(&msg, NULL, 0, 0)) 42 { 43 TranslateMessage(&msg); 44 DispatchMessage(&msg); 45 } 46 return msg.wParam; 47 } 48 //-------------------------------------------------------------- 49 WSADATA ws; 50 SOCKET Cs1,Cs2; 51 struct sockaddr_in Cs1A,Cs2A; 52 char aa[200]; 53 char bb[200]; 54 int d; 55 #define Cs1Port 5555 //本地端口 56 #define Cs2Port 6666 //远程端口 57 #define Cs1IP "127.0.0.1" //本地网址 58 #define Cs2IP "127.0.0.1" //远程网址 59 //-------------------------------------------------------------- 60 //消息处理 61 LRESULT CALLBACK WndProc(HWND hW, UINT msg, WPARAM wP, LPARAM lP) 62 { 63 switch (msg) 64 { 65 case WM_DESTROY: //关闭程序触发事件 66 WSAAsyncSelect(Cs1, hW, 0, 0); //向Windows注销Socket触发事件 67 closesocket(Cs1); //释放连接 68 WSACleanup( ); //卸载动态连接库WinSock DLL 69 PostQuitMessage(0); 70 break; 71 case WM_CREATE: //程序初始化触发事件 72 WSAStartup(0x0101,&ws); //装载动态连接库WinSock DLL 73 Cs1=socket(AF_INET, SOCK_DGRAM,0); //创建UDP套接字Socket 74 Cs1A.sin_family=AF_INET; //地址类型:AF_INET for Internet 75 Cs1A.sin_port = htons(Cs1Port); //本地Socket的属性:端口 76 Cs1A.sin_addr.s_addr =inet_addr(Cs1IP); //本地Socket的属性:IP地址 77 d=bind(Cs1,(struct sockaddr *) &Cs1A,sizeof(Cs1A)); //设置本地socket的属性,捆绑 78 Cs2A.sin_family=AF_INET; //地址类型:AF_INET for Internet 79 Cs2A.sin_port = htons(Cs2Port); //远程Socket的属性:端口 80 Cs2A.sin_addr.s_addr =inet_addr(Cs2IP); //远程Socket的属性:IP地址 81 WSAAsyncSelect(Cs1,hW,WM_USER+1,FD_READ); //向Windows注册Socket触发事件 82 Beep(2000,100); 83 break; 84 case WM_USER+1: //Socket触发事件 85 switch(LOWORD(lP)) 86 { 87 case FD_READ: //收到远程信息 88 // d=recvfrom(Cs1,bb,sizeof(bb),0,(struct sockaddr *) &Cs2A,&d); 89 d=recv(wP,bb,sizeof(bb),0);//wP=Cs //读接收缓冲区 90 bb[d]=0; 91 MessageBox(0,bb,"UDP[5555](FD_READ)",0); 92 break; 93 } 94 break; 95 case WM_LBUTTONDOWN: //点击鼠标左键 96 wsprintf(aa,"UDP[5555]:Hello!"); 97 d = sizeof(Cs2A); 98 sendto(Cs1,aa,strlen(aa),0,(struct sockaddr *) &Cs2A,d); //发送信息"UDP[5555]:Hello!" 99 break; 100 } 101 return DefWindowProc(hW,msg,wP,lP); 102 } 103 //--------------------------------------------------------------
1 // UDP6666.cpp : Defines the entry point for the application. 2 //========================================================================= 3 //用户数据报协议UDP 4 //说明:在窗口里点击鼠标左键,则发送信息到远程机。 5 //========================================================================= 6 //VC 6.0 add WSOCK32.LIB in Project -> Settings... -> Link 7 #include <winsock.h> 8 //-------------------------------------------------------------- 9 // Global Variables: 10 HINSTANCE hInst; // 实例句柄,方便各程序访问 11 HWND hWnd; // 主窗口句柄,方便各程序访问 12 char Title[]="UDP[6666]"; // 窗口标题 The title bar text 13 int WinX0=500,WinY0=100,WinW=200,WinH=500; // 窗口位置、大小 14 15 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 16 //-------------------------------------------------------------- 17 int APIENTRY WinMain(HINSTANCE hI,HINSTANCE hP,LPSTR lp,int nC) 18 { 19 MSG msg; 20 hInst = hI; // Store instance handle in our global variable 21 WNDCLASS wc; 22 23 memset(&wc,0,sizeof(WNDCLASS)); 24 25 wc.lpfnWndProc = (WNDPROC)WndProc; 26 wc.hInstance = hI; 27 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); 28 wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1); 29 wc.lpszClassName = "Win32API"; 30 RegisterClass(&wc); 31 //特殊窗口1,始终在顶层,任务栏显示。 32 hWnd=CreateWindowEx(WS_EX_TOPMOST,"Win32API",Title, 33 WS_DLGFRAME|WS_SYSMENU, 34 WinX0,WinY0,WinW,WinH, // 窗口位置、大小 35 NULL, NULL, hI, NULL); 36 37 if (!hWnd) return FALSE; 38 39 ShowWindow(hWnd, nC); 40 // Main message loop: 41 while (GetMessage(&msg, NULL, 0, 0)) 42 { 43 TranslateMessage(&msg); 44 DispatchMessage(&msg); 45 } 46 return msg.wParam; 47 } 48 //-------------------------------------------------------------- 49 WSADATA ws; 50 SOCKET Cs1,Cs2; 51 struct sockaddr_in Cs1A,Cs2A; 52 char aa[200]; 53 char bb[200]; 54 int d; 55 #define Cs1Port 6666 //本地端口 56 #define Cs2Port 5555 //远程端口 57 #define Cs1IP "127.0.0.1" //本地网址 58 #define Cs2IP "127.0.0.1" //远程网址 59 //-------------------------------------------------------------- 60 //消息处理 61 LRESULT CALLBACK WndProc(HWND hW, UINT msg, WPARAM wP, LPARAM lP) 62 { 63 switch (msg) 64 { 65 case WM_DESTROY: //关闭程序触发事件 66 WSAAsyncSelect(Cs1, hW, 0, 0); //向Windows注销Socket触发事件 67 closesocket(Cs1); //释放连接 68 WSACleanup( ); //卸载动态连接库WinSock DLL 69 PostQuitMessage(0); 70 break; 71 case WM_CREATE: //程序初始化触发事件 72 WSAStartup(0x0101,&ws); //装载动态连接库WinSock DLL 73 Cs1=socket(AF_INET, SOCK_DGRAM,0); //创建UDP套接字Socket 74 Cs1A.sin_family=AF_INET; //地址类型:AF_INET for Internet 75 Cs1A.sin_port = htons(Cs1Port); //本地Socket的属性:端口 76 Cs1A.sin_addr.s_addr =inet_addr(Cs1IP); //本地Socket的属性:IP地址 77 d=bind(Cs1,(struct sockaddr *) &Cs1A,sizeof(Cs1A)); //设置本地socket的属性,捆绑 78 Cs2A.sin_family=AF_INET; //地址类型:AF_INET for Internet 79 Cs2A.sin_port = htons(Cs2Port); //远程Socket的属性:端口 80 Cs2A.sin_addr.s_addr =inet_addr(Cs2IP); //远程Socket的属性:IP地址 81 WSAAsyncSelect(Cs1,hW,WM_USER+1,FD_READ); //向Windows注册Socket触发事件 82 Beep(2000,100); 83 break; 84 case WM_USER+1: //Socket触发事件 85 switch(LOWORD(lP)) 86 { 87 case FD_READ: //收到远程信息 88 // d=recvfrom(Cs1,bb,sizeof(bb),0,(struct sockaddr *) &Cs2A,&d); 89 d=recv(wP,bb,sizeof(bb),0);//wP=Cs //读接收缓冲区 90 bb[d]=0; 91 MessageBox(0,bb,"UDP[6666](FD_READ)",0); 92 break; 93 } 94 break; 95 case WM_LBUTTONDOWN: //点击鼠标左键 96 wsprintf(aa,"UDP[6666]:Hello!"); 97 d = sizeof(Cs2A); 98 sendto(Cs1,aa,strlen(aa),0,(struct sockaddr *) &Cs2A,d); //发送信息"UDP[6666]:Hello!" 99 break; 100 } 101 return DefWindowProc(hW,msg,wP,lP); 102 } 103 //--------------------------------------------------------------
第二部分:简单的TCP网络通信程序
1 //Server.cpp : Defines the entry point for the application. use VC 6.0 2 //========================================================================= 3 //说明:监听客户机连接请求, 向客户机发送连接应答"Server:Welcome!"; 4 //说明:接收客户机服务请求, 向客户机发送服务数据. 5 //========================================================================= 6 //VC6.0 add WSOCK32.LIB in Project->Settings...->Link 7 #include <winsock.h> //by user 8 //------------------------------------------------ 9 // Global Variables: 10 HINSTANCE hInst; // 实例句柄,方便各程序访问 11 HWND hWnd; // 主窗口句柄,方便各程序访问 12 char Title[]=" TCP-Server "; // 窗口标题 The title bar text 13 int WinX0=500,WinY0=100,WinW=300,WinH=50; // 窗口位置、大小 14 15 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 16 //------------------------------------------------ 17 int APIENTRY WinMain(HINSTANCE hI,HINSTANCE hP,LPSTR lp,int nC) 18 { 19 MSG msg; 20 hInst = hI; // Store instance handle in our global variable 21 WNDCLASS wc; 22 23 memset(&wc,0,sizeof(WNDCLASS)); 24 25 wc.lpfnWndProc = (WNDPROC)WndProc; 26 wc.hInstance = hI; 27 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); 28 wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1); 29 wc.lpszClassName = "Win32API"; 30 RegisterClass(&wc); 31 //特殊窗口1,始终在顶层,任务栏显示。 32 hWnd=CreateWindowEx(WS_EX_TOPMOST,"Win32API",Title, 33 WS_DLGFRAME|WS_SYSMENU, 34 WinX0,WinY0,WinW,WinH, // 窗口位置、大小 35 NULL, NULL, hI, NULL); 36 if(!hWnd) return FALSE; 37 38 ShowWindow(hWnd, nC); 39 // Main message loop: 40 while (GetMessage(&msg, NULL, 0, 0)) 41 { 42 TranslateMessage(&msg); 43 DispatchMessage(&msg); 44 } 45 return msg.wParam; 46 } 47 //------------------------------------------------ 48 WSADATA ws; 49 SOCKET Ss,Cs; //服务器和客户机的套接口变量 50 struct sockaddr_in SA,CA; //服务器和客户机的主机地址变量 51 WORD SPort = 6666; //本机端口(服务器) 52 //char SIP[16]="127.0.0.1"; //本机IP地址(服务器) 53 char SIP[16]="0.0.0.0"; //本机IP地址(服务器) 54 char Msg[666],Buf[666]; 55 int d,l=sizeof(CA); 56 //------------------------------------------------ 57 void SetSockAddr(struct sockaddr_in *A,WORD Port,char *IP) 58 { 59 A->sin_family = AF_INET; //TCP/IP协议 60 A->sin_port = htons(Port); //端口。 61 A->sin_addr.s_addr =inet_addr(IP); //IP网址。 62 } 63 //------------------------------------------------ 64 //消息处理 65 LRESULT CALLBACK WndProc(HWND hW,UINT msg, 66 WPARAM wP,LPARAM lP) 67 { 68 switch (msg) 69 { 70 case WM_CREATE: //消息:产生窗口 71 WSAStartup(0x0101,&ws); 72 Ss=socket(AF_INET, SOCK_STREAM,0); //创建套接口(流式) 73 SetSockAddr(&SA,SPort,SIP); //设置服务器主机地址 74 d=bind(Ss,(struct sockaddr *)&SA,sizeof(SA));//捆绑主机地址 75 if(d!=0) 76 { 77 wsprintf(Msg,"[Server] %s","bind Error!"); 78 SetWindowText(hW, Msg); //在窗口标题栏显示连接信息 79 break; 80 } 81 //向windows注册套接口Ss所产生的网络消息事件。 82 WSAAsyncSelect(Ss,hW,WM_USER+1,FD_ACCEPT|FD_READ|FD_CLOSE); 83 listen(Ss,5); //监听客户机连接请求 84 break; 85 case WM_DESTROY: //消息:关闭窗口 86 WSAAsyncSelect(Ss, hW, 0, 0); //注销套接口Ss的消息事件。 87 closesocket(Ss); //关闭套接口Ss 88 WSACleanup(); //卸载WinSock DLL 89 PostQuitMessage(0); //向windows发送退出程序的消息 90 break; 91 case WM_USER+1: 92 switch(LOWORD(lP)) 93 { 94 case FD_ACCEPT: 95 Cs=accept(Ss,(struct sockaddr *)&CA,&l);//获取客户机主机地址 96 wsprintf(Msg,"[Server]Client[%d] come in!",Cs); 97 SetWindowText(hW, Msg); //在窗口标题栏显示连接信息 98 99 wsprintf(Msg,"Server:Welcome!"); 100 send(Cs, Msg,strlen(Msg),0); //向客户机发送连接应答 101 break; 102 case FD_READ: 103 d=recv(wP,Buf,sizeof(Buf),0); //接收客户机服务请求,wP=Cs 104 Buf[d]=0; 105 wsprintf(Msg,"[Server]Client[%d]:%s",wP,Buf); 106 SetWindowText(hW, Msg); //在窗口标题栏显示服务请求 107 108 wsprintf(Msg,"Server:%s",Buf); 109 send(wP, Msg,strlen(Msg),0); //向客户机发送服务数据 110 break; 111 case FD_CLOSE: 112 wsprintf(Msg,"[Server]Client[%d] leave!",wP); 113 SetWindowText(hW, Msg); //在窗口标题栏显示信息 114 break; 115 } 116 break; 117 } 118 return DefWindowProc(hW,msg,wP,lP); 119 } 120 //------------------------------------------------
1 //Client.cpp : Defines the entry point for the application. use VC 6.0 2 //========================================================================= 3 //基于TCP,Client程序 4 //说明:鼠标左键按下: 向服务器发送服务请求"Hello!" 5 //说明:鼠标右键按下: 向服务器发送服务请求"Hi!" 6 //========================================================================= 7 //VC6.0 add WSOCK32.LIB in Project->Settings...->Link 8 #include <winsock.h> //by user 9 //------------------------------------------------ 10 // Global Variables: 11 HINSTANCE hInst; // 实例句柄,方便各程序访问 12 HWND hWnd; // 主窗口句柄,方便各程序访问 13 char Title[]=" TCP-Client "; // 窗口标题 The title bar text 14 int WinX0=310,WinY0=10,WinW=300,WinH=50; // 窗口位置、大小 15 16 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 17 //------------------------------------------------ 18 int APIENTRY WinMain(HINSTANCE hI,HINSTANCE hP,LPSTR lp,int nC) 19 { 20 MSG msg; 21 hInst = hI; // Store instance handle in our global variable 22 WNDCLASS wc; 23 24 memset(&wc,0,sizeof(WNDCLASS)); 25 26 wc.lpfnWndProc = (WNDPROC)WndProc; 27 wc.hInstance = hI; 28 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); 29 wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1); 30 wc.lpszClassName = "Win32API"; 31 RegisterClass(&wc); 32 //特殊窗口1,始终在顶层,任务栏显示。 33 hWnd=CreateWindowEx(WS_EX_TOPMOST,"Win32API",Title, 34 WS_DLGFRAME|WS_SYSMENU, 35 // WinX0,WinY0,WinW,WinH, // 窗口位置、大小 36 CW_USEDEFAULT,CW_USEDEFAULT,WinW,WinH, // 窗口位置 Windows selects the default position 、大小 37 NULL, NULL, hI, NULL); 38 if(!hWnd) return FALSE; 39 40 ShowWindow(hWnd, nC); 41 // Main message loop: 42 while (GetMessage(&msg, NULL, 0, 0)) 43 { 44 TranslateMessage(&msg); 45 DispatchMessage(&msg); 46 } 47 return msg.wParam; 48 } 49 //------------------------------------------------ 50 WSADATA ws; 51 SOCKET Ss,Cs; //服务器和客户机的套接口变量 52 struct sockaddr_in SA,CA; //服务器和客户机的主机地址变量 53 WORD SPort = 6666; //远程服务器端口 54 char SIP[16]="127.0.0.1"; //远程服务器IP地址 55 //char SIP[16]="0.0.0.0"; //远程服务器IP地址 56 char Msg[666],Buf[666]; 57 int d,l=sizeof(CA); 58 //------------------------------------------------ 59 void SetSockAddr(
以上是关于实验一:基于Winsock完成简单的网络程序开发的主要内容,如果未能解决你的问题,请参考以下文章