#include <head.h>
#define SER_PORT 8888 //服务器端口号
#define SER_IP "192.168.115.9"//服务器IP地址
#define CLI_PORT 9999
#define CLI_IP "192.168.115.10"
int main(int argc, const char *argv[])
{
int cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd==-1)
{
perror("socket");
return -1;
}
int reuse=1;
if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1)
{
perror("setsockopt");
return -1;
}
printf("端口快速重用\n");
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(CLI_PORT);
sin.sin_addr.s_addr=inet_addr(CLI_IP);
if(bind(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("bind");
return -1;
}
struct sockaddr_in cin;
cin.sin_family=AF_INET;
cin.sin_port=htons(SER_PORT);
cin.sin_addr.s_addr=inet_addr(SER_IP);
if(connect(cfd,(struct sockaddr*)&cin,sizeof(cin))==-1)
{
perror("connect");
return -1;
}
char buf;
char red[5]={0xff,0x02,0x00,0x00,0xff};
char bul[5]={0xff,0x02,0x01,0x00,0xff};
while(1)
{
printf("输入控制键:");
scanf("%c",&buf);
getchar();
if(buf=='s')
{
red[3]-=0x01;
send(cfd,red,sizeof(red),0);
}else if(buf=='w')
{
red[3]+=0x01;
send(cfd,red,sizeof(red),0);
}else if(buf=='d')
{
bul[3]+=0x01;
send(cfd,bul,sizeof(bul),0);
}else if(buf=='a')
{
bul[3]-=0x01;
send(cfd,bul,sizeof(bul),0);
}else{
printf("退出\n");
break;
}
}
close(cfd);
return 0;
}
TCP客户端设置控制机械臂
最新推荐文章于 2024-04-23 19:50:18 发布