1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/socket.h>
4 #include <arpa/inet.h>
5 #include <netinet/in.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <linux/input.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12
13 #define CLI_PORT 8888 //1024-49151
14 #define CLI_IP "192.168.124.105"
15
16 int main(int argc, const char *argv[])
17 {
18 if(argc <3)
19 {
20 printf("请在命令行传入服务器的port和 ip\n");
21 return -1;
22 }
23
24 //创建流式套接字文件
25 int cfd =socket(AF_INET,SOCK_STREAM,0);
26 if(cfd<0)
27 {
28 fprintf(stderr,"line:%d ",__LINE__);
29 perror("socket");
30 return -1;
31 }
32 printf("创建流式套接字成功, cfd=%d __%d__\n",cfd,__LINE__);
33 //填充服务器的地址信息,给connect函数使用
34 struct sockaddr_in sin;
35 sin.sin_family =AF_INET;//必须填AF_INET;
36 sin.sin_port =htons(atoi(argv[1]));//服务器绑定的端口号
37 sin.sin_addr.s_addr=inet_addr(argv[2]);//服务器要运行的主机的IP的网络地址
38 //连接指定的服务器
39 if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))<0)
40 {
41 fprintf(stderr,"line:%d ",__LINE__);
42 perror("connect");
43 return -1;
44 }
45 printf("connect server[%s:%s] success __%d__\n",argv[2],argv[1],__LINE__);
46 //打开键盘驱动文件:/dev/input/event1
47 int fd =open("/dev/input/event1",O_RDONLY);
48 if(fd<0)
49 {
50 perror("open");
51 return -1;
52 }
53 struct input_event ev;
54 char buf[5]={0xff,0x02,0x00,-90,0xff};
55 unsigned char buf1[5]={0xff,0x02,0x01,180,0xff};
56 if(send(cfd,buf,sizeof(buf),0)<0)
57 {
58 fprintf(stderr,"line:%d ",__LINE__);
59 perror("send");
60 return -1;
61 }
62 printf("send success __%d__\n",__LINE__);
63
64 char c;
65 while(1)
66 {
67 if(read(fd,&ev,sizeof(ev))<0)
68 {
69 perror("read");
70 return -1;
71 }
72 switch(ev.code*ev.value)
73 {
74 case 17: //控制红色手臂++ 'w'
75 buf[3]+=5;
76 if(buf[3]>90)
77 buf[3]=90;
78 break;
79
80 case 31: //红色手臂--'s'
81 buf[3]-=5;
82 if(buf[3]<-90)
83 buf[3]=-90;
84 break;
85
86 case 30: //控制蓝色手臂++'a'
87 buf1[3]+=5;
88 if(buf1[3]>180)
89 buf1[3]=180;
90 break;
91 case 32: //蓝色手臂--'d'
92 buf1[3]-=5;
93 if(buf1[3]<0)
94 buf1[3]=0;
95 break;
96
97 default:
98 continue;
99 }
100 if(send(cfd,buf,sizeof(buf),0)<0)
101 {
102 fprintf(stderr,"line:%d ",__LINE__);
103 perror("send");
104 return -1;
105
106 }
107 if(send(cfd,buf1,sizeof(buf1),0)<0)
108 {
109 fprintf(stderr,"line:%d ",__LINE__);
110 perror("send");
111 return -1;
112 }
113
114 }
115
116
117
118 //关闭文件描述符
119
120 close(cfd);
121
122 return 0;
123 }
05-07