#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.120"//服务器IP
#define CLI_PORT 6666//客户端端口
#define CLI_IP "192.168.125.121"//客户端IP
int main(int argc, const char *argv[])
{
int cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd==-1){
perror("socket error\n");
return -1;
}
printf("cfd=%d\n",cfd);
struct sockaddr_in cin;
cin.sin_family=AF_INET;
cin.sin_port=htons(CLI_PORT);
cin.sin_addr.s_addr=inet_addr(CLI_IP);
if(bind(cfd,(struct sockaddr*)&cin,sizeof(cin))==-1){
perror("bind error\n");
return -1;
}
printf("bind success\n");
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\n");
return -1;
}
printf("connect success\n");
char rbuf[128]={0xff,0x02,0x00,0x00,0xff};
unsigned char bbuf[128]={0xff,0x02,0x01,0x00,0xff};
send(cfd,rbuf,sizeof(rbuf),0);
sleep(3);
send(cfd,bbuf,sizeof(bbuf),0);
char ctl=0;
while(1){
printf("input red arm operate:");
scanf("%hhx",&rbuf[3]);
getchar();
printf("input blue arm operate:");
scanf("%hhx",&bbuf[3]);
getchar();
send(cfd,rbuf,sizeof(rbuf),0);
sleep(3);
send(cfd,bbuf,sizeof(bbuf),0);
printf("operate成功\n");
}
close(cfd);
return 0;
}
关于操作机械臂的简单TCP通信实操
最新推荐文章于 2024-08-29 21:47:04 发布