TCP和UDP的 cs模型

TCP服务器

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <time.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <errno.h>

#define LOG(s) printf("[%s] {%s:%d} %s\n", __DATE__, __FILE__, __LINE__, s);

int main(int argc, char *argv[])
{
	//创建服务器socket
	int server = 0;
	if((server = socket(AF_INET, SOCK_STREAM, 0)) == -1)
	{
		LOG("socket error");
		return -1;
	}

	//绑定服务器IP和端口号
	//给地址信息结构体赋值
	struct sockaddr_in saddr = {0};
	saddr.sin_family = AF_INET;
	saddr.sin_port = htons(8888);
	saddr.sin_addr.s_addr = htonl(INADDR_ANY);

	if(bind(server, (struct sockaddr*)&saddr, sizeof(saddr)) == -1)
	{
		LOG("bind error");
		return -1;
	}
	
	//监听对应的端口号
	if(listen(server, 5) == -1)
	{
		LOG("listen error");
		return -1;
	}
	
	puts("server start success");
	
	//创建用于与客户端通信的socket
	struct sockaddr_in caddr = {0};
	int client = 0;
	socklen_t asize = sizeof(caddr);
	if((client = accept(server, (struct sockaddr*)&caddr, &asize)) == -1)
	{
		LOG("accept error");
		return -1;
	}


	
	//接收消息
	char buf[128] = " ";

	while(1)
	{
		int len = 0;
		bzero(buf, sizeof(buf));
		if((len = recv(client, buf, sizeof(buf), 0)) < 0)
		{
			LOG("read error");
		}
		else if(len == 0)
		{
			break;
		}
		//buf[strlen(buf)-1] = '\0';
		printf("[%s/%d]client: %s\n", inet_ntoa(caddr.sin_addr), ntohs(caddr.sin_port), buf);
		
		bzero(buf, sizeof(buf));
		//发送消息
		strcpy(buf, "ok");
		write(client, buf, len);
			
	}
	
	close(client);
	close(server);
	
	//关闭文件描述符

    return 0;
}


TCP客户端

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <time.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <errno.h>

#define LOG(s) printf("[%s] {%s:%d} %s \n", __DATE__, __FILE__, __LINE__, s);

int main(int argc, char *argv[])
{
	
	//创建套接字
	int client = 0;
	if((client = socket(AF_INET, SOCK_STREAM, 0)) == -1)
	{
		LOG("socket error");
		return -1;
	}

	//给地址信息结构体赋值
	struct sockaddr_in caddr = {0};
	caddr.sin_family = AF_INET;
	caddr.sin_port = htons(8888);
	caddr.sin_addr.s_addr = inet_addr("192.168.114.77");
	
	//连接服务端
	socklen_t asize = sizeof(caddr);
	if(connect(client, (struct sockaddr*)&caddr, asize) == -1)
	{
		LOG("connect error");
		return -1;
	}
	printf("connect success\n");	

	//发送消息
	char buf[128] = "";
	while(1)
	{
		bzero(buf, sizeof(buf));
		fgets(buf, sizeof(buf), stdin);
		buf[strlen(buf)-1] = '\0';

		if(send(client, buf, sizeof(buf), 0) == -1)
		{
			LOG("send error");
			return -1;
		}
	
		//接收消息

		bzero(buf, sizeof(buf));
		int len = 0;
		if(((len = recv(client, buf, sizeof(buf), 0)) < 0))
		{
			LOG("recv error");
			return -1;
		}
		else if(len == 0)
		{
			puts("server close");
		}

		printf("recv:\n");
		printf("%s\n", buf);
	}
	
	//关闭文件描述符
	close(client);

    return 0;
}


UDP服务器

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <time.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <errno.h>

#define LOG(s) printf("[%s] {%s:%d} %s \n", __DATE__, __FILE__, __LINE__, s);

int main(int argc, char *argv[])
{

	//创建套接字
	int server = 0;
	if((server = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
	{
		LOG("socket");
		return -1;
	}
	
	//给地址信息结构体赋值
	struct sockaddr_in saddr = {0};
	saddr.sin_family = AF_INET;
	saddr.sin_port = htons(8899);
	saddr.sin_addr.s_addr = htonl(INADDR_ANY);
	
	//绑定地址信息结构体
	if(bind(server, (struct sockaddr*)&saddr, sizeof(saddr)) == -1)
	{
		LOG("bind error");
		return -1;
	}

	puts("udp server start success");
	
	char buf[128] = "";
	socklen_t len = 0;
	struct sockaddr_in remote = {0};
	while(1)
	{
		bzero(buf, sizeof(buf));
		//接受消息
		int len = sizeof(remote);
		int res = 0;
		if((res = recvfrom(server, buf, sizeof(buf), 0, (struct sockaddr*)&remote, &len)) == -1)
		{
			LOG("recvfrom error");
			return -1;
		}
		else if(res == 0)
		{
			break;
		}
		printf("[%s/%d] client: %s\n", inet_ntoa(remote.sin_addr), ntohs(remote.sin_port), buf);
		//发送消息
		bzero(buf, sizeof(buf));
		strcpy(buf, "ok");
		if(sendto(server, buf, sizeof(buf), 0, (struct sockaddr*)&remote, len) == -1)
		{
			LOG("sendto error");
			return -1;
		}
	}	
	
	//关闭文件描述符
	close(server);
    return 0;
}


UDP客户端

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <time.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <errno.h>

#define LOG(s) printf("[%s] {%s:%d} %s \n", __DATE__, __FILE__, __LINE__, s);

int main(int argc, char *argv[])
{

	//创建套接字
	int client = 0;
	if((client = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
	{
		LOG("socket error");
		return -1;
	}
	
	//地址信息结构体赋值
	struct sockaddr_in caddr = {0};
	struct sockaddr_in remote = {0};

	caddr.sin_family = AF_INET;
	caddr.sin_port = htons(10086);
	caddr.sin_addr.s_addr = htonl(INADDR_ANY);

	remote.sin_family = AF_INET;
	remote.sin_port = htons(8899);
	remote.sin_addr.s_addr = inet_addr("192.168.114.227");	
	
	char buf[128] = "";
	socklen_t len = sizeof(remote);
	while(1)
	{
		int res = 0;
		bzero(buf, sizeof(buf));

		//发送信息
		fgets(buf, sizeof(buf), stdin);
		buf[strlen(buf)-1] = '\0';
		if(sendto(client, buf, sizeof(buf), 0, (struct sockaddr*)&remote, len) < 0)
		{
			LOG("sendto error");
			return -1;
		}

		//接受信息
		bzero(buf, sizeof(buf));
		if((res = recvfrom(client, buf, sizeof(buf), 0, (struct sockaddr*)&remote, &len)) == -1)
		{
			LOG("recvfrom error");
			return -1;
		}
		printf("recv: %s\n", buf);

	}
	
	//关闭文件描述符
	close(client);

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值