思维导图
控制机械臂
#include<head.h>
#define SER_PORT 8888
#define SER_IP "192.168.122.104"
#define CLI_PORT 6666
#define CLI_IP "192.168.122.113"
int main(int argc, const char *argv[])
{
//创建套接字
int cfd=-1;
cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd==-1)
{
perror("socket error");
return -1;
}
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;
}
//接收终端输入
char buf[128]="";
char s[5]={0xff,0x02,0x00,0,0xff};
char d[5]={0xff,0x02,0x01,0,0xff};
while(1)
{
bzero(buf,sizeof(buf));
printf("请输入:");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
if(strcmp(buf,"w")==0)
{
s[3]++;
send(cfd,s,sizeof(s),0);
}
if(strcmp(buf,"s")==0)
{
s[3]--;
send(cfd,s,sizeof(s),0);
}
if(strcmp(buf,"d")==0)
{
d[3]++;
send(cfd,d,sizeof(d),0);
}
if(strcmp(buf,"a")==0)
{
d[3]--;
send(cfd,d,sizeof(d),0);
}
if(strcmp(buf,"quit")==0)
{
break;
}
}
close(cfd);
//关闭文件
return 0;
}