网络编程day2.c

TCP 服务器
#include 
 
#define PORT 8888
#define IP "192.168.125.60"
 
#define ERR_MSG(msg) 	do{\
		fprintf(stderr,"__%d__",__LINE__);\
		perror(msg);\
	}while(0)
 
int main(int argc, const char *argv[])
{
	//创建流式套接字 socket
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd == -1){
		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
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORT);
	sin.sin_addr.s_addr = inet_addr(IP);
 
	//绑定服务器的地址信息---->必须绑定  bind
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) == -1){
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success!\n");
 
	//将套接字设置为监听状态 listen
	if(listen(sfd,128) == -1){
		ERR_MSG("listen");
		return -1;
	}
	printf("listen success!\n");
	//获取一个已经完成的客户端信息,生成一个新的文件描述符 accept
	struct sockaddr_in cin;
	socklen_t addrlen = sizeof(cin);
	int newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
	if(newfd == -1){
		ERR_MSG("accept");
		return -1;
	}
	printf("accept success!\n");
 
	char buf[32]= "";
	ssize_t res;
	while(1){
		//接收
		bzero(buf,sizeof(buf));
		res = recv(newfd,buf,sizeof(buf),0);
		if(res == -1){
			ERR_MSG("recv");
			return -1;
		}else if(res == 0){
			printf("客户端已下线\n");
			break;
		}
		printf("已接收:%s\n",buf);
		//发送
		strcat(buf,"O v O");
		if(send(newfd,buf,sizeof(buf),0) == -1){
			ERR_MSG("send");
			return -1;
		}
	}
 
	//关闭文件描述符
	if(close(sfd) == -1){
		ERR_MSG("close sfd");
		return -1;
	}
	if(close(newfd) == -1){
		ERR_MSG("close newfd");
		return -1;
	}
	return 0;
TCP 客户端
#include 
 
#define PORT 8888
#define IP "192.168.125.60"
 
#define ERR_MSG(msg) 	do{\
	fprintf(stderr,"__%d__",__LINE__);\
	perror(msg);\
}while(0)
 
int main(int argc, const char *argv[])
{
	//创建流式套接字 socket
	int cfd = socket(AF_INET,SOCK_STREAM,0);
	if(cfd == -1){
		ERR_MSG("socket");
		return -1;
	}
	printf("socket create success! cfd = %d\n",cfd);
 
 
	//真实的地址信息结构体根据地址族决定 AF_INET
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORT);
	sin.sin_addr.s_addr = inet_addr(IP);
 
	//连接服务器 connect
	if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin)) == -1){
		ERR_MSG("connect");
		return -1;
	}
	printf("connect success!\n");
 
	char buf[32]= "";
	ssize_t res;
	while(1){
		//发送
		bzero(buf,sizeof(buf));
		printf("请输入:");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = 0;
		if(send(cfd,buf,sizeof(buf),0) == -1){
			ERR_MSG("send");
			return -1;
		}
		//接收
		bzero(buf,sizeof(buf));
		res = recv(cfd,buf,sizeof(buf),0);
		if(res == -1){
			ERR_MSG("recv");
			return -1;
		}else if(res == 0){
			printf("服务器已下线\n");
			break;
		}
		printf("已接收:%s\n",buf);
	}
 
	//关闭文件描述符
	if(close(cfd) == -1){
		ERR_MSG("close sfd");
		return -1;
	}
	return 0;
}
DUP服务器:

#include 
 
#define PORT 8888
#define IP "192.168.125.60"
 
#define ERR_MSG(msg) 	do{\
		fprintf(stderr,"__%d__",__LINE__);\
		perror(msg);\
	}while(0)
 
int main(int argc, const char *argv[])
{
	//创建报式套接字 socket
	int sfd = socket(AF_INET,SOCK_DGRAM,0);
	if(sfd == -1){
		ERR_MSG("socket");
		return -1;
	}
	printf("socket create success! sfd = %d\n",sfd);
 
	//填充地址信息结构体给bind函数绑定
	//真实的地址信息结构体根据地址族决定 AF_INET
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORT);
	sin.sin_addr.s_addr = inet_addr(IP);
 
	//绑定服务器的地址信息---->必须绑定  bind
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) == -1){
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success!\n");
 
	char buf[32]= "";
	ssize_t res;
	struct sockaddr_in cin;
	socklen_t addrlen = sizeof(cin);
	while(1){
		//接收
		bzero(buf,sizeof(buf));
		res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen);
		if(res == -1){
			ERR_MSG("recv");
			return -1;
		}else if(res == 0){
			printf("客户端已下线\n");
			break;
		}
		printf("已接收:%s\n",buf);
		//发送
		strcat(buf," O v O");
		if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,addrlen) == -1){
			ERR_MSG("send");
			return -1;
		}
		printf("send success!\n");
	}
 
	//关闭文件描述符
	if(close(sfd) == -1){
		ERR_MSG("close sfd");
		return -1;
	}
	return 0;
}
DUP客户端
#include 
 
#define PORT 8888
#define IP "192.168.125.60"
 
#define ERR_MSG(msg) 	do{\
		fprintf(stderr,"__%d__",__LINE__);\
		perror(msg);\
	}while(0)
 
int main(int argc, const char *argv[])
{
	//创建报式套接字 socket
	int sfd = socket(AF_INET,SOCK_DGRAM,0);
	if(sfd == -1){
		ERR_MSG("socket");
		return -1;
	}
	printf("socket create success! sfd = %d\n",sfd);
 
	//填充地址信息结构体给bind函数绑定
	//真实的地址信息结构体根据地址族决定 AF_INET
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORT);
	sin.sin_addr.s_addr = inet_addr(IP);
 
	char buf[32]= "";
	ssize_t res;
	struct sockaddr_in cin;
	socklen_t addrlen = sizeof(cin);
	while(1){
		//发送
		bzero(buf,sizeof(buf));
		printf("请输入:");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = 0;
		if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin)) == -1){
			ERR_MSG("send");
			return -1;
		}
		printf("send success!\n");
		//接收
		bzero(buf,sizeof(buf));
		res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen);
		if(res == -1){
			ERR_MSG("recv");
			return -1;
		}else if(res == 0){
			printf("服务器已下线\n");
			break;
		}
		printf("已接收:%s\n",buf);
	}
 
	//关闭文件描述符
	if(close(sfd) == -1){
		ERR_MSG("close sfd");
		return -1;
	}
	return 0;
DUP服务器
#include 
 
#define PORT 8888
#define IP "192.168.125.60"
 
#define ERR_MSG(msg) 	do{\
		fprintf(stderr,"__%d__",__LINE__);\
		perror(msg);\
	}while(0)
 
int main(int argc, const char *argv[])
{
	//创建报式套接字 socket
	int sfd = socket(AF_INET,SOCK_DGRAM,0);
	if(sfd == -1){
		ERR_MSG("socket");
		return -1;
	}
	printf("socket create success! sfd = %d\n",sfd);
 
	//填充地址信息结构体给bind函数绑定
	//真实的地址信息结构体根据地址族决定 AF_INET
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORT);
	sin.sin_addr.s_addr = inet_addr(IP);
 
	//绑定服务器的地址信息---->必须绑定  bind
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) == -1){
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success!\n");
 
	char buf[32]= "";
	ssize_t res;
	struct sockaddr_in cin;
	socklen_t addrlen = sizeof(cin);
	while(1){
		//接收
		bzero(buf,sizeof(buf));
		res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen);
		if(res == -1){
			ERR_MSG("recv");
			return -1;
		}else if(res == 0){
			printf("客户端已下线\n");
			break;
		}
		printf("已接收:%s\n",buf);
		//发送
		strcat(buf," O v O");
		if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,addrlen) == -1){
			ERR_MSG("send");
			return -1;
		}
		printf("send success!\n");
	}
 
	//关闭文件描述符
	if(close(sfd) == -1){
		ERR_MSG("close sfd");
		return -1;
	}
	return 0;

你可能感兴趣的:(网络,c语言,开发语言)