作业--day31

TCP

server.c

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

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

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

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;
}

思维导图

相关推荐
hikktn39 分钟前
如何在 Rust 中实现内存安全:与 C/C++ 的对比分析
c语言·安全·rust
观音山保我别报错41 分钟前
C语言扫雷小游戏
c语言·开发语言·算法
小林熬夜学编程3 小时前
【Linux系统编程】第四十一弹---线程深度解析:从地址空间到多线程实践
linux·c语言·开发语言·c++·算法
墨墨祺3 小时前
嵌入式之C语言(基础篇)
c语言·开发语言
躺不平的理查德4 小时前
数据结构-链表【chapter1】【c语言版】
c语言·开发语言·数据结构·链表·visual studio
幼儿园园霸柒柒5 小时前
第七章: 7.3求一个3*3的整型矩阵对角线元素之和
c语言·c++·算法·矩阵·c#·1024程序员节
好想有猫猫5 小时前
【51单片机】串口通信原理 + 使用
c语言·单片机·嵌入式硬件·51单片机·1024程序员节
摆烂小白敲代码6 小时前
背包九讲——背包问题求方案数
c语言·c++·算法·背包问题·背包问题求方案数
stm 学习ing7 小时前
C语言 循环高级
c语言·开发语言·单片机·嵌入式硬件·算法·嵌入式实时数据库
w微信150135078127 小时前
小华一级 代理商 HC32F005C6PA-TSSOP20 HC32F005系列
c语言·arm开发·单片机·嵌入式硬件