网络编程-day2

TCP服务器

#include 	

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

#define PORT 8888      //端口号,范围1024~49151
#define IP   "192.168.43.126"    //本机IP   

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\n", sfd);

	//允许端口快速的被复用
	int reuse = 1;                                                            
    if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    {
        ERR_MSG("setsockopt");
        return -1;
    }
    printf("允许端口被复用成功\n");



	//填充地址信息结构体给bind函数绑定
	//真实的地址信息结构体根据地址族制定AF_INET:man 7 ip
	struct sockaddr_in sin;
	sin.sin_family        = AF_INET;       //必须填AF_INET
	sin.sin_port          = htons(PORT);   //端口号的网络字节序
	sin.sin_addr.s_addr   = inet_addr(IP);   //本机IP

	//绑定服务器的地址信息 -->必须绑定 bind
	if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
	{
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success\n");

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

	int newfd = -1;
	struct sockaddr_in cin;    //存储客户端的地址信息
	socklen_t addrlen = sizeof(cin);   //真实的地址信息结构体大小

	//获取一个已经完成的客户端信息,生成一个新的文件描述符 accept
	//newfd = accept(sfd, NULL, NULL);
	newfd = accept(sfd, (struct sockaddr*)&cin, &addrlen);
	if(newfd < 0)
	{
		ERR_MSG("newfd");
		return -1;
	}
	printf("[%s:%d]客户端连接成功 newfd=%d\n",\
			inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd);


	char buf[128] = "";
	ssize_t res = 0;
	while(1)
	{
		//清空字符串
		bzero(buf, sizeof(buf));  //也可以用memset
		//接收
		res = recv(newfd, buf, sizeof(buf), 0);
		if(res < 0)
		{
			ERR_MSG("recv");
			return -1;
		}else if(0 == res)
		{
			printf("[%s:%d]客户端下线 newfd=%d\n",\
			inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd);
			break;
		}
		printf("[%s:%d] newfd=%d :%s\n", \
				inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, buf);

		//发送
		strcat(buf, "O.o");
		if(send(newfd, buf, sizeof(buf), 0) < 0)
		{
			ERR_MSG("send");
			return -1;
		}
		printf("send success\n");

	
	}
	//关闭文件描述符
	if(close(sfd) < 0)
	{
		ERR_MSG("close");
		return -1;
	}

	return 0;
}

TCP客户端

#include 	

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

#define PORT 8888      //端口号,范围1024~49151
#define IP   "192.168.116.74"    //本机IP    

int main(int argc, const char *argv[])
{
	//创建流式套接字 socket
	int cfd = socket(AF_INET, SOCK_STREAM, 0);
	if(cfd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("socket create success cfd=%d\n", cfd);
	
	//连接指定服务器 connect
	
	char buf[128] = "";
	ssize_t res = 0;
	while(1)
	{
		//清空字符串
		bzero(buf, sizeof(buf));  //也可以用memset
		printf("请输入>>> ");
		fgets(buf ,sizeof(buf), stdin);
		buf[strlen(buf)-1] = 0;

		//发送
		strcat(buf, "O.o");
		if(send(cfd, buf, sizeof(buf), 0) < 0)
		{
			ERR_MSG("send");
			return -1;
		}
		printf("send success\n");

		//接收
		res = recv(cfd, buf, sizeof(buf), 0);
		if(res < 0)
		{
			ERR_MSG("recv");
			return -1;
		}else if(0 == res)
		{
			printf("服务器下线 cfd=%d\n", cfd);
			break;
		}
		printf("cfd=%d : %s\n", cfd, buf);
	
	}
	//关闭文件描述符
	if(close(cfd) < 0)
	{
		ERR_MSG("close");
		return -1;
	}

	return 0;
}

UDP服务器

#include 	

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

#define PORT 8888      //端口号,范围1024~49151
#define IP   "192.168.116.74"    //本机IP  

int main(int argc, const char *argv[])
{
	//创建报式套接字 socket
	int sfd = socket(AF_INET, SOCK_DGRAM, 0);
	if(sfd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("socket create success sfd=%d\n", sfd);

	//填充服务器的地址信息结构体,给bind函数使用
	//真实的地址信息结构体根据地址族指定,AF_INET:man 7 IP
	struct sockaddr_in sin;
	sin.sin_family 			= AF_INET; 		//必须填AF_INET
	sin.sin_port 			= htons(PORT); 	//端口号的网络字节序,1024-49151
	sin.sin_addr.s_addr 	= inet_addr(IP);//本机IP,ifconfig

	//绑定地址信息结构体--->bind
	if(bind(sfd,(struct sockaddr*)&sin, sizeof(sin)) < 0)
	{
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success\n");
	
	char buf[128] = "";
	struct sockaddr_in cin;    //存储发送方的地址信息
	socklen_t addrlen = sizeof(cin);

	while(1)
	{
		bzero(buf, sizeof(buf));
		
		//接收数据

		if(recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, &addrlen) < 0)
		{
			ERR_MSG("recv");
			return -1;
		}
		printf("[%s:%d] : %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port),buf);

		//发送数据--->谁发还谁
		strcat(buf, "O.o");
		if(sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, sizeof(cin)) < 0)
		{
			ERR_MSG("sendto");
			return -1;
		}
		printf("sendto success\n");
	}

	//关闭套接字
	close(sfd);

	return 0;
}

UDP客户端

#include 	

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

#define PORT 8888      //端口号,范围1024~49151
#define IP   "192.168.116.74"    //本机IP,ifconfig    

int main(int argc, const char *argv[])
{
	//创建报式套接字 socket
	int cfd = socket(AF_INET, SOCK_DGRAM, 0);
	if(cfd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("socket create success cfd=%d\n", cfd);

	//填充服务器的地址信息结构体,给bind函数使用
	//真实的地址信息结构体根据地址族指定,AF_INET:man 7 IP
	struct sockaddr_in sin;
	sin.sin_family 			= AF_INET; 		//必须填AF_INET
	sin.sin_port 			= htons(PORT); 	//端口号的网络字节序,1024-49151
	sin.sin_addr.s_addr 	= inet_addr(IP);//本机IP,ifconfig
	
	char buf[128] = "";

	while(1)
	{
		bzero(buf, sizeof(buf));
		printf("请输入>>>");
		fgets(buf, sizeof(buf), stdin);
		buf[strlen(buf)-1] = 0;
		
		//发送数据
		if(sendto(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, sizeof(sin)) < 0)
		{
			ERR_MSG("sendto");
			return -1;
		}
		printf("sendto success\n");

		//接收数据
		if(recvfrom(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, NULL) < 0)
		{
			ERR_MSG("recv");
			return -1;
		}
		printf("[cfd=%d : %s\n", cfd,buf);

	}

	//关闭套接字
	close(cfd);

	return 0;
}

思维导图

 

你可能感兴趣的:(作业,网络)