1.UDP/IP

int sendMsg()
{
    while (1)
    {
        //设置sockaddr结构  接收端地址和端口号
        sockaddr_in saOtherDevice;
        saOtherDevice.sin_family = AF_INET;
        saOtherDevice.sin_addr.s_addr = inet_addr("192.168.31.55");
        saOtherDevice.sin_port = htons(3000);
 

        char sendbuff[548] = {0};

        scanf_s("%s", sendbuff, sizeof(sendbuff) - 1);
        int sendto_ret = sendto(socket_This_Device, sendbuff, sizeof(sendbuff) - 1, 0, (sockaddr*)&saOtherDevice, sizeof(saOtherDevice));
        if (SOCKET_ERROR == sendto_ret)
        {
            write_error();
        }

        Sleep(3);
    }
}



int create(const char* ServerIpAdress)
{
 
        /*接收信息,主要是高低版本信息*/
        WSADATA wsaData;

        /* 使用Windef.h中声明的MAKEWORD(低字节、高字节)宏 */
        WORD wVersionRequested = MAKEWORD(2, 2);


        /*启用网络链接库,调用的封装库命令*/
        if (0 != WSAStartup(wVersionRequested, &wsaData))
        {
            write_error();
        }


        /*确认WinSock DLL支持2.2*/
        if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
            /* Tell the user that we could not find a usable */
            /* WinSock DLL.                                  */
            write_error();
            //清理网络库
            WSACleanup();

        }

        //创建套接字。 创建网络类型 tcp或者upd
          socket_This_Device = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
        if (INVALID_SOCKET == socket_This_Device)
        {
            write_error();
            //清理网络库
            WSACleanup();

        }


        //设置本设备的sockaddr结构  
        sockaddr_in saThis_Device;
        saThis_Device.sin_family = AF_INET;
        saThis_Device.sin_addr.s_addr = inet_addr(ServerIpAdress);
        saThis_Device.sin_port = htons(4000);

        //使用绑定侦听套接字    将本机地址和端口号绑定到socket     如果不作为接收端,这一步可以省略
 
        if (SOCKET_ERROR == bind(socket_This_Device, (SOCKADDR*)&saThis_Device, sizeof(saThis_Device)))
        {
            string ret = to_string(WSAGetLastError());
            MessageBoxA(0, ret.c_str(), "error_bind", 0);
            //释放stocket
            closesocket(socket_This_Device);
            //清理网络库
            WSACleanup();
            return -1;
        }

        /*本设备发送消息给其它IP*/
        CloseHandle(CreateThread(0, 0, (LPTHREAD_START_ROUTINE)sendMsg,0, 0, NULL));

        while (true)
        {
            /*接收字符串和发送端设备的IP和端口,类似TCP的revc*/
      
            char Getbuff[548] = {0};

            sockaddr_in saGetDeviceMsg = { 0 };
            int saGetDeviceMsgSize = sizeof(sockaddr);
    

            int recvfrom_ret = recvfrom(socket_This_Device, Getbuff, sizeof(Getbuff), 0, (sockaddr*)&saGetDeviceMsg, &saGetDeviceMsgSize);
            if (SOCKET_ERROR == recvfrom_ret)
            {
                write_error();
            }
 

            printf("ClientIp:%d.%d.%d.%d:%d  \n Buff:%s\n\n",
                saGetDeviceMsg.sin_addr.S_un.S_un_b.s_b1,
                saGetDeviceMsg.sin_addr.S_un.S_un_b.s_b2,
                saGetDeviceMsg.sin_addr.S_un.S_un_b.s_b3,
                saGetDeviceMsg.sin_addr.S_un.S_un_b.s_b4,
                ntohs(saGetDeviceMsg.sin_port),
                Getbuff);


            Sleep(3);
        }

        system("pause");

        //释放stocket
        closesocket(socket_This_Device);
        //清理网络库
        WSACleanup();

}

代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值