zoukankan      html  css  js  c++  java
  • Winsock.简单UDP

    PS:vs2017 编译C++代码 支持 XP:项目属性-->链接器-->系统-->需要的最小版本--> 输入 "5.1"

    1、ZC:测试:c向s 发送长度为0的数据,是可行的。(1)用s和c都在本机 测试,OK (2)用 冬的HP笔记本 和 我的联想小新笔记本 测试,OK

    2、测试代码:

    // SampleUDP_01.cpp : This file contains the 'main' function. Program execution begins and ends there.
    //
    
    #define _CRT_SECURE_NO_WARNINGS
    #define _WINSOCK_DEPRECATED_NO_WARNINGS
    
    #include <iostream>
    
    int main1();
    int main2();
    
    int main(int _argc, char** _argv)
    {
        std::cout << "Hello World!
    "; 
    
        if (_argc <= 1)
        {
            printf("Command line no parameter .");
            return 0;
        }
    
        printf("%s
    ", _argv[0]);
        if (_argv[1][0] == 's')
            main1();
        else if (_argv[1][0] == 'c')
            main2();
        else
            printf("Unknown command line parameter : %s
    ", _argv[1]);
    
        return 0;
    }
    
    
    
    #include <stdio.h>
    #include <winsock2.h>
    #pragma comment(lib, "ws2_32.lib")
    #define PORT 8888
    
    int main1()
    {
        WSADATA wsaData;
        int Ret = WSAStartup(MAKEWORD(2, 2), &wsaData);
        printf("Server!
    ");
        if (Ret != 0)
        {
            printf("无法初始化winsock.
    ");
            WSACleanup();
    }
        else {
    
            //    printf("初始化winsock成功
    ");
        }
    
        if (LOBYTE(wsaData.wVersion) != 2 ||
            HIBYTE(wsaData.wVersion) != 2)
        {
            WSACleanup();
            return 1;
        }
    
        SOCKET socket = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
        sockaddr_in addr;
        addr.sin_addr.S_un.S_addr = INADDR_ANY;
        addr.sin_family = AF_INET;
        addr.sin_port = ntohs(PORT);
    
        if (socket == INVALID_SOCKET)
        {
            printf("socket Error!");
        }
        int rs = bind(socket, (sockaddr*)&addr, sizeof(addr));
    
        if (rs != 0)
        {
            printf("bind failed!");
            return 1;
        }
        char buf[200] = "";
        char temp[200] = "";
        sockaddr_in addrcl;
        int len = sizeof(sockaddr);
    
        while (1) {
    
            int iRecv = recvfrom(socket, buf, sizeof(buf), 0, (sockaddr*)&addrcl, &len);//用新建的地址结构保存客户端的信息 
    
            if (iRecv == 0)
            {
                printf("recvfrom len : %d
    ", iRecv);
                break;
            }
    
            sprintf(temp, "%s:%s", inet_ntoa(addrcl.sin_addr), buf);
            sendto(socket, temp, strlen(temp) + 1, 0, (sockaddr*)&addrcl, sizeof(addr));
            if (strcmp(buf, "srvexit") == 0)
            {
                break;
            }
            printf("%s", buf);
        }
        closesocket(socket);
    
        WSACleanup();
        printf("exit
    ");
        return 0;
    }
    
    
    #include <WinSock2.h>
    #include <stdio.h>
    #define PORT 8888
    int main2()
    {
        WSADATA wsaData;
        int Ret = WSAStartup(MAKEWORD(2, 2), &wsaData);
        printf("Client..!
    ");
        if (Ret != 0)
        {
            printf("无法初始化winsock.
    ");
            WSACleanup();
        }
        else {
    
            //    printf("初始化winsock成功
    ");
        }
    
        SOCKET socketc = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
        sockaddr_in addr;
        //addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
        addr.sin_addr.S_un.S_addr = inet_addr("192.168.2.102");
        addr.sin_family = AF_INET;
        addr.sin_port = ntohs(PORT);
    
        if (socketc == INVALID_SOCKET)
        {
            printf("socket Error!");
        }
    
        int len = sizeof(sockaddr);
    
    
        while (1)//client
        {
            char buf[200] = "";
            char rbuf[300] = "";
            printf("
    :");
            scanf("%s", &buf);
            //sendto(socketc, buf, strlen(buf) + 1, 0, (SOCKADDR*)&addr, len);//用回原来的地址结构
            sendto(socketc, buf, 0, 0, (SOCKADDR*)&addr, len);//用回原来的地址结构
            break;
    
            recvfrom(socketc, rbuf, sizeof(rbuf), 0, (SOCKADDR*)&addr, &len);
            if (strcmp(rbuf, "exit") == 0)
            {
                break;
            }
            printf("%s", rbuf);
    
        }
    
        closesocket(socketc);
        WSACleanup();
        printf("end
    ");
        return 0;
    }

    3、vs2017 编译C++代码 支持 XP

      

    4、

    5、

  • 相关阅读:
    更改Linux时区的两种方法
    Azure上部署FTP服务
    log4j2简单使用
    elasticsearch5.5.2环境搭建
    springTask和Schedule学习
    防止SpringMVC拦截器拦截js等静态资源文件
    Mysql中使用FIND_IN_SET解决IN条件为字符串时只有第一个数据可用的问题
    ServletContext总结
    window.location.href和window.location.replace的区别
    javadoc文档
  • 原文地址:https://www.cnblogs.com/cppskill/p/11986290.html
Copyright © 2011-2022 走看看