• 从零构建自己的远控•UDP构建socket通信(5)


    先用udp构建一个socket通信示例为后面编写iocp服务器做准备

    先编写服务器 3步 初始化套接字  绑定端口  收数据

    #include <stdio.h>
    #include <winsock2.h>
    #include <WS2tcpip.h>
    #pragma comment(lib,"ws2_32.lib")
    
    int main()
    {
        WSADATA wsaData;
        if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
        {
            printf("WSAStartup error%08X", WSAGetLastError());
            return 0;
        }
        //1.初始化套接字
        SOCKET s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    
        //2.绑定端口
        sockaddr_in addr;
        //转换大小尾
        addr.sin_family = AF_INET;
        addr.sin_port = htons(0x12345);
        inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
        if (bind(s, (sockaddr*)&addr, sizeof(addr)) != 0)
        {
            printf("WSAStartup error%08X", WSAGetLastError());
            return 0;
        }
    
        //域名转换
        //struct addrinfo hints = { 0 };
        //hints.ai_family = AF_INET;
        //struct addrinfo* resulut = NULL;
        //struct addrinfo* ptr = NULL;
        //if (getaddrinfo("localhost", NULL, &hints, &resulut))
        //{
        //    printf("getaddrinfo error");
        //    return 0;
        //}
        ////是个链表
        //struct sockaddr_in* sockaddr_ipv4;
        ////循环遍历所有ip地址
        //for (ptr = resulut; ptr != NULL; ptr = ptr->ai_next)
        //{
        //    sockaddr_ipv4 = (struct sockaddr_in*)ptr->ai_addr;
        ///*    printf("	IPv4 address %s
    ",
        //        inet_ntoa(sockaddr_ipv4->sin_addr));*/
        //    char szIP[128];
        //    inet_ntop(AF_INET, &sockaddr_ipv4->sin_addr, szIP, sizeof(szIP));
        //    printf("%s", szIP);
    
        //}
        //3.收发数据
        printf("recvfrom");
        char szBuffer[256];
        sockaddr_in caddr; //客户端地址
        int nLength = sizeof(caddr);
        while (1)
        {
            int nRet=recvfrom(s, szBuffer,sizeof(szBuffer),0,(sockaddr *)&caddr, &nLength);
            if (nRet==SOCKET_ERROR)
            {
                printf("recvfrom error %08X", WSAGetLastError());
                break;
            }
        szBuffer[nRet] = '';
    printf("data:%s bytes:%d ", szBuffer, nRet); } //关闭初始化 closesocket(s); //反初始化库 WSACleanup(); }

    客户端 2步 初始化套接字 发消息

    #include <stdio.h>
    #include <winsock2.h>
    #include <WS2tcpip.h>
    #pragma comment(lib,"ws2_32.lib")
    
    
    int main()
    {
        WSADATA wsaData;
        if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
        {
            printf("WSAStartup error%08X", WSAGetLastError());
            return 0;
        }
        //1.初始化套接字
        SOCKET s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    
        sockaddr_in addr;
        //转换大小尾
        addr.sin_family = AF_INET;
        addr.sin_port = htons(0x12345);
        inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
    
        //2.收发数据
        char szBuffer[256];
        while (1)
        {
            scanf_s("%s", szBuffer, sizeof(szBuffer));
            //发送数据
             sendto(s, szBuffer, strlen(szBuffer), 0, (sockaddr*)&addr, sizeof(addr));
        
        }
        //关闭初始化
        closesocket(s);
        //反初始化库
        WSACleanup();
    }

    丢包严重。。 

    单线程的简单模拟通信, socket 离不开多线程的 不可能只有一个socket结构 后面要把socket 封装成类 为每一个客户端实例化

     这样代码服务端

    #include <stdio.h>
    #include <winsock2.h>
    #include <WS2tcpip.h>
    #pragma comment(lib,"ws2_32.lib")
    
    DWORD WINAPI RecvThread(LPVOID lpParameter)//接收消息线程
    {
        SOCKET s = (SOCKET)lpParameter;
        char szBuffer[256];
        sockaddr_in caddr; //客户端地址
        int nLength = sizeof(caddr);
        while (1)
        {
            //接收  返回接收长度
            int nRet = recvfrom(s, szBuffer, sizeof(szBuffer), 0, (sockaddr*)&caddr, &nLength);
            if (nRet == SOCKET_ERROR)
            {
                printf("recvfrom error %08X", WSAGetLastError());
                break;
            }
            szBuffer[nRet] = '';
    
            char IPdotdec[20]; //存放点分十进制IP地址
            struct in_addr s; // IPv4地址结构体
            inet_ntop(AF_INET, &caddr.sin_addr, IPdotdec, 16);//点阵转换ip
            printf("ip:%s port:%d  data:%s bytes:%d
    ", IPdotdec, caddr.sin_port, szBuffer, nRet);
        }
    }
    
    int main()
    {
        WSADATA wsaData;
        if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
        {
            printf("WSAStartup error%08X", WSAGetLastError());
            return 0;
        }
        //1.初始化套接字
        SOCKET s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    
        //2.绑定端口
        sockaddr_in addr;
        //转换大小尾
        addr.sin_family = AF_INET;
        addr.sin_port = htons(0x12345);
        inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
        if (bind(s, (sockaddr*)&addr, sizeof(addr)) != 0)
        {
            printf("WSAStartup error%08X", WSAGetLastError());
            return 0;
        }
    
    
        //3.收发数据
        printf("recvfrom、
    ");
        CreateThread(NULL, 0, RecvThread, (LPVOID)s, 0, NULL);
        char szBuffer[256];
        sockaddr_in caddr; //客户端地址
        int nLength = sizeof(caddr);
        while (1)
        {
            //接收  返回接收长度
            int nRet = recvfrom(s, szBuffer, sizeof(szBuffer), 0, (sockaddr*)&caddr, &nLength);
            if (nRet == SOCKET_ERROR)
            {
                printf("recvfrom error %08X", WSAGetLastError());
                break;
            }
            szBuffer[nRet] = '';
        
            char IPdotdec[20]; //存放点分十进制IP地址
            inet_ntop(AF_INET, &caddr.sin_addr, IPdotdec, 16);//点阵转换ip
            printf("ip:%s port:%d  data:%s bytes:%d
    ", IPdotdec, caddr.sin_port, szBuffer, nRet);
            
            sendto(s,"ok", strlen("ok"), 0, (sockaddr*)&caddr, sizeof(caddr));
        }
        //关闭初始化
        closesocket(s);
        //反初始化库
        WSACleanup();
    }

    代码端

    #include <stdio.h>
    #include <winsock2.h>
    #include <WS2tcpip.h>
    #pragma comment(lib,"ws2_32.lib")
    
    DWORD WINAPI RecvThread(LPVOID lpParameter)//接收消息线程
    {
        SOCKET s = (SOCKET)lpParameter;
        char szBuffer[256];
        sockaddr_in caddr; //客户端地址
        int nLength = sizeof(caddr);
        while (1)
        {
            //接收  返回接收长度
            int nRet = recvfrom(s, szBuffer, sizeof(szBuffer), 0, (sockaddr*)&caddr, &nLength);
            if (nRet == SOCKET_ERROR)
            {
                printf("recvfrom error %08X", WSAGetLastError());
                break;
            }
            szBuffer[nRet] = '';
    
            char IPdotdec[20]; //存放点分十进制IP地址
            struct in_addr s; // IPv4地址结构体
            inet_ntop(AF_INET, &caddr.sin_addr, IPdotdec, 16);//点阵转换ip
            printf("ip:%s port:%d  data:%s bytes:%d
    ", IPdotdec, caddr.sin_port, szBuffer, nRet);
        }    
    }
    
    int main()
    {
        WSADATA wsaData;
        if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
        {
            printf("WSAStartup error%08X", WSAGetLastError());
            return 0;
        }
        //1.初始化套接字
        SOCKET s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    
        sockaddr_in addr;
        //转换大小尾
        addr.sin_family = AF_INET;
        addr.sin_port = htons(0x12345);
        inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
    
    
        //2.收发数据
        char szBuffer[256];
    
        int flag = 1;
        while (1)
        {
            scanf_s("%s", szBuffer, sizeof(szBuffer));
         fflush(stdin);
    //发送数据 sendto(s, szBuffer, strlen(szBuffer), 0, (sockaddr*)&addr, sizeof(addr)); //char IPdotdec[20]; //存放点分十进制IP地址 //inet_ntop(AF_INET, &addr.sin_addr, IPdotdec, 16); //printf("ip :%s port:%d ", IPdotdec, addr.sin_port); if (flag) { CreateThread(NULL, 0, RecvThread, (LPVOID)s, 0, NULL); flag = 0; } } //关闭初始化 closesocket(s); //反初始化库 WSACleanup(); }
    从此山高路远,纵马扬鞭。愿往后旅途,三冬暖,春不寒,天黑有灯,下雨有伞。此生尽兴,不负勇往。
  • 相关阅读:
    _ 下划线 Underscores __init__
    Page not found (404) 不被Django的exception中间件捕捉 中间件
    从装修儿童房时的门锁说起
    欧拉定理 费马小定理的推广
    线性运算 非线性运算
    Optimistic concurrency control 死锁 悲观锁 乐观锁 自旋锁
    Avoiding Full Table Scans
    批量的单向的ssh 认证
    批量的单向的ssh 认证
    Corrupted MAC on input at /usr/local/perl/lib/site_perl/5.22.1/x86_64-linux/Net/SSH/Perl/Packet.pm l
  • 原文地址:https://www.cnblogs.com/feizianquan/p/15158535.html
Copyright © 2020-2023  润新知