网络编程第五天

2023-12-26 19:46:56
服务器代码
#include "../head.h"
#define PORT 8888
#define IP "192.168.124.29"

int main(int argc, const char *argv[])
{
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("socket create success sfd=%d",sfd);
	LINE;
	int reuse = 1;
	if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	printf("允许端口快速修复成功\n");
	struct sockaddr_in sin;
	sin.sin_family 		= AF_INET;
	sin.sin_port 		= htons(PORT);
	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");
	LINE;
	if(listen(sfd,128) < 0)
	{
		ERR_MSG("listen");
		return -1;
	}
	printf("listen success");
	LINE;

	fd_set readfds,tempfds;
	FD_ZERO(&readfds);
	FD_SET(0,&readfds);
	FD_SET(sfd,&readfds);
	int s_res = 0;
	int maxfd = sfd;
	char buf[128] = "";
	ssize_t res = 0;
	int newfd;
	struct sockaddr_in cin;
	socklen_t addrlen = sizeof(cin);
	while(1)
	{
		tempfds = readfds;
		s_res = select(maxfd+1,&tempfds,NULL,NULL,NULL);
		if(s_res < 0)
		{
			ERR_MSG("select");
			return -1;
		}
		else if(0 == s_res)
		{
			printf("time out\n");
			break;
		}
		for(int i=0;i<=maxfd;i++)
		{
			if(FD_ISSET(i,&tempfds) == 0)
			{
				continue;
			}
			if(0 == i)
			{
				printf("触发键盘输入事件\n");
				fgets(buf,sizeof(buf),stdin);
				buf[strlen(buf)-1] = '\0';
				printf("buf=%s\n",buf);
			}
			else if(i == sfd)
			{
				printf("触发客户端链接事件\n");
				newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
				if(newfd < 0)
				{
					ERR_MSG("accept");
					return -1;
				}
				printf("[%s : %d] newfd=%d 客户端链接成功",\
						inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
				LINE;
				FD_SET(newfd,&readfds);
				maxfd = maxfd>newfd ? maxfd:newfd;

			}

			else
			{
				printf("触发客户端交互事件\n");
				bzero(buf,sizeof(buf));
				res = recv(i,buf,sizeof(buf),0);
				if(res < 0)
				{
					ERR_MSG("recv");
					return -1;
				}
				else if(0 == res)
				{
					printf("[%s : %d] newfd=%d 客户端下线",\
							inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),i);
					LINE;
					FD_CLR(i,&readfds);
					continue;
				}

				printf("[%s : %d] newfd=%d 说: %s",\
						inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),i,buf);
				LINE;
				strcat(buf,"^_^");
				if(send(i,buf,sizeof(buf),0) < 0)
				{
					ERR_MSG("send");
					return -1;
				}
				printf("send success");
				LINE;
			}
		}

	}
	return 0;
}
客户端代码
#include "../head.h"
#define IP "192.168.124.29"
#define PORT 8888
int main(int argc, const char *argv[])
{
	int cfd = socket(AF_INET,SOCK_STREAM,0);
	if(cfd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("socket create success cfd=%d ",cfd);
	LINE;
	struct sockaddr_in sin;
	sin.sin_family 		=AF_INET;
	sin.sin_port 		= htons(PORT);
	sin.sin_addr.s_addr = inet_addr(IP);
	if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
	{
		ERR_MSG("connect");
		return -1;
	}
	printf("connect success ");
	LINE;
	char buf[128] = "";
	ssize_t res = 0;
	fd_set readfds,tempfds;
	FD_ZERO(&readfds);
	FD_SET(0,&readfds);
	FD_SET(cfd,&readfds);
	int s_res = 0;
	while(1)
	{
		tempfds = readfds;
		s_res = select(cfd+1,&tempfds,NULL,NULL,NULL);
		if(FD_ISSET(0,&tempfds))
		{

			fgets(buf,sizeof(buf),stdin);
			buf[strlen(buf)-1] = '\0';
			if(send(cfd,buf,sizeof(buf),0) < 0)
			{
				ERR_MSG("send");
				return -1;
			}
			printf("send success");
			LINE;

		}
		if(FD_ISSET(cfd,&tempfds))
		{

			bzero(buf,sizeof(buf));
			res = recv(cfd,buf,sizeof(buf),0);
			if(res < 0)
			{
				ERR_MSG("recv");
				return -1;
			}
			else if(0 == res)
			{
				printf("服务器已下线 ");
				LINE;
				printf("%s : %d\n",inet_ntoa(sin.sin_addr),ntohs(sin.sin_port));
				break;
			}
			printf("%s : %d 说:",inet_ntoa(sin.sin_addr),ntohs(sin.sin_port));
			printf("%s\n",buf);
			LINE;
		}
	}
	if(close(cfd) < 0)
	{
		ERR_MSG("close");
		return -1;
	}

	return 0;
}

文章来源:https://blog.csdn.net/hahhsssds/article/details/135228418
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。