2025.01.01(IO模型分类,超时检测,抓包分析,机械臂客户端)
作业:基于机械臂服务器写出客户端代码并执行
#include <myhead.h>
#define IP "192.168.124.62"
#define PORT 8888
int main(int argc, const char *argv[])
{
//1.创建套接字
int cfd = socket(AF_INET,SOCK_STREAM,0);
if(cfd==-1)
{
perror("socket");
return -1;
}
//2.绑定
//3.连接
//3-1.填充地址信息结构体
struct sockaddr_in aaa;
aaa.sin_family = AF_INET;
aaa.sin_port = htons(PORT);
aaa.sin_addr.s_addr = inet_addr(IP);
//3-2.连接服务器
if(connect(cfd,(struct sockaddr *)&aaa,sizeof(aaa))==-1)
{
perror("connect");
return -1;
}
//4.向服务器发送消息
char rbuf[5] = {0xff,0x02,0x00,-0x5A,0xff}; //红色臂
unsigned char bbuf[5] = {0xff,0x02,0x01,0x00,0xff}; //蓝色臂
//先发送初始数据
send(cfd,rbuf,sizeof(rbuf),0);
sleep(1);
send(cfd,bbuf,sizeof(bbuf),0);
//通过键盘控制数据的收发
char bbb;
while(1)
{
scanf("%c",&bbb);
while(getchar()!='\n');
switch(bbb)
{
//红色臂
case 'w':
rbuf[3] += 5; //增加5度
if(rbuf[3]>=90)
{
rbuf[3] = 90;
}
send(cfd,rbuf,sizeof(rbuf),0);
break;
case 'a':
rbuf[3] -= 5; //减少5度
if(rbuf[3]<=-90)
{
rbuf[3] = -90;
}
send(cfd,rbuf,sizeof(rbuf),0);
break;
//蓝色臂
case 's':
bbuf[3] += 5; //增加5度
if(bbuf[3] > 180)
{
bbuf[3] = 180;
}
send(cfd,bbuf,sizeof(bbuf),0);
break;
case 'd':
bbuf[3] -= 5; //减少5度
if(bbuf[3] < 0)
{
bbuf[3] = 0;
}
send(cfd,bbuf,sizeof(bbuf),0);
break;
case 'q': //初始化
rbuf[3] = -90;
bbuf[3] = 0;
send(cfd,rbuf,sizeof(rbuf),0);
send(cfd,bbuf,sizeof(bbuf),0);
break;
}
}
return 0;
}
思维导图
域套接字
io模型分类
超时分析
抓包分析