TCP/UDP(3/13)

TCP服务器 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define ERR_MSG(msg) do{\
	fprintf(stderr,"__%d__",__LINE__);\
	perror(msg);\
}while(0)
#define PORT 2222
#define IP "192.168.31.241"
int main(int argc, const char *argv[])
{
	//创建流式套接字>>TCP
	int sfd=socket(AF_INET,SOCK_STREAM,0);
	if(sfd < 0)
	{
		perror("sfd");
		return -1;
	}
	printf("sfd=%d\n",sfd);
	//允许端口快速被复用
	int reuse=1;
	if( setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0 )
	{
		perror("setsocket");
		return -1;
	}
	printf("setsocket success\n");
	//填充地址信息结构体,真实的地址结构体在man 7 ip
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;//必须填这个  ipv4
	sin.sin_port = htons(PORT);//
	sin.sin_addr.s_addr = inet_addr(IP);
	//将IP地址和端口号绑定到指定的套接字中
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
	{
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success\n");
	//将套接字设置为被动监听状态,只负责监听是否有客户端连接成功
	if(listen(sfd,10) < 0)
	{
		ERR_MSG("listen");
		return -1;
	}
	printf("listen success\n");
	//新结构体储存客户端数据
	struct sockaddr_in cin;
	socklen_t addrlen=sizeof(cin);
	int newfd =0;
	newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
	if(newfd < 0)
	{
		ERR_MSG("accept");
		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));
		res=recv(newfd,buf,sizeof(buf),0);
		if(res==0)
		{
			printf("断开连接\n");
			return -1;
		}
		else if(res<0)
		{
			ERR_MSG("recv");
			return -1;
		}
		printf("[%s | %d] newfd=%d:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);
		//发送
		strcat(buf,"*_*");
		if(send(newfd,buf,sizeof(buf),0) < 0)
		{
			ERR_MSG("send");
			return -1;
		}
		printf("send success\n");
	}
	close(newfd);
	close(sfd);
	return 0;
}

TCP客户端

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define ERR_MSG(msg) do{\
	fprintf(stderr,"__%d__",__LINE__);\
	perror(msg);\
}while(0)
#define PORT 2222
#define IP "192.168.31.241"
#define PORT1 2222
#define IP1 "192.168.31.241"
int main(int argc, const char *argv[])
{
	//创建流式套接字>>TCP
	int sfd=socket(AF_INET,SOCK_STREAM,0);
	if(sfd < 0)
	{
		perror("sfd");
		return -1;
	}
	printf("sfd=%d\n",sfd);
	//填充地址信息结构体,真实的地址结构体在man 7 ip
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORT);
	sin.sin_addr.s_addr = inet_addr(IP);
	//将IP地址和端口号绑定到指定的套接字中
	/*if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
	{
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success\n");*/
	//连接服务器
	if(connect(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
	{
		ERR_MSG("connect");
		return -1;
	}
	printf("connect success\n");

	char buf[128]="";
	ssize_t res=0;
	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;
	}
	printf("send success\n");
	//接受服务器返回的数据
	bzero(buf,sizeof(buf));
	res=recv(sfd,buf,sizeof(buf),0);
	if(res==0)
	{
	printf("断开连接\n");
	return -1;
	}
	else if(res<0)
	{
		ERR_MSG("recv");
		return -1;
	}
	printf(":%s\n",buf);
//	printf("[%s | %d] newfd=%d:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);
	}
	close(sfd);
	return 0;
}

UDP服务器

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define ERR_MSG(msg) do{\
	fprintf(stderr,"__%d__",__LINE__);\
	perror(msg);\
}while(0)
#define PORT 2222
#define IP "192.168.31.241"
int main(int argc, const char *argv[])
{
	//创建报式套接字
	int sfd=socket(AF_INET,SOCK_DGRAM,0);
	if(sfd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("%d\n",sfd);
	//结构体
	struct sockaddr_in sin;
	sin.sin_family  =AF_INET;
	sin.sin_port    =htons(PORT);
	sin.sin_addr.s_addr=inet_addr(IP);
	printf("sin success\n");
	//绑定  必须
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
	{
	ERR_MSG("bind");
	return -1;
	}

	char buf[128]="";
	ssize_t res=0;
	struct sockaddr_in cin;
	socklen_t addlen=sizeof(cin);
	while(1)
	{
		//接收数据
		bzero(buf,sizeof(buf));
		res=recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr *)&cin,&addlen);
		if(res<0)
		{
			ERR_MSG("recvfron");
			return -1;
		}
		printf("[%s | %d] %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
		
		//发送数据
		strcat(buf,"*_*");
		if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr *)&cin,sizeof(cin)) < 0)
		{
			ERR_MSG("sendto");
			return -1;
		}
		printf("sendto success\n");
	}
	return 0;
}

UDP客户端

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define ERR_MSG(msg) do{\
	fprintf(stderr,"__%d__",__LINE__);\
	perror(msg);\
}while(0)
#define PORT 2222
#define IP "192.168.31.241"
int main(int argc, const char *argv[])
{
	//创建报式套接字
	int sfd=socket(AF_INET,SOCK_DGRAM,0);
	if(sfd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("%d\n",sfd);
	//结构体
	struct sockaddr_in sin;
	sin.sin_family  =AF_INET;
	sin.sin_port    =htons(PORT);
	sin.sin_addr.s_addr=inet_addr(IP);
	printf("sin success\n");
	/*
	//绑定 
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
	{
	ERR_MSG("bind");
	return -1;
	}*/

	char buf[128]="";
	ssize_t res=0;
	socklen_t addrlen=sizeof(sin);
	while(1)
	{
			
		//发送数据
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1]='\0';
		if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr *)&sin,sizeof(sin)) < 0)
		{
			ERR_MSG("sendto");
			return -1;
		}
		printf("sendto success\n");
	
		//接收数据
		bzero(buf,sizeof(buf));
		res=recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr *)&sin,&addrlen);
		if(res<0)
		{
			ERR_MSG("recvfron");
			return -1;
		}
		printf("[%s | %d] %s\n",inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),buf);
	}
	return 0;
}

你可能感兴趣的:(tcp/ip,udp,网络)