Linux UDP簡單聊天室

服務器端:

#include<stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
int sockfd = 0;
//創建結構體用來存放客戶端信息
typedef struct node
{
	int client_port;
	struct sockaddr_in clientaddr;
	char name[128];
	struct node *next;
}node_t;
node_t *link_head()
{
	node_t *head = (node_t *)malloc(sizeof(node_t));
	head->next = NULL;
	return head;
}
node_t *h;
//正插法
void link_creat(node_t *head , int client_port,char *name,struct sockaddr_in clientaddr)
{
	node_t *new = (node_t *)malloc(sizeof(node_t));
	new->client_port = client_port;
	strcpy(new->name,name);
	new->clientaddr = clientaddr;
	new->next = head->next;
	head->next = new;
}
//打印
void link_show(node_t *head)
{
	while(head->next !=NULL)
	{
		head =head->next;
		printf("client  %s\n",head->name);
	}
}
//刪除
int link_delete(node_t *head,int client_port)
{
	if(link_empty(head))
	{
		printf("無需刪除\n");
		return -1;
	}
	while(head->next != NULL)
	{
		node_t *t = head->next;
		printf("%d\n",client_port);
		if(t->client_port == client_port)
		{
			head->next = t->next;
			printf("%d\n",t->client_port);
			printf("刪除成功\n");
			free(t);
			t = NULL;
			return 1;
		}
		head = head->next; 
	}
	return 0;
}
//判斷是否爲空
int link_empty(node_t *head)
{
	if(head->next == NULL)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
//查找
char *link_search(node_t *head,int client_port)
{
	while(head->next != NULL)
	{
		head = head->next;
		if(head->client_port == client_port)
		{
			return head->name;
		}
	}
	return NULL;
}
//發送給處除了自己的其他用戶
int cliene_send(node_t *head,struct sockaddr_in clientaddr,char *buf)
{
	socklen_t len = sizeof(clientaddr);
	while(head->next != NULL)
	{
		head = head->next;
		if(head->client_port != ntohs(clientaddr.sin_port))
		{
			if(sendto(sockfd,buf,strlen(buf),0,(struct sockaddr *)&(head->clientaddr),len)==-1)
			{
				printf("[%d]send error\n",__LINE__);
				return -1;
			}
		}
	}
}
//接收線程
void *fun_recv(void *a)
{
	struct sockaddr_in clientaddr = *(struct sockaddr_in *)a;
	char buf[128] = {0};
	char client_name[128];
	socklen_t len = sizeof(clientaddr);
	int r = 0;
	int client_port;
	char *isclient_port = NULL;
//	node_t *h = link_head();
	while(1)
	{
		memset(buf,0,sizeof(buf));
		r = recvfrom(sockfd,buf,sizeof(buf),0,(struct sockaddr *)&clientaddr,&len);
		client_port = ntohs(clientaddr.sin_port);
		isclient_port = link_search(h,client_port);
		if(r == -1)
		{
			printf("[%d]recv error\n",__LINE__);
			return;
		}
		else
		{
			//如果客戶端不存在那麼輸入的第一條消息就爲用戶名
			if(isclient_port==NULL)
			{
				memset(client_name,0,sizeof(client_name));
				strcat(client_name,buf);
				link_creat(h,client_port,client_name,clientaddr);
				printf("%s登錄了\n",buf);
				strcat(client_name,"上線了");
		//		printf("port = %d\n",ntohs(clientaddr.sin_port));
				cliene_send(h,clientaddr,client_name);
			}
			else
			{
				//服務器接收到quit後會將這個客戶端退出通知其他人,並刪除數據
				if(strcmp(buf,"quit")==0)
				{
					printf("客戶端%s請求退出\n",isclient_port);
					char ss[128] = {0};
					memset(ss,0,sizeof(ss));
					strcat(ss,isclient_port);
					strcat(ss,"退出了 ");
					cliene_send(h,clientaddr,ss);
					link_delete(h,client_port);
					link_show(h);
				}
				else
				{
					char ss[128] = {0};
					memset(ss,0,sizeof(ss));
					strcat(ss,isclient_port);
					strcat(ss,": ");
					strcat(ss,buf);
					cliene_send(h,clientaddr,ss);
					//link_show(h);
					printf("%s :%s\n",isclient_port,buf);
				//	printf("port = %d\n",ntohs(clientaddr.sin_port));
				}
			}
		}
		//printf("port = %d\n",ntohs(clientaddr.sin_port));
		//printf("addr = %s\n",inet_ntoa(clientaddr.sin_addr));
	}

}
//發送線程
void *fun_send(void *a)
{
//	struct sockaddr_in clientaddr = *(struct sockaddr_in *)a;
	char buf[128] = {0};
	while(1)
	{
		node_t *head = (node_t *)a;
		socklen_t len = sizeof(head->clientaddr);
		scanf("%s",buf);
		while(head->next!=NULL)
		{
			head = head->next;
			if(sendto(sockfd,buf,strlen(buf),0,(struct sockaddr *)&(head->clientaddr),len)==-1)
			{
				printf("[%d]send error\n",__LINE__);
				return;
			}
		}
	}
}
int main(int argc, const char *argv[])
{
	h=link_head();
	struct sockaddr_in serveraddr,clientaddr;
	if(argc!=2)
	{
		printf("請輸入端口號\n");
		return -1;
	}
	//創建套接字
	sockfd = socket(AF_INET,SOCK_DGRAM,0);
	if(sockfd == -1)
	{
		printf("socket error\n");
		return -1;
	}
	//設置網絡信息
	serveraddr.sin_family = AF_INET;
	serveraddr.sin_port = htons(atoi(argv[1]));
	serveraddr.sin_addr.s_addr = inet_addr("192.168.11.218");

	//綁定
	if(bind(sockfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr))==-1)
	{
		printf("bind error\n");
		return -1;
	}	
	pthread_t pthid1,pthid2;
	//通信
	if(pthread_create(&pthid1,NULL,fun_recv,&clientaddr)!=0)
	{
		printf("pthread error\n");
		return -1;
	}
	
	if(pthread_create(&pthid2,NULL,fun_send,h)!=0)
	{
		printf("pthread error\n");
		return -1;
	}
	pthread_join(pthid1,NULL);
	pthread_join(pthid2,NULL);
	close(sockfd);
	return 0;
}

客戶端:

#include<stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
int sockfd = 0;
struct sockaddr_in serveraddr;
socklen_t len = sizeof(serveraddr);
pthread_t pthid1,pthid2;
//發送線程
void *fun_send(void *a)
{
	char buf[128] = {0};
	printf("請輸入用戶名\n");
	while(1)
	{
		
		gets(buf);
		if(sendto(sockfd,buf,strlen(buf),0,(struct sockaddr *)&serveraddr,sizeof(serveraddr))==-1)
		{
			printf("sendto error\n");
			return;
		}
		if(strcmp(buf,"quit")== 0)
		{
			exit(0);
			break;
		}
	}
}
//接收線程
void *fun_recv(void *a)
{
	char buf[128] = {0};
	while(1)
	{
		memset(buf,0,sizeof(buf));
		if(recvfrom(sockfd,buf,sizeof(buf),0,(struct sockaddr *)&serveraddr,&len)==-1)
		{
			printf("recv error\n");
			return;
		}
		else
		{
			if(strcmp(buf,"quit")== 0)
			{
				//exit(0);
				pthread_exit(pthid1);
				pthread_exit(pthid2);
			}
			printf("%s\n",buf);
		}
	}
}
int main(int argc, const char *argv[])
{
	if(argc!=2)
	{
		printf("請輸入端口號\n");
		return -1;
	}
	//創建套接字
	sockfd = socket(AF_INET,SOCK_DGRAM,0);
	if(sockfd == -1)
	{
		printf("socket error\n");
		return -1;
	}
	//設置網絡信息
	serveraddr.sin_family = AF_INET;
	serveraddr.sin_port = htons(atoi(argv[1]));
	serveraddr.sin_addr.s_addr = inet_addr("192.168.11.218");

	//通信
	if(pthread_create(&pthid1,NULL,fun_send,NULL)!=0)
	{
		printf("pthread error\n");
		return -1;
	}
	if(pthread_create(&pthid2,NULL,fun_recv,NULL)!=0)
	{
		printf("pthread error\n");
		return -1;
	}
	pthread_join(pthid1,NULL);
	pthread_join(pthid2,NULL);
	close(sockfd);
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章