网络编程day2作业

2023-12-20 15:12:11

TCP通信模型

TCP服务器端

#include<myhead.h>

#define PORT 8888
#define IP "192.168.125.130"

int main(int argc, const char *argv[])
{
	//创建文件描述符
	int sfd = -1;
	if((sfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
	{
		perror("socket error");
		return -1;
	}

	int reuse = 1;
	if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))==-1)
	{   
		perror("setsockopt error");
		return -1;                                                                     
	}  
	//定义结构体
	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)) == -1)
	{
		perror("bind");
		return -1;
	}
	if(listen(sfd,128)==-1)
	{
		perror("listen");
		return -1;
	}


	//定义接受客服端的地址结构体
	struct sockaddr_in cin;
	socklen_t socklen = sizeof(cin);

	int newfd = -1;
	if((newfd=accept(sfd,(struct sockaddr*)&cin,&socklen)) == -1)
	{
		perror("accept");
		return -1;
	}

	//收发数据
	char buf[128]="";
	while(1)
	{
		bzero(buf,sizeof(buf));

		int res=recv(newfd,buf,sizeof(buf),0);
		if(res == 0)
		{
			printf("客服端下线\n");
			break;
		}
		printf("%s\n",buf);
		strcat(buf,"*_*");
		send(newfd,buf,sizeof(buf),0);
	}
	close(sfd);
	close(newfd);

	return 0;
}

?TCP客户端

#include<myhead.h>

#define PORT 8888
#define IP "192.168.125.130"
#define PORT1 6666
#define IP1 "192.168.125.130"
int main(int argc, const char *argv[])
{
	//创建文件描述符
	int sfd = -1;
	if((sfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
	{
		perror("socket error");
		return -1;
	}

    int reuse = 1;
    if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))==-1)
    {   
        perror("setsockopt error");
        return -1;                                                                     
    }  
	//定义结构体
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORT1);
	sin.sin_addr.s_addr = inet_addr(IP1);

	//链接
	if(bind(sfd,(struct sockaddr*) &sin,sizeof(sin)) == -1)
	{
		perror("bind");
		return -1;
	}
		//定义接受客服端的地址结构体
	struct sockaddr_in cin;

	cin.sin_family = AF_INET;
	cin.sin_port = htons(PORT);
	cin.sin_addr.s_addr = inet_addr(IP);
	if(connect(sfd,(struct sockaddr*)&cin,sizeof(sin)) == -1)
	{
		perror("connect");
		return -1;
	}

	//收发数据
	char buf[128]="";
	while(1)
	{
		bzero(buf,sizeof(buf));

		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1]=0;
		send(sfd,buf,sizeof(buf),0);

		bzero(buf,sizeof(buf));

		int res=recv(sfd,buf,sizeof(buf),0);
		if(res == 0)
		{
			printf("服务器下线\n");
			break;
		}
		printf("%s\n",buf);
	}
	close(sfd);

	return 0;
}

效果图:?

?UDP通信模型

UDP服务器:

#include<myhead.h>

#define PORT 8888
#define IP "192.168.125.130"

int main(int argc, const char *argv[])
{
	int sfd = -1;
	if((sfd = socket(AF_INET,SOCK_DGRAM,0)) == -1)
	{
		perror("socket");
		return -1;
	}

	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)) == -1)
	{
		perror("bind");
		return -1;
	}

	struct sockaddr_in cin;
	socklen_t socklen = sizeof(cin);
	char buf[128]="";
	while(1)
	{
		bzero(buf,sizeof(buf));

		int res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&socklen);
		if(res == 0)
		{
			printf("客户端下线\n");
			break;
		}
		printf("发来的消息为%s\n",buf);
		strcpy(buf,"*_*");
		sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,sizeof(cin));
	}
	return 0;
}

UDP客户端:

#include<myhead.h>

#define PORT 8888
#define IP "192.168.125.130"
#define PORT1 6666
#define IP1 "192.168.125.130"
int main(int argc, const char *argv[])
{
	int sfd = -1;
	if((sfd = socket(AF_INET,SOCK_DGRAM,0)) == -1)
	{
		perror("socket");
		return -1;
	}

	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORT1);
	sin.sin_addr.s_addr = inet_addr(IP1);

	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) == -1)
	{
		perror("bind");
		return -1;
	}

	struct sockaddr_in cin;
	cin.sin_family = AF_INET;
	cin.sin_port = htons(PORT);
	cin.sin_addr.s_addr = inet_addr(IP);
	socklen_t socklen = sizeof(cin);
	char buf[128]="";
	while(1)
	{
		bzero(buf,sizeof(buf));
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1]=0;
		sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,sizeof(cin));

		int res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&socklen);
		if(res == 0)
		{
			printf("服务器下线\n");
			break;
		}
		printf("传回来的消息为%s\n",buf);
	}
	return 0;
}

效果图:

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