TCP机械臂测试
#include<myhead.h>
#define SER_PORT 8888 //与服务器保持一致
#define SER_IP "192.168.0.147" //服务器ip地址
#define CLI_PORT 6666 //客户端端口号
#define CLI_IP "192.168.0.130" //客户端ip地址
int main(int argc, const char *argv[])
{
//1、创建用于通信的套接字文件描述符
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if(cfd == -1)
{
perror("socket error");
return -1;
}
printf("cfd = %d\n", cfd); //3
//3、连接到服务器
//3.1 填充服务器地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET; //通信域
sin.sin_port = htons(SER_PORT); //服务器端口号
sin.sin_addr.s_addr = inet_addr(SER_IP); //服务器ip地址
//3.2 连接服务器
if(connect(cfd, (struct sockaddr*)&sin, sizeof(sin)) == -1)
{
perror("connect error");
return -1;
}
printf("连接服务器成功\n");
//准备数据
char rbuf[5] = {0xff, 0x02, 0x00, 0x00, 0xff};
char bbuf[5] = {0xff, 0x02, 0x01, 0x00, 0xff};
char op;
//w红色增加 s红色减小 a蓝色增加 d蓝色减小
send(cfd, rbuf,sizeof(bbuf), 0);
send(cfd, bbuf,sizeof(bbuf), 0);
while(1)
{
scanf("%c",&op);
getchar();
if(op == 'w')
{
rbuf[3] = rbuf[3] + 5;
send(cfd, rbuf,sizeof(bbuf), 0);
}
else if(op == 's')
{
rbuf[3] = rbuf[3] - 5;
send(cfd, rbuf,sizeof(bbuf), 0);
}
else if(op == 'a')
{
bbuf[3] = bbuf[3] + 5;
send(cfd, bbuf,sizeof(bbuf), 0);
}
else if(op == 'd')
{
bbuf[3] = bbuf[3] - 5;
send(cfd, bbuf,sizeof(bbuf), 0);
}
else
{
printf("input error\n");
getchar();
}
}
//5、关闭套接字
close(cfd);
return 0;
}