1 #include <head.h> //TCP通信
2
3 #define SEN_PORT 8888
4 #define SEN_IP "192.168.115.148"
5
6 int main(int argc, const char *argv[])
7 {
8 //1 创建套接字
9 int sfd=socket(AF_INET,SOCK_STREAM,0);
10 if(sfd==-1)
11 {
12 MSG_ERR("socker");
13 return -1;
14 }
15 printf("创建套接字成功\n");
16 //连接到服务器
17 struct sockaddr_in sin;
18 sin.sin_family=AF_INET;
19 sin.sin_port=htons(SEN_PORT);
20 sin.sin_addr.s_addr=inet_addr(SEN_IP);
21 int fd=connect(sfd,(struct sockaddr *)&sin,sizeof(sin));
22 if(fd==-1)
23 {
24 MSG_ERR("connect");
25 return -1;
26 }
27 printf("连接成功\n");
28 char redrr[5]={0xff,0x02,0x00,0x00,0xff};
29 if(send(sfd,redrr,sizeof(redrr),0)==-1)
30 {
31 MSG_ERR("send");
32 return -1;
33 }
34 sleep(1);
35 char bluerr[5]={0xff,0x02,0x01,0x00,0xff};
36 if(send(sfd,bluerr,sizeof(bluerr),0)==-1)
37 {
38 MSG_ERR("send");
39 return -1;
40 }
41 char a;
42 //5 读写操作
43 while(1)
44 {
45 printf("输入:");
46 scanf("%c",&a);
47
48 getchar();
49 switch( a )
50 {
51 case 'w': redrr[3]++;
52 send(sfd,redrr,sizeof(redrr),0);
53 printf("%#x\n",redrr[3]);
54 break ;
55 case 'a': bluerr[3]--;
56 send(sfd,bluerr,sizeof(redrr),0);
57 printf("%#x\n",bluerr[3]);
58 break ;
59 case 's': redrr[3]--;
60 send(sfd,redrr,sizeof(redrr),0);
61 printf("%#x\n",redrr[3]);
62 break ;
63 case 'd': bluerr[3]++;
64 send(sfd,bluerr,sizeof(bluerr),0);
65 printf("%#x\n",bluerr[3]);
66 break ;
67 }
68
69 }
70 //6 关闭套接字
71 close(sfd);
72 return 0;
73 }
~
~
10-05
3044
06-12
1850
12-20
4053