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

思维导图

相关推荐
myw0712058 分钟前
湘大头歌程-Ride to Office练习笔记
c语言·数据结构·笔记·算法
Yue丶越3 小时前
【C语言】自定义类型:联合体与枚举
c语言·开发语言
Bona Sun4 小时前
单片机手搓掌上游戏机(十五)—pico运行fc模拟器之编译环境
c语言·c++·单片机·游戏机
white-persist4 小时前
【攻防世界】reverse | IgniteMe 详细题解 WP
c语言·汇编·数据结构·c++·python·算法·网络安全
Bona Sun6 小时前
单片机手搓掌上游戏机(十六)—pico运行fc模拟器之程序修改烧录
c语言·c++·单片机·游戏机
小邓   ༽7 小时前
50道C++编程练习题及解答-C编程例题
c语言·汇编·c++·编程练习·c语言练习题
Bona Sun8 小时前
单片机手搓掌上游戏机(十三)—pico运行fc模拟器之硬件准备
c语言·c++·单片机·游戏机
Bona Sun8 小时前
单片机手搓掌上游戏机(十八)—pico运行fc模拟器之更大屏幕
c语言·c++·单片机·游戏机
Yue丶越9 小时前
【C语言】自定义类型:结构体
c语言·开发语言
再睡一夏就好10 小时前
string.h头文件中strcpy、memset等常见函数的使用介绍与模拟实现
c语言·c++·笔记·string·内存函数·strcpy