网络编程——服务端、客户端搭建,机械臂操控

这是一个包含服务端、客户端和机械臂控制程序的示例。服务端创建并监听8888端口,接收客户端连接并进行数据交互。客户端连接到服务端,输入内容并发送,同时接收并显示服务端返回的数据。机械臂控制程序通过键盘输入控制机械臂的运动,根据按键发送特定指令到服务端来调整机械臂的角度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、服务端

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#include<arpa/inet.h>
#include<unistd.h>
#define ERR_MSG(msg) do{\
	fprintf(stderr,"_%d_:",__LINE__);\
	perror(msg);\
}while(0)
#define IP "192.168.31.48"
int main(int argc, const char *argv[])
{
	//创建流式套接字
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd<0)
	{
		ERR_MSG("socket");
		return -1;
	}
	//允许端口快速重用
	int reuse = 1;
	if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	//绑定服务器的IP和端口
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(8888);
	sin.sin_addr.s_addr=inet_addr(IP);

	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
	{
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success\n");

	//设置为被动监听状态
	if(listen(sfd,128)<0)
	{
		ERR_MSG("listen");
		return -1;
	}
	struct sockaddr_in cin;
	//获取新的文件描述符
	socklen_t addrlen=sizeof(cin);
	int newfd = accept (sfd,(struct sockaddr*)&cin,&addrlen);
	if(newfd <0)
	{
		ERR_MSG("accept");
		return -1;
	}
	printf("newfd = %d\n",newfd);

	char buf[128] = "";
	ssize_t res = 0;
	while(1)
	{
		bzero(buf,sizeof(buf));
		//接收
		res = recv(newfd,buf,sizeof(buf),0);
		if(res < 0)
		{
			ERR_MSG("recv");
			return -1;
		}
		else if(0 == res)
		{
			fprintf(stderr,"%d客户端关闭\n",newfd);
			break;
		}
		else
		{
			printf("%s",buf);

		}
		//发送
		strcat(buf,"*****");
		if(send(newfd,buf,sizeof(buf),0)<0)
		{
			ERR_MSG("send");
			return -1;
		}
	}
	close(newfd);
	close(sfd);
	return 0;
}

2、客户端

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#include<arpa/inet.h>
#include<unistd.h>
#define ERR_MSG(msg) do{\
	fprintf(stderr,"_%d_:",__LINE__);\
	perror(msg);\
}while(0)
#define IP "192.168.31.48"
int main(int argc, const char *argv[])
{
	//创建流式套接字
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd<0)
	{
		ERR_MSG("socket");
		return -1;
	}
 
	//绑定客户端的IP和端口
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(8888);
	sin.sin_addr.s_addr=inet_addr(IP);


	//连接服务器
	if(connect(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
	{
		ERR_MSG("connect");
		return -1;
	}

	char buf[128] = "";

	ssize_t res = 0;
	while(1)
	{
		bzero(buf,sizeof(buf));
		//发送
		printf("请输入要发送的内容:");
		fgets(buf,sizeof(buf),stdin);
		if(send(sfd,buf,sizeof(buf),0)<0)
		{
			ERR_MSG("send");
			return -1;
		}
		//接收
		res = recv(sfd,buf,sizeof(buf),0);
		if(res < 0)
		{
			ERR_MSG("recv");
			return -1;
		}
		else if(0 == res)
		{
			fprintf(stderr,"%d客户端关闭\n",sfd);
			break;
		}
		printf("%s\n",buf);


	}
	close(sfd);


	return 0;
}

3、机械臂的操控

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<linux/input.h>

#define ERR_MSG(msg) do{\
	fprintf(stderr,"_%d_:",__LINE__);\
	perror(msg);\
}while(0)

#define IP "192.168.31.89"

int main(int argc, const char *argv[])
{
	//创建流式套接字
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd<0)
	{
		ERR_MSG("socket");
		return -1;
	}
	//绑定IP和端口
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(8888);
	sin.sin_addr.s_addr=inet_addr(IP);

	//连接服务器
	if(connect(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
	{
		ERR_MSG("connect");
		return -1;
	}
	printf("connect success\n");

	//测试,读取键盘驱动文件
	int fd = open("/dev/input/event1",O_RDONLY);
	if(fd<0)
	{
		ERR_MSG("open");
		return -1;
	}
	struct input_event ev;
/*	while(1)
	{
		if(read(fd,&ev,sizeof(ev))<=0)
		{
			ERR_MSG("read");
			return -1;
		}
		printf("type = %-4d code = %-4d value=%-4d\n",ev.type,ev.code,ev.value);
	}*/

	char buf[5] = {0xff,0x02,0x01,90,0xff};
	unsigned char buf1[5] = {0xff,0x02,0x00,60,0xff};
	while(1)
	{
		if(read(fd,&ev,sizeof(ev))<=0)
		{
			ERR_MSG("read");
			return -1;
		}
		switch(ev.code*ev.value)
		{
		case 17://w
			{
				buf[3]=buf[3]+5;
				if(buf[3]>180)
				{
					buf[3]=180;
				}
				if(send(sfd,buf,sizeof(buf),0)<0)
				{
					ERR_MSG("send");
					return -1;
				}
				break;
			}
		case 31://s
			{
				buf[3]=buf[3]-5;
				if(buf[3]<0)
				{
					buf[3]=0;
				}
				if(send(sfd,buf,sizeof(buf),0)<0)
				{
					ERR_MSG("send");
					return -1;
				}
				break;
			}
		case 32://d
			{
				buf1[3]=buf1[3]+5;
				if(buf1[3]>90)
				{
					buf[3]=90;
				}
				if(send(sfd,buf1,sizeof(buf1),0)<0)
				{
					ERR_MSG("send");
					return -1;
				}
				break;
			}
		case 30://a
			{
				buf1[3]=buf1[3]-5;
				if(buf[3]<-90)
				{
					buf[3]=-90;
				}
				if(send(sfd,buf1,sizeof(buf1),0)<0)
				{
					ERR_MSG("send");
					return -1;
				}
				break;
			}
		}

	}
	close(sfd);


	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值