网络编程day02(socket套接字)

今日任务:

TCP\UDP服务端客户端通信

TCP:代码

服务端:

#include 
#include 
#include 
#include 
#include 
#include 
#include 

//自定义报错提示
#define ERR_MSG(msg) do{\
		fprintf(stderr,"__%d__",__LINE__);\
		perror(msg);\
}while(0)
#define SER_PORT 8888
#define SER_IP "192.168.125.2"
/*
 * function:    TCP服务端
 * @param [ in] 
 * @param [out] 
 * @return      
 */
int main(int argc, const char *argv[])
{
	//1.创建socket套接字,
	int sfd=socket(AF_INET,SOCK_STREAM,0);
	if(sfd<0){
		ERR_MSG("socket");
		return -1;
	}
	puts("socket create");

	//允许端口快速复用
	int reuse = 1;
    if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    {
        ERR_MSG("setsockopt");
        return -1;
    }
    printf("允许端口快速复用成功\n");
	//2.绑定服务器IP和端口号bind
	struct sockaddr_in addr;
	addr.sin_family=AF_INET;
	addr.sin_port=htons(SER_PORT);
	addr.sin_addr.s_addr=inet_addr(SER_IP);
	if(bind(sfd,(struct sockaddr*)&addr,sizeof(addr))<0){
		ERR_MSG("bind");
		return -1;
	}
	puts("bind success");


	//3.建立监听listen
	if(listen(sfd,128)<0){
		ERR_MSG("listen");
		return -1;
	}
	puts("listen suucess");
	//4.等待客户端连接, accept
	struct sockaddr_in cli_addr;
	socklen_t cli_addrlen=sizeof(cli_addr);
	
	int cfd=accept(sfd,(struct sockaddr*)&cli_addr,&cli_addrlen);
	if(cfd<0){
		ERR_MSG("accept");
		return -1;
	}
	puts("accept");

	//5.接受发送消息recv;send
	char buf[128];
	while(1){
		bzero(buf,sizeof(buf));
		int recv_res=recv(cfd,buf,sizeof(buf),0);

		if(recv_res<0){
			ERR_MSG("recv");
			return -1;
		}else if(recv_res==0){
			printf("socket peer has shutdown\n");
			break;
		}
		puts("recv success");
		printf("[%s:%d]:%s\n",inet_ntoa(cli_addr.sin_addr),ntohs(cli_addr.sin_port),buf);
		if(strcmp(buf,"quit")==0)
			break;

		strcat(buf,"-----i has received");
		int send_res=send(cfd,buf,sizeof(buf),0);
		if(send_res<0){
			ERR_MSG("send");
			return -1;
		}
		puts("send success");
	}
	
	//6.关闭
	close(cfd);
	close(sfd);
	
	return 0;
}

客户端

#include 
#include 
#include 
#include 
#include 
#include 
#include 

//自定义报错提示
#define ERR_MSG(msg) do{\
		fprintf(stderr,"__%d__",__LINE__);\
		perror(msg);\
}while(0)
#define SER_PORT 8888
#define SER_IP "192.168.125.2"
/*
 * function:    TCP客户端
 * @param [ in] 
 * @param [out] 
 * @return      
 */
int main(int argc, const char *argv[])
{
	//1.创建socket套接字,
	int cfd=socket(AF_INET,SOCK_STREAM,0);
	if(cfd<0){
		ERR_MSG("socket");
		return -1;
	}
	puts("socket create");

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

	//2.连接服务端connect
	struct sockaddr_in addr;
	addr.sin_family=AF_INET;
	addr.sin_port=htons(SER_PORT);
	addr.sin_addr.s_addr=inet_addr(SER_IP);
	if(connect(cfd,(struct sockaddr*)&addr,sizeof(addr))<0){
		ERR_MSG("connect");
		return -1;
	}
	puts("connect success");
	//5.接受发送消息recv;send
	char buf[128];
	while(1){
		bzero(buf,sizeof(buf));
		printf("请输入>>>");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1]='\0';

		if(strcmp(buf,"quit")==0)
			break;

		int send_res=send(cfd,buf,sizeof(buf),0);
		if(send_res<0){
			ERR_MSG("send");
			return -1;
		}
		puts("send success");

		bzero(buf,sizeof(buf));
		int recv_res=recv(cfd,buf,sizeof(buf),0);

		if(recv_res<0){
			ERR_MSG("recv");
			return -1;
		}else if(recv_res==0){
			printf("socket peer has shutdown\n");
			break;
		}
		puts("recv success");
		printf("[%s:%d]:%s\n",SER_IP,SER_PORT,buf);

		}
	
	//6.关闭
	close(cfd);
	
	return 0;
}

运行结果:

网络编程day02(socket套接字)_第1张图片

UDP:代码

服务端:

#include 
#include 
#include 
#include           /* See NOTES */
#include 
#include 
#include 
#include 

//自定义报错提示
#define ERR_MSG(msg) do{\
		fprintf(stderr,"__%d__",__LINE__);\
		perror(msg);\
}while(0)

#define SER_PORT 8888
#define SER_IP "192.168.125.2"


int main(int argc, const char *argv[])
{
	//1.创建socket套接字
	int sfd=socket(AF_INET,SOCK_DGRAM,0);
	if(sfd<0){
		ERR_MSG("socket");
		return -1;
	}
	puts("socket success");
	
	//2.绑定IP和端口号
	struct sockaddr_in addr;
	addr.sin_family=AF_INET;
	addr.sin_port=htons(SER_PORT);
	addr.sin_addr.s_addr=inet_addr(SER_IP);
	if(bind(sfd,(struct sockaddr*)&addr,sizeof(addr))<0){
		ERR_MSG("bind");
		return -1;
	}
	puts("bind success");


	//3.接受和发送消息
	//
	struct sockaddr_in src_addr;
	socklen_t addrlen=sizeof(src_addr);
	char buf[128];
	while(1){

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

		
		if(strcmp(buf,"quit")==0)
			break;
		
		strcat(buf,"-----i has received");
		int send_res=sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&src_addr,addrlen);
		if(send_res<0){
			ERR_MSG("sendto");
			return -1;
		}
		puts("sendto success");


		}
	//4.关闭
	close(sfd);
	return 0;
}

客户端

#include 
#include 
#include 
#include           /* See NOTES */
#include 
#include 
#include 
#include 

//自定义报错提示
#define ERR_MSG(msg) do{\
		fprintf(stderr,"__%d__",__LINE__);\
		perror(msg);\
}while(0)

#define SER_PORT 8888
#define SER_IP "192.168.125.2"


int main(int argc, const char *argv[])
{
	//1.创建socket套接字
	int cfd=socket(AF_INET,SOCK_DGRAM,0);
	if(cfd<0){
		ERR_MSG("socket");
		return -1;
	}
	puts("socket success");
	
	
	//3.接受和发送消息
	//目标地址信息
	struct sockaddr_in addr;
	addr.sin_family=AF_INET;
	addr.sin_port=htons(SER_PORT);
	addr.sin_addr.s_addr=inet_addr(SER_IP);
	socklen_t addrlen=sizeof(addr);

	//记录接受信息的目标地址信息
	struct sockaddr_in recv_addr;
	socklen_t recv_addrlen=sizeof(recv_addr);
	char buf[128];
	while(1){
		
		printf("请输入>>>");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1]='\0';
		

	
		int send_res=sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&addr,addrlen);
		if(send_res<0){
			ERR_MSG("sendto");
			return -1;
		}
		puts("sendto success");
		if(strcmp(buf,"quit")==0)
			break;

		bzero(buf,sizeof(buf));
		int recv_res=recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&recv_addr,&recv_addrlen);
		if(recv_res<0){
			ERR_MSG("recvfrom");
			return -1;
		}
		puts("recvfrom success");
		printf("[%s:%d]:%s\n",inet_ntoa(recv_addr.sin_addr),ntohs(recv_addr.sin_port),buf);	

		}
	//4.关闭
	close(cfd);
	return 0;
}

运行结果:

网络编程day02(socket套接字)_第2张图片

今日思维导图:

somthing error:UDP的客户端处

网络编程day02(socket套接字)_第3张图片

一开始addrlen只有定义没有赋值,白天的时候有报错给他加了一个初始值;晚上突然一下可以一下不可以,找了找错误改了医改,应该也是这个;

你可能感兴趣的:(c语言)