main_cli.c

来自「linux下socket编程示例,通过网络传送控制信息给下位机」· C语言 代码 · 共 86 行

C
86
字号
  /*.......................................................*/  #include <stdio.h>  #include <stdlib.h>  #include <errno.h>  #include <string.h>  #include <netdb.h>  #include <sys/types.h>  #include <netinet/in.h>  #include <sys/socket.h>    #define PORT 3497  /* the port client will be connecting to */typedef union cmd_tmp{/**/	unsigned char cmd_buf_char[64];/**/	struct abc{		int head;/*0x55aaaa55*/		int length;/*64*/		int modulation;		int vpi;		int vci;		char info[64-20];	}cmd_strcut;} mips_arm;extern int adsl2_printf(int len,char *buf);int main(int argc, char *argv[]){    int sockfd, numbytes, sendsize;    struct hostent *he;    struct sockaddr_in their_addr; /* connector's address information */    mips_arm cmd_buf;        if(argc<4){	printf("  socket_set_cli <modulation> <vpi> <vci> \n");	return 0;    }else{	cmd_buf.cmd_strcut.head = 0x55aaaa55;	cmd_buf.cmd_strcut.length = 64;	cmd_buf.cmd_strcut.modulation = strtol(&argv[1][0], NULL, 10);	cmd_buf.cmd_strcut.vpi = strtol(&argv[2][0], NULL, 10);	cmd_buf.cmd_strcut.vci = strtol(&argv[3][0], NULL, 10); 	printf("set adsl modulation: %d\n", cmd_buf.cmd_strcut.modulation);    	printf("set adsl vpi %d, vci %d\n", cmd_buf.cmd_strcut.vpi, cmd_buf.cmd_strcut.vci);    }    he=gethostbyname("192.168.1.252");    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {        perror("socket");        exit(1);    }    their_addr.sin_family = AF_INET;   /* host byte order */    their_addr.sin_port = htons(PORT);  /* short, network byte order */    their_addr.sin_addr = *((struct in_addr *)he->h_addr);    bzero(&(their_addr.sin_zero), 8);   /* zero the rest of the struct */    if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) {        perror("connect");        exit(1);    }    printf("connect ok\n");    sendsize=64;    if ((numbytes = send(sockfd, cmd_buf.cmd_buf_char , sendsize, 0)) == -1){	perror("send");	printf("send 2 e\n");	return 0;    }    printf("send ok\n");    return 0;  } 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?