程式碼如下:

下載:https://download.csdn.net/download/mao0514/10830400

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
 
#define MYPORT 3956
char* SERVERIP = "192.168.1.57";
 
#define ERR_EXIT(m) \
    do \
{ \
    perror(m); \
    exit(EXIT_FAILURE); \
    } while(0)
 
void echo_cli(int sock,int reg)
{
    struct sockaddr_in servaddr;
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(MYPORT);
    servaddr.sin_addr.s_addr = inet_addr(SERVERIP);
    
    int ret,i;
    char tmp[128] = {0};
    char sendbuf[128] = {0};
    char recvbuf[128] = {0};
         sendbuf[0] = 0x42;
	 sendbuf[1] = 01;
	 sendbuf[2] = 0;
	 sendbuf[3] = 0x80;
	 sendbuf[4] = 0;
	 sendbuf[5] = 0x04;
	 sendbuf[6] = 0x00;
	 sendbuf[7] = 0x01;
	 sendbuf[8] = 0;
	 sendbuf[9] = 0x00;
	 sendbuf[10] = 0x00;
	 sendbuf[11] = reg;
//42 01 00 80 00 04 00 01 00 00 00 24 
   // printf("test3\n");
    while (1)
    {
       // fgets(tmp,sizeof(tmp),stdin);//scanf
       // printf("send\n");
        sendto(sock, sendbuf, 12, 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
        
        ret = recvfrom(sock, recvbuf, sizeof(recvbuf), 0, NULL, NULL);
        if (ret == -1)
        {
            if (errno == EINTR)
                continue;
            ERR_EXIT("recvfrom");
        }
        else
        {
            printf("recv[%02x]:",reg&0xff);
            for(i=0;i<12;i++)
            printf(" 0x%02x",recvbuf[i]&0xff);
            printf("\n");
        }
        // sendbuf[11]+=4;
        memset(sendbuf, 0, sizeof(sendbuf));
        memset(recvbuf, 0, sizeof(recvbuf));
        break;
    }
    
}
 
int main(int argc, char* argv[])
{
    int sock,i;
   // printf("test1\n");
    if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
        ERR_EXIT("socket");
   // printf("test2\n");
  // printf("%x,%x",*argv[0],*argv[1]);
   for(i=0;i<10;i++)
    echo_cli(sock,i*4);
  
      close(sock);
    return 0;
}


rm net
arm-hisiv600-linux-gcc  net.c -o net 
#gcc  net.c -o net 

參考:https://blog.csdn.net/lell3538/article/details/53335472