#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <termios.h>
#define SER_IP “192.168.1.7” // 服务器IP地址
#define SER_PORT 8888 // 服务器端口号
#define CLI_IP “192.168.1.11” // 客户端IP地址
#define CLI_PORT 7777 // 客户端端口号
// 设置终端为非阻塞模式
void set_nonblocking_mode() {
struct termios ttystate;
tcgetattr(STDIN_FILENO, &ttystate);
ttystate.c_lflag &= ~(ICANON | ECHO); // 禁用规范模式和回显
ttystate.c_cc[VMIN] = 1;
ttystate.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
}
// 恢复终端设置
void reset_terminal_mode() {
struct termios ttystate;
tcgetattr(STDIN_FILENO, &ttystate);
ttystate.c_lflag |= (ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
}
int main(int argc, const char *argv[])
{
// 1、创建用于通信的套接字文件描述符
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if(cfd == -1)
{
perror(“socket error”);
return -1;
}
// 2、给客户端套接字绑定IP地址和端口号(可选)
// 2.1 填充地址信息结构体
struct sockaddr_in cin;
cin.sin_family = AF_INET;
cin.sin_addr.s_addr = inet_addr(CLI_IP);
cin.sin_port = htons(CLI_PORT);
// 2.2 绑定
if(bind(cfd, (struct sockaddr*)&cin, sizeof(cin)) == -1)
{
perror("bind error");
close(cfd);
return -1;
}
// 3、连接服务器
// 3.1 组装对端地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET; // 通信域
sin.sin_addr.s_addr = inet_addr(SER_IP); // 要连接的服务器IP
sin.sin_port = htons(SER_PORT); // 服务器的端口号
// 3.2 连接操作
if(connect(cfd, (struct sockaddr*)&sin, sizeof(sin)) == -1)
{
perror("connect error");
close(cfd);
return -1;
}
printf("Connected to server %s:%d\n", SER_IP, SER_PORT);
printf("Controls:\n");
printf(" w - Increase red arm angle\n");
printf(" s - Decrease red arm angle\n");
printf(" d - Increase blue arm angle\n");
printf(" a - Decrease blue arm angle\n");
printf(" q - Quit\n");
// 设置终端为非阻塞模式
set_nonblocking_mode();
// 准备数据包
unsigned char rbuf[5] = {0xff, 0x02, 0x00, 0x10, 0xff}; // 红色摆臂初始角度
unsigned char bbuf[5] = {0xff, 0x02, 0x01, 0x10, 0xff}; // 蓝色摆臂初始角度
// 发送初始位置
send(cfd, rbuf, sizeof(rbuf), 0);
send(cfd, bbuf, sizeof(bbuf), 0);
int red_angle = 0x10; // 红色臂初始角度
int blue_angle = 0x10; // 蓝色臂初始角度
char input;
int running = 1;
while(running)
{
// 检查键盘输入
if(read(STDIN_FILENO, &input, 1) > 0)
{
switch(input)
{
case 'w': // 红色臂角度增大
if(red_angle < 0x64) {
red_angle += 1;
rbuf[3] = red_angle;
send(cfd, rbuf, sizeof(rbuf), 0);
printf("Red arm angle increased to: %d\n", red_angle);
}
break;
case 's': // 红色臂角度减小
if(red_angle > 0x00) {
red_angle -= 1;
rbuf[3] = red_angle;
send(cfd, rbuf, sizeof(rbuf), 0);
printf("Red arm angle decreased to: %d\n", red_angle);
}
break;
case 'd': // 蓝色臂角度增大
if(blue_angle < 0x64) {
blue_angle += 1;
bbuf[3] = blue_angle;
send(cfd, bbuf, sizeof(bbuf), 0);
printf("Blue arm angle increased to: %d\n", blue_angle);
}
break;
case 'a': // 蓝色臂角度减小
if(blue_angle > 0x00) {
blue_angle -= 1;
bbuf[3] = blue_angle;
send(cfd, bbuf, sizeof(bbuf), 0);
printf("Blue arm angle decreased to: %d\n", blue_angle);
}
break;
case 'q': // 退出
running = 0;
printf("Quitting...\n");
break;
default:
break;
}
}
// 短暂休眠以减少CPU占用
usleep(10000); // 10ms
}
// 恢复终端设置
reset_terminal_mode();
// 5、关闭套接字
close(cfd);
printf("Connection closed.\n");
return 0;
}
110

被折叠的 条评论
为什么被折叠?



