TCP协议服务器/客户端搭建

服务器
代码示例

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>

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

#define IP "192.168.31.114"

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和端口到地址信息结构体中,实际结构体是由地址族决定:
	//AF_INET -->man 7 ip
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;             //地址族必须指定为AF_INET
	sin.sin_port = htons(8888);           //端口号的网络字节序:1024~49151
	sin.sin_addr.s_addr = inet_addr(IP);  //ubuntude的本机IP:ifconfig查看

	//绑定服务器IP和端口,必须绑定
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
	{
		ERR_MSG("bind");
		return -1;
	}

	//将套接字设置为被动监听状态
	if(listen(sfd,10) < 0)
	{
		ERR_MSG("listen");
		return -1;
	}

	//接收客户端地址信息结构体
	struct sockaddr_in din;
	socklen_t addrlen = sizeof(din);

	//获取新的文件描述符,用于通信、交互
	int newfd = accept(sfd,(struct sockaddr*)&din,&addrlen);
	if(newfd < 0)
	{
		ERR_MSG("accept");
		return -1;
	}
	printf("%#X\n",din.sin_addr.s_addr);

	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,"newfd=%d客户端关闭\n",newfd);
			break;
		}

		printf("newfd=%d  %s\n",newfd,buf);
		
		//发送
		strcat(buf,"**");
		if(send(newfd,buf,sizeof(buf),0) < 0)
		{
			ERR_MSG("send");
			return -1;
		}
		printf("success\n");

	}

	close(newfd);
	close(sfd);
	return 0;
}

客户端
代码示例

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>

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

#define IP  "192.168.31.114"

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");

	char buf[128] = "";
	ssize_t res;

	while(1)
	{
		bzero(buf,sizeof(buf));
		printf("请输入>>>");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = 0;
		//发送
		if(send(sfd,buf,sizeof(buf),0) < 0)
		{
			ERR_MSG("send");
			return -1;
		}

		//接收
		bzero(buf,sizeof(buf));
		res = recv(sfd,buf,sizeof(buf),0);
		if(res < 0)
		{
			ERR_MSG("recv");
			return -1;
		}
		else if(0 == res)
		{
			fprintf(stderr,"服务器关闭\n");
			break;
		}
		printf("%s\n",buf);

	}

	close(sfd);
	return 0;
}

练习
wasd控制机械臂运动
代码示例

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

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

#define IP  "192.168.31.9"

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;

	char buf[5] = {0xff,0x02,0x00,0,0xff};
	unsigned char cuf[5] = {0xff,0x02,0x01,90,0xff};

	while(1)
	{
		if(read(fd,&ev,sizeof(ev)) <= 0)
		{
			ERR_MSG("read");
			return -1;
		}

		switch(ev.code*ev.value)
		{
		case 17:
			{
				buf[3] += 5;
				if(buf[3] > 90)
				{
					buf[3] = 90;
					continue;
				}
				if(send(sfd,buf,sizeof(buf),0) < 0)
				{
					ERR_MSG("send");
					return -1;
				}
				break;
			}
		case 31:
			{
				buf[3] -= 5;
				if(buf[3] < -90)
				{
					buf[3] = -90;
					continue;
				}
				if(send(sfd,buf,sizeof(buf),0) < 0)
				{
					ERR_MSG("send");
					return -1;
				}
				break;
			}
		case 32:
			{
				cuf[3] += 5;
				if(cuf[3] > 180)
				{
					cuf[3] = 180;
					continue;
				}
				if(send(sfd,cuf,sizeof(cuf),0) < 0)
				{
					ERR_MSG("send");
					return -1;
				}
				break;
			}
		case 30:
			{
				cuf[3] -= 5;
				if(cuf[3] < 0)
				{
					cuf[3] = 0;
					continue;
				}
				if(send(sfd,cuf,sizeof(cuf),0) < 0)
				{
					ERR_MSG("send");
					return -1;
				}
				break;
			}
		}
	}

	close(sfd);
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值