赞
踩
TCP机械臂测试。
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <pthread.h>
- #include <semaphore.h>
- #include <wait.h>
- #include <signal.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <sys/socket.h>
- #include <sys/ipc.h>
- #include <sys/sem.h>
- #include <semaphore.h>
- #include <sys/msg.h>
- #include <sys/shm.h>
- #include <sys/un.h>
-
- #define SER_PORT 8888
- #define SER_IP "192.168.125.172"
- #define CLI_PORT 6666
- #define CLI_IP "192.168.125.176"
-
- int main(int argc, const char *argv[])
- {
- //创建套接字标识符
- int cfd=socket(AF_INET,SOCK_STREAM,0);
- if(-1 == cfd)
- {
- perror("socket error");
- return -1;
- }
- printf("socket success\n");
- printf("cfd=%d\n",cfd);
-
- //连接服务器
- //准备对端结构体信息
- struct sockaddr_in sin;
- sin.sin_family=AF_INET;
- sin.sin_port=htons(SER_PORT);
- sin.sin_addr.s_addr=inet_addr(SER_IP);
- //连接服务器
- if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin)) == -1)
- {
- perror("connect error");
- return -1;
- }
- printf("connect success\n");
-
- //数据收发
- char rbuf[5]={0xff,0x02,0x00,0x00,0xff};
- char bbuf[5]={0xff,0x02,0x01,0x00,0xff};
- send(cfd,rbuf,sizeof(rbuf),0);
- send(cfd,bbuf,sizeof(bbuf),0);
-
- while(1)
- {
- char c=0;
- int num=0;
- scanf(" %c %d",&c,&num);
- switch(c)
- {
- case 'w':
- case 'W':
- rbuf[3]+=num;
- send(cfd,rbuf,sizeof(rbuf),0);
- break;
- case 's':
- case 'S':
- rbuf[3]-=num;
- send(cfd,rbuf,sizeof(rbuf),0);
- break;
- case 'd':
- case 'D':
- bbuf[3]+=num;
- send(cfd,bbuf,sizeof(bbuf),0);
- break;
- case 'a':
- case 'A':
- bbuf[3]-=num;
- send(cfd,bbuf,sizeof(bbuf),0);
- break;
- default:
- printf("错误操作\n");
- break;
- }
-
- }
-
- close(cfd);
-
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。