VC++ UDP网络控制台程序

来源:互联网 发布:软件信息服务业 所得税 编辑:程序博客网 时间:2024/05/16 04:37

原文:http://blog.csdn.net/ccf19881030/article/details/16991215

我在server里面分别添加了发送,在clent添加了接收的部分附在最后面.


采用的是VC2008,控制台应用程序,使用UDP编写。

1、服务端代码

//UDPServer.cpp

[cpp] view plain copy
  1. #include <WinSock2.h>  
  2. #include <stdio.h>  
  3.   
  4. #define SERVERPORT 6000 //服务端口号  
  5.   
  6. #pragma comment(lib, "WS2_32.lib");  
  7.   
  8. int main(int argc, char *argv[])  
  9. {  
  10.     //加载套接字库  
  11.     WORD wVersionRequested;  
  12.     WSADATA wsaData;  
  13.     int err;  
  14.   
  15.     wVersionRequested = MAKEWORD( 2, 2 );  
  16.       
  17.     err = WSAStartup( wVersionRequested, &wsaData );  
  18.     if ( err != 0 ) {  
  19.         /* Tell the user that we could not find a usable */  
  20.         /* WinSock DLL.                                  */  
  21.         return -1;  
  22.     }  
  23.   
  24.     /* Confirm that the WinSock DLL supports 2.2.*/  
  25.     /* Note that if the DLL supports versions greater    */  
  26.     /* than 2.2 in addition to 2.2, it will still return */  
  27.     /* 2.2 in wVersion since that is the version we      */  
  28.     /* requested.                                        */  
  29.   
  30.     if ( LOBYTE( wsaData.wVersion ) != 2 ||  
  31.         HIBYTE( wsaData.wVersion ) != 2 ) {  
  32.             /* Tell the user that we could not find a usable */  
  33.             /* WinSock DLL.                                  */  
  34.             WSACleanup( );  
  35.             return -1;   
  36.     }  
  37.   
  38.     /* The WinSock DLL is acceptable. Proceed. */  
  39.     //创建套接字  
  40.     SOCKET sockServer = socket(AF_INET, SOCK_DGRAM, 0);  
  41.     if (INVALID_SOCKET == sockServer)  
  42.     {  
  43.         printf("socket() called failed! The error code is: %d\n", WSAGetLastError());  
  44.         return -1;  
  45.     }  
  46.     else  
  47.     {  
  48.         printf("socket() called succesful!\n");  
  49.     }  
  50.       
  51.     //服务器端  
  52.     SOCKADDR_IN addrServer;  
  53.     addrServer.sin_addr.S_un.S_addr = htonl(INADDR_ANY);  
  54.     addrServer.sin_family = AF_INET;  
  55.     addrServer.sin_port = htons(SERVERPORT);  
  56.   
  57.     //绑定套接字  
  58.     err = bind(sockServer, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));  
  59.     if (SOCKET_ERROR == err)  
  60.     {  
  61.         printf("bind() called failed! The error code is: %d\n", WSAGetLastError());  
  62.         return -1;  
  63.     }  
  64.     else  
  65.     {  
  66.         printf("bind() called successful!\n");  
  67.     }  
  68.   
  69.     //等待并接收数据  
  70.     SOCKADDR_IN addrClient;//用于接收发送端的地址信息  
  71.     int len = sizeof(SOCKADDR);  
  72.     char recvBuf[100];  
  73.     recvfrom(sockServer, recvBuf, 100, 0, (SOCKADDR*)&addrClient, &len);  
  74.     //打印接收到的数据  
  75.     printf("receive data:%s from client [%s,%d]", recvBuf, inet_ntoa(addrClient.sin_addr), addrClient.sin_port);  
  76.       
  77.     //关闭套接字  
  78.     closesocket(sockServer);  
  79.       
  80.     //终止套接字库的使用  
  81.     WSACleanup();  
  82.       
  83.     return 0;  
  84. }  

2、客户端代码

//UDPClient.cpp

[cpp] view plain copy
  1. #include <WinSock2.h>  
  2. #include <stdio.h>  
  3.   
  4. #define SERVERPORT 6000 //服务端口号  
  5.   
  6. #pragma comment(lib, "WS2_32.lib");  
  7.   
  8. int main(int argc, char *argv[])  
  9. {  
  10.     //加载套接字库  
  11.     WORD wVersionRequested;  
  12.     WSADATA wsaData;  
  13.     int err;  
  14.   
  15.     wVersionRequested = MAKEWORD( 2, 2 );  
  16.   
  17.     err = WSAStartup( wVersionRequested, &wsaData );  
  18.     if ( err != 0 ) {  
  19.         /* Tell the user that we could not find a usable */  
  20.         /* WinSock DLL.                                  */  
  21.         return -1;  
  22.     }  
  23.   
  24.     /* Confirm that the WinSock DLL supports 2.2.*/  
  25.     /* Note that if the DLL supports versions greater    */  
  26.     /* than 2.2 in addition to 2.2, it will still return */  
  27.     /* 2.2 in wVersion since that is the version we      */  
  28.     /* requested.                                        */  
  29.   
  30.     if ( LOBYTE( wsaData.wVersion ) != 2 ||  
  31.         HIBYTE( wsaData.wVersion ) != 2 ) {  
  32.             /* Tell the user that we could not find a usable */  
  33.             /* WinSock DLL.                                  */  
  34.             WSACleanup( );  
  35.             return -1;   
  36.     }  
  37.       
  38.     //创建套接字  
  39.     SOCKET sockClient = socket(AF_INET, SOCK_DGRAM, 0);  
  40.     if (INVALID_SOCKET == sockClient)  
  41.     {  
  42.         printf("socket() called failed! The error code is: %d\n", WSAGetLastError());  
  43.         return -1;  
  44.     }  
  45.     else  
  46.     {  
  47.         printf("socket() called succesful!\n");  
  48.     }  
  49.   
  50.     SOCKADDR_IN addrServer;  
  51.     addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");  
  52.     addrServer.sin_family = AF_INET;  
  53.     addrServer.sin_port = htons(SERVERPORT);  
  54.       
  55.     //发送数据  
  56.     err = sendto(sockClient, "Hello", strlen("Hello")+1, 0, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));  
  57.     if (SOCKET_ERROR == err)  
  58.     {  
  59.         printf("sendto() called failed! The error code is: %s\n", WSAGetLastError());  
  60.         return -1;  
  61.     }  
  62.     else  
  63.     {  
  64.         printf("sendto() called successful!\n");  
  65.     }  
  66.   
  67.     //关闭套接字  
  68.     closesocket(sockClient);  
  69.   
  70.     //终止套接字库的使用  
  71.     WSACleanup();  
  72.   
  73.     return 0;  
  74. }  
先启动服务端UDPServer程序,再启动客户端UDPClient程序,运行结果如下:

服务端UDPServer

客户端UDPClient

此时服务端UDPServer的结果会发生变化,会受到客户端发送过来的数据,如下图所示:


参考资料:

1、《VC++深入详解》 第14章网络编程 ,孙鑫主编

2、MSDN帮助文档

#include <WinSock2.h>#include <stdio.h>#define SERVERPORT 6000//服务端口号#pragma comment(lib, "WS2_32.lib");int main(int argc, char *argv[]){//加载套接字库WORD wVersionRequested;WSADATA wsaData;int err;wVersionRequested = MAKEWORD( 2, 2 );err = WSAStartup( wVersionRequested, &wsaData );if ( err != 0 ) {/* Tell the user that we could not find a usable *//* WinSock DLL.                                  */return -1;}/* Confirm that the WinSock DLL supports 2.2.*//* Note that if the DLL supports versions greater    *//* than 2.2 in addition to 2.2, it will still return *//* 2.2 in wVersion since that is the version we      *//* requested.                                        */if ( LOBYTE( wsaData.wVersion ) != 2 ||HIBYTE( wsaData.wVersion ) != 2 ) {/* Tell the user that we could not find a usable *//* WinSock DLL.                                  */WSACleanup( );return -1; }/* The WinSock DLL is acceptable. Proceed. *///创建套接字SOCKET sockServer = socket(AF_INET, SOCK_DGRAM, 0);if (INVALID_SOCKET == sockServer){printf("socket() called failed! The error code is: %d\n", WSAGetLastError());return -1;}else{printf("socket() called succesful!\n");}//服务器端SOCKADDR_IN addrServer;addrServer.sin_addr.S_un.S_addr = htonl(INADDR_ANY);addrServer.sin_family = AF_INET;addrServer.sin_port = htons(SERVERPORT);//绑定套接字err = bind(sockServer, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));if (SOCKET_ERROR == err){printf("bind() called failed! The error code is: %d\n", WSAGetLastError());return -1;}else{printf("bind() called successful!\n");}//等待并接收数据SOCKADDR_IN addrClient;//用于接收发送端的地址信息int len = sizeof(SOCKADDR);char recvBuf[100];recvfrom(sockServer, recvBuf, 100, 0, (SOCKADDR*)&addrClient, &len);//打印接收到的数据printf("receive data:%s from client [%s,%d]", recvBuf, inet_ntoa(addrClient.sin_addr), addrClient.sin_port);//发送数据err = sendto(sockServer, "Hello Client", strlen("Hello Client")+1, 0, (SOCKADDR*)&addrClient, sizeof(SOCKADDR));if (SOCKET_ERROR == err){printf("sendto() called failed! The error code is: %s\n", WSAGetLastError());return -1;}else{printf("sendto() called successful!\n");}//关闭套接字closesocket(sockServer);//终止套接字库的使用WSACleanup();return 0;}


clent端:

#include <WinSock2.h>#include <stdio.h>#define SERVERPORT 6000//服务端口号#pragma comment(lib, "WS2_32.lib");int main(int argc, char *argv[]){//加载套接字库WORD wVersionRequested;WSADATA wsaData;int err;wVersionRequested = MAKEWORD( 2, 2 );err = WSAStartup( wVersionRequested, &wsaData );if ( err != 0 ) {/* Tell the user that we could not find a usable *//* WinSock DLL.                                  */return -1;}/* Confirm that the WinSock DLL supports 2.2.*//* Note that if the DLL supports versions greater    *//* than 2.2 in addition to 2.2, it will still return *//* 2.2 in wVersion since that is the version we      *//* requested.                                        */if ( LOBYTE( wsaData.wVersion ) != 2 ||HIBYTE( wsaData.wVersion ) != 2 ) {/* Tell the user that we could not find a usable *//* WinSock DLL.                                  */WSACleanup( );return -1; }//创建套接字SOCKET sockClient = socket(AF_INET, SOCK_DGRAM, 0);if (INVALID_SOCKET == sockClient){printf("socket() called failed! The error code is: %d\n", WSAGetLastError());return -1;}else{printf("socket() called succesful!\n");}SOCKADDR_IN addrServer;addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");addrServer.sin_family = AF_INET;addrServer.sin_port = htons(SERVERPORT);//发送数据err = sendto(sockClient, "Hello server", strlen("Hello server")+1, 0, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));if (SOCKET_ERROR == err){printf("sendto() called failed! The error code is: %s\n", WSAGetLastError());return -1;}else{printf("sendto() called successful!\n");}int len = sizeof(SOCKADDR);char recvBuf[100];recvfrom(sockClient, recvBuf, 100, 0, (SOCKADDR*)&addrServer, &len);//打印接收到的数据printf("receive data:%s from client [%s,%d]", recvBuf, inet_ntoa(addrServer.sin_addr), addrServer.sin_port);//关闭套接字closesocket(sockClient);//终止套接字库的使用WSACleanup();return 0;}


原创粉丝点击