码迷,mamicode.com
首页 > 编程语言 > 详细

VC++ UDP网络控制台程序

时间:2017-12-14 04:13:02      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:设置   img   bind   编程   error   gpo   soc   clu   int   

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

1、服务端代码

//UDPServer.cpp

#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);  
      
    //关闭套接字  
    closesocket(sockServer);  
      
    //终止套接字库的使用  
    WSACleanup();  
      
    return 0;  
}  

2、客户端代码

//UDPClient.cpp

#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", strlen("Hello")+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");  
    }  
  
    //关闭套接字  
    closesocket(sockClient);  
  
    //终止套接字库的使用  
    WSACleanup();  
  
    return 0;  
}  

注意:上面两部分代码中的

#pragma comment(lib, "WS2_32.lib"); 
可以删除,而改为设置程序属性,具体如下步骤:

  server 和 client 都要"项目属性"--->"配置属性"----> "链接"----> "输入" --->"附加依赖项"中添加"ws2_32.lib"

 

先启动服务端UDPServer程序,再启动客户端UDPClient程序,运行结果如下:

服务端UDPServer

技术分享图片

客户端UDPClient

 

技术分享图片

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

技术分享图片

参考资料:

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

2、MSDN帮助文档

VC++ UDP网络控制台程序

标签:设置   img   bind   编程   error   gpo   soc   clu   int   

原文地址:http://www.cnblogs.com/rainbow70626/p/8034950.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!