作业--day31

发布时间:2023年12月18日

TCP

server.c

#include <myhead.h>

#define PORT 8888
#define IP "192.168.0.102"

int main(int argc, const char *argv[])
{
	int sfd = socket(AF_INET, SOCK_STREAM, 0);
	if(sfd == -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 error");
		return -1;
	}

	if(listen(sfd, 128) == -1){
		perror("listen error");
		return -1;
	}

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

	char buf[128];
	while(1){
		bzero(buf, sizeof(buf));
		int res = recv(newfd, buf, sizeof(buf), 0);
		if(res == 0){
			puts("客户端已下线");
			break;
		}
		printf("[%s: %d] %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf);
		
		strcat(buf, "*_*");
		send(newfd, buf, sizeof(buf), 0);
		puts("发送成功");
	}

	close(newfd);
	close(sfd);

	return 0;
}

client.c

#include <myhead.h>

#define SPORT 8888
#define SIP "192.168.0.102"

#define CPORT 9999

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

	int reuse = -1;
	if(setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1){
		perror("setsockopt error");
		return -1;
	}

	struct sockaddr_in cin;
	cin.sin_family = AF_INET;
	cin.sin_port = htons(CPORT);
	cin.sin_addr.s_addr = inet_addr(SIP);
	if(bind(cfd, (struct sockaddr *)&cin, sizeof(cin)) == -1){
		perror("bind error");
		return -1;
	}

	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(SPORT);
	sin.sin_addr.s_addr = inet_addr(SIP);
	if(connect(cfd, (struct sockaddr *)&sin, sizeof(sin)) == -1){
		perror("connect error");
		return -1;
	}

	char sbuf[128] = "hello";
	char rbuf[128];
	while(1){
		send(cfd, sbuf, sizeof(sbuf), 0);
		bzero(rbuf, sizeof(rbuf));
		recv(cfd, rbuf, sizeof(rbuf), 0);
		printf("[%s: %d] %s\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), rbuf);
		sleep(1);
	}

	close(cfd);
	return 0;
}

UDP

server.c

#include <myhead.h>

#define PORT 8888
#define IP "192.168.0.102"

int main(int argc, const char *argv[])
{
	int sfd = socket(AF_INET, SOCK_DGRAM, 0);
	if(sfd == -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 error");
		return -1;
	}

	struct sockaddr_in cin;
	socklen_t socklen = sizeof(cin);

	char buf[128];
	while(1){
		bzero(buf, sizeof(buf));
		recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr *)&cin, &socklen);
		printf("[%s: %d] %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf);
		strcat(buf, "*_*");
		sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr *)&cin, socklen);
	}
	close(sfd);
	return 0;
}

client.c

#include <myhead.h>

#define PORT 8888
#define IP "192.168.0.102"

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

	int reuse = -1;
	if(setsockopt(cfd, 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);
	socklen_t socklen = sizeof(sin);
	if(connect(cfd, (struct sockaddr *)&sin, sizeof(sin)) == -1){
		perror("connect error");
		return -1;
	}

	char buf[128] = "hello";
	while(1){
		sendto(cfd, buf, sizeof(buf), 0, (struct sockaddr *)&sin, socklen);
		recvfrom(cfd, buf, sizeof(buf), 0, NULL, NULL);
		printf("[%s: %d] %s\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), buf);
		sleep(1);
	}
	close(cfd);
	return 0;
}

思维导图

在这里插入图片描述

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