Linux UDP简单聊天室

论坛 期权论坛 脚本     
匿名技术用户   2020-12-30 15:44   11   0

服务器端:

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

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP