• windows vc++ vs winsock2.h socket通信


    sendto

    int sendto(
      SOCKET         s,
      const char     *buf,
      int            len,
      int            flags,
      const sockaddr *to,
      int            tolen
    );

    这个使用时需要这样来用。特别是最后的tolen,必须得是一个int参数,不能用sizeof。

    int addrsize = sizeof(addrto);
    int len = sendto(sock, message, BUF_SIZE, 0, (SOCKADDR*)&addrto, addrsize);
    if (len == SOCKET_ERROR)
        printf("recvfrom err = %d
    ", WSAGetLastError());

    比如下面的用法就不行。原因不知道。

    int len = sendto(sock, message, BUF_SIZE, 0, (SOCKADDR*)&addrto, sizeof(addrsize));

    recv和recvfrom

    recv用于tcp的stream流。recvfrom用于udp。

    完整代码

    不知为何,只是接收不行,需要先发送一个数据才能接收?

    #define _WINSOCK_DEPRECATED_NO_WARNINGS
    #include <stdio.h>
    #include <winsock2.h>
    #include <ws2ipdef.h>
    #include <ws2tcpip.h>
    #include <wsipv6ok.h>
    
    
    #pragma comment(lib, "ws2_32.lib")
    
    #define BUF_SIZE 500
    #define _WINSOCK_DEPRECATED_NO_WARNINGS
    
    int iResult = 0;
    WSADATA wsaData;
    SOCKET sock;
    char message[BUF_SIZE] = "send...";
    SOCKADDR_IN6 addrto, addrfrom;
    char rcvbuf[BUF_SIZE];
    
    typedef struct {
        unsigned char ver : 2;
        unsigned char tp_type : 1;
        unsigned char ackflag : 1;
        unsigned char res1 : 4;
        unsigned char seq;
        unsigned char ackseq;
        unsigned char res2;
    }secondhdr_t;
    
    secondhdr_t sechdr;
    unsigned char sequence = 0;
    
    int main(int argc, char * argv[])
    {
        iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    
        sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);    /*需要管理员权限*/
        if (sock == INVALID_SOCKET)
            printf("sock err = %d
    ", WSAGetLastError());
    
        addrfrom.sin6_family = AF_INET6;
        iResult = inet_pton(AF_INET6, "fc00::d41:0:0:d41:2", addrfrom.sin6_addr.u.Byte);
        addrfrom.sin6_port = htons(2020);
        addrfrom.sin6_scope_id = 0;
    
        addrto.sin6_family = AF_INET6;
        iResult = inet_pton(AF_INET6, "fc00::d41:0:0:d41:1", addrto.sin6_addr.u.Byte);
        addrto.sin6_port = htons(2020);
        addrto.sin6_scope_id = 0;
    
        iResult = bind(sock, (SOCKADDR *)&addrfrom, sizeof(addrfrom));
        if (iResult == SOCKET_ERROR)
            printf("bind err = %d
    ", WSAGetLastError());
    
        int addrsize = sizeof(addrto);
        int len = sendto(sock, message, BUF_SIZE, 0, (SOCKADDR*)&addrto, addrsize);
        if (len == SOCKET_ERROR)
            printf("recvfrom err = %d
    ", WSAGetLastError());
    
        while (1)
        {
            addrsize = sizeof(addrfrom);
            len = recvfrom(sock, rcvbuf, BUF_SIZE, 0, (SOCKADDR*)&addrfrom, &addrsize);
            if(len == SOCKET_ERROR)
                printf("recvfrom err = %d
    ", WSAGetLastError());
            for (int i = 0; i < 10; i++)
            {
                printf("%X ", rcvbuf[i]);
            }
            printf("
    len=%d
    ", len);
        }
    }
    View Code
  • 相关阅读:
    Linux Shell 下的输出重定向
    解决 Scrapy-Redis 空跑问题,链接跑完后自动关闭爬虫
    数据清洗基本概念
    前端常见的跨域请求解决方案
    Pandas模块:表计算与数据分析
    Matplotlib模块:绘图和可视化
    numpy如何使用
    Gerapy 使用详解
    MySQL常见数据库引擎及比较
    基于scrapy-redis组件的分布式爬虫
  • 原文地址:https://www.cnblogs.com/yanhc/p/13416866.html
Copyright © 2020-2023  润新知