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


猜你喜欢

转载自blog.csdn.net/m0_72847002/article/details/132548727