#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>
#include <netdb.h>
#define SER_PORT 8888
#define SER_IP "192.168.117.156"
#define CLI_PORT 6668
#define CLI_IP "192.168.117.87"
int main(int argc, char const *argv[])
{
int cfd = socket(AF_INET, SOCK_STREAM, 0);//套接字
if(cfd == -1){
perror("socket");
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 sockadrr*)&cin, sizeof(cin)) == -1){
perror("bind");
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");
return -1;
}
printf("Connect success\n");
long int c=0;
long int d=0;
char buf[102] = {0xff,0x02,0x00,c,0xff};
unsigned char str[102] = {0xff,0x02,0x01,d,0xff};
while(1){
printf("请输入w/s:\n");
int key = getchar();
if(key == 'w'){
send(cfd,buf,sizeof(buf),0);
printf("请输入角度,十六进制\n");
scanf("%ld",&c);
while(getchar() != '\n');
}else if(key == 's'){
send(cfd,str,sizeof(str),0);
printf("请输入角度,十六进制\n");
scanf("%ld",&d);
while(getchar() != '\n');
}
}
close(cfd);
return 0;
}
20240520机械臂
最新推荐文章于 2024-10-01 22:54:40 发布