zoukankan      html  css  js  c++  java
  • UDP Client—Linux

    #include <stdio.h>
    #include <netinet/ip.h>
    
    int main(int argc,char *argv[])
    {
    #define PERCENT        100
    #define THOUSAND    1000
        int a = 0;
        int iSock = 0, tmpSock = 0;
        int opt = 0;
        int iRet = 0;
        int iRes = 0;
        int iFileDesc = 0;
        int iFileRet = 0;
        unsigned short usPeerPort = 11000;
        char *pPeerIP = "192.168.1.100";
        unsigned char pRecvBuff[20480];
        unsigned char pSendBuff[20480] = "I'm Zynq ^_^";
        time_t now;
        struct tm *p;
        unsigned int uiCnt = 0;
        unsigned int uiPeriod = 0;
        unsigned int uiSpeed = 0;
        unsigned int uiTmp = 0;
        char filePre[20] = {0};
        char fileName[100] = {0};
        char cmd[100] = {0};
        struct sockaddr_in addrLocal, addrPeer;
        static unsigned int s_uiLen = 0;
    
        memset(pRecvBuff, 0x00, 1024);
    
        iSock = socket(AF_INET, SOCK_DGRAM, 0);
        if (iSock < 0)
        {
            printf("Create socket error![%d]
    ", iSock);
            return 0;
        }
    
        //iRes = 1;
        //ioctlsocket(iSock, FIONBIO, (u_long FAR*)&iRes);
    
        /* 设置本地的IP地址 */
        addrLocal.sin_family = AF_INET;
        addrLocal.sin_port = htons(1698);
        addrLocal.sin_addr.s_addr = htonl(INADDR_ANY);
        //addrLocal.sin_addr.S_un.S_addr = inet_addr("10.10.22.100");
    
        /* 设置远端的IP地址 --by cqs */
        addrPeer.sin_family = AF_INET;
        addrPeer.sin_port = htons(usPeerPort);
        addrPeer.sin_addr.s_addr = inet_addr(pPeerIP);
    
        iRet = bind(iSock, (struct sockaddr *)&addrLocal, sizeof(struct sockaddr));
        if (iRet < 0)
        {
            printf("bind error
    ");
            return 0;
        }
    
        
        iRet = sendto(iSock, (char *)pSendBuff, 50, 0, (struct sockaddr*)&addrPeer, sizeof(struct sockaddr));
        if (iRet > 0)
        {
            printf("Udp send: %s
    ", pSendBuff);
        }
        else
        {
            printf("Udp send error![%d]
    ", iRet);
        }
    
        return 0;
    }
  • 相关阅读:
    ASP.NET Post方式提交
    MVC增加操作日志
    asp.net MVC 下拉多级联动及编辑
    redis基本数据类型之String
    关于idea下使用springinitializr创建项目时 初始化失败的解决
    Failed to read artifact descriptor for org.mybatis:mybatis:jar:2.2.1
    如何查看日志文件
    nginx 部署vue 以及同一端口下部署监听多个vue 项目
    JsonView 与JsonIgnore 使用
    vue 打包部署
  • 原文地址:https://www.cnblogs.com/cnpirate/p/3480673.html
Copyright © 2011-2022 走看看