作业:机械臂
#include <head.h>
#define PORT 8888
#define IP "192.168.125.31"
int main(int argc, const char *argv[])
{
int sfd = -1;
if((sfd=socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket error\n");
return -1;
}
//重置端口
int reuse = -1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1)
{
perror("setsockopt error\n");
return -1;
}
//定义结构体绑定地址和端口号
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
if(connect(sfd, (struct sockaddr*)&sin, sizeof(sin)) == -1)
{
perror("connect error\n");
return -1;
}
printf("已连接服务器\n");
char buf[5] = {0xff, 0x02, 0x00, 0x00, 0xff};
unsigned char bbuf[5] = {0xff, 0x02, 0x01, 0x5a, 0xff};
char value;
while(1)
{
value = getchar();
switch(value)
{
case 'W':
case 'w':
{
buf[3] += 2;
if(buf[3]>=90)
{
buf[3]=90;
}
}
break;
case 'S':
case 's':
{
buf[3] -= 2;
if(buf[3]<=-90)
{
buf[3]=-90;
}
}
break;
case 'A':
case 'a':
{
bbuf[3] += 2;
if(bbuf[3]>=180)
{
bbuf[3]=180;
}
}
break;
case 'D':
case 'd':
{
bbuf[3] -= 2;
if(bbuf[3]<=0)
{
bbuf[3] = 0;
}
}
break;
default:
break;
}
if(value == 'W'||value=='w'||value=='S'||value=='s')
{
send(sfd, buf, sizeof(buf), 0);
}
else if(value=='A'||value=='a'||value=='D'||value=='d')
{
send(sfd, bbuf, sizeof(bbuf), 0);
}
}
close(sfd);
return 0;
}
有一点点瑕疵