华清远见作业第二十七天——网络编程(第二天)

思维导图:

在虚拟机实现客户端控制机械臂

代码:

cpp 复制代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <a.h>
#define SER_PORT 8888 //服务端口
#define SER_IP "192.168.125.4" //服务器ip地址
#define CLI_PORT 6666// 客户端的端口
#define CLI_IP "192.168.125.229"  //客户端的ip地址
void menu()
{
	printf("*********机械臂操作*********\n");
	printf("*      w增大红色角度       *\n");
	printf("*      s减少红色角度       *\n");
	printf("*      d增大蓝色角度       *\n");
	printf("*      a减少蓝色角度       *\n");
	printf("*      q退出机械臂q        *\n");
	printf("*********机械臂操作*********\n");	
}
int main(int argc, const char *argv[])
{
	//1创建用于通信的套接字文件描述符
	int cfd=-1;
	cfd=socket(AF_INET,SOCK_STREAM,0);
	if(cfd==-1)
	{
		perror("socket error");
		return -1;
	}
	printf("cfd=%d\n",cfd);
	//将端口号快速重用函数
	int reuse=1;
	if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1)
	{
		perror("setsockopt error");
		return -1;
	}
	printf("端口号快速重用成功\n");
	//2绑定
	//2.1填充地址信息结构体
	struct sockaddr_in cin;
	cin.sin_family=AF_INET;
	cin.sin_port=htons(CLI_PORT);
	cin.sin_addr.s_addr=inet_addr(CLI_IP);
	//2.2
	if(bind(cfd,(struct sockaddr*)&cin,sizeof(cin))==-1)
	{
		perror("bind error");
		return -1;
	}
	printf("bind success\n");
	//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);
	//3.2连接 
	if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
	{
		perror("connect error");
		return -1;
	}
	printf("connect success\n");
	//4收发数据
	char rbuf[5]={0xff,0x02,0x00,0x01,0xff}; //红色
	char bbuf[5]={0xff,0x02,0x01,0x02,0xff}; //蓝色
	char key=0;
	int s=1;  //控制while循环
	while(s==1)
	{
		//从终端获取按键

		menu();
		printf("请输入相关操作:");
		scanf("%c",&key);
		getchar();
		//从输入的字节符进行分支选择
		switch(key)
		{
			case 'w':
			case 'W':
				{
					rbuf[3]+=2;
					if(rbuf[3]>=90)
					{
						rbuf[3]=90;
					}
					//发送数据
					int c=send(cfd,rbuf,sizeof(rbuf),0);
					if(c==-1)
					{
						perror("send error");
						return -1;
					}
					break;
					
				}
			case 's':
			case 'S':
				{
					rbuf[3]-=2;
					if(rbuf[3]<=-90)
					{
						rbuf[3]=-90;
					}
					//发送数据
					int c=send(cfd,rbuf,sizeof(rbuf),0);
					if(c==-1)
					{
						perror("send error");
						return -1;
					}
					break;
					
				}
			case 'd':
			case 'D':
				{
					bbuf[3]+=2;
					if(bbuf[3]>=180)
					{
						rbuf[3]=180;
					}
					//发送数据
					int c=send(cfd,bbuf,sizeof(bbuf),0);
					if(c==-1)
					{
						perror("send error");
						return -1;
					}
					break;
					
				}
			case 'a':
			case 'A':
				{
					bbuf[3]=bbuf[3]-2;
					if(bbuf[3]<=0)
					{
						bbuf[3]=0;
					}
					printf("%d\n",bbuf[3]);
					//发送数据
					int c=send(cfd,bbuf,sizeof(bbuf),0);
					if(c==-1)
					{
						perror("send error");
						return -1;
					}
					break;
					
				}
			case 'q':
			case 'Q':
				{
					s=0;
					break; //结束循环
					
				}
			default:
				{
					printf("内容输入错误请重新输入\n");
					break;
				}
						
		}
		
	}
	//5关闭套接字
	close(cfd);
	return 0;
}

运行效果:

机械臂资源:

https://download.csdn.net/download/m0_62462327/88746050?spm=1001.2014.3001.5501

相关推荐
傻乐u兔11 小时前
C语言进阶————指针4
c语言·开发语言
历程里程碑11 小时前
Linux22 文件系统
linux·运维·c语言·开发语言·数据结构·c++·算法
2601_9491465318 小时前
C语言语音通知接口接入教程:如何使用C语言直接调用语音预警API
c语言·开发语言
知南x20 小时前
【Ascend C系列课程(高级)】(1) 算子调试+调优
c语言·开发语言
2的n次方_1 天前
Runtime 执行提交机制:NPU 硬件队列的管理与任务原子化下发
c语言·开发语言
凡人叶枫1 天前
C++中智能指针详解(Linux实战版)| 彻底解决内存泄漏,新手也能吃透
java·linux·c语言·开发语言·c++·嵌入式开发
凡人叶枫1 天前
C++中输入、输出和文件操作详解(Linux实战版)| 从基础到项目落地,避坑指南
linux·服务器·c语言·开发语言·c++
傻乐u兔1 天前
C语言进阶————指针3
c语言·开发语言
CodeSheep程序羊1 天前
拼多多春节加班工资曝光,没几个敢给这个数的。
java·c语言·开发语言·c++·python·程序人生·职场和发展
I'mChloe1 天前
PTO-ISA 深度解析:PyPTO 范式生成的底层指令集与 NPU 算子执行的硬件映射
c语言·开发语言