简易多人聊天室

/*服务器*/
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <pthread.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/wait.h>


//person number  who is chating  
#define CHATNUM 8
int sd[CHATNUM];


struct pthreadParement
{
       struct sockaddr_in client_ip;
       int client_sock;
};
void myError(char *error,int errorLine)
{
     fprintf(stderr,"errorline:%d ",errorLine);
     perror(error);
     exit(1);
}


// server send message that one client send to every client that online 
void  sendMsgToClient(struct pthreadParement *parement)
{
     char name[30];
     char buffer[2048];
     char temp[3000];
     char tmp[30];
     int i=0;
     int returnValue=-1;
     //get client's name
     if((returnValue=recv(parement->client_sock,&name,20,0))==-1)
     {
        myError("recv",__LINE__);
     }
     name[returnValue]=':';
     name[returnValue+1]='\0';
     strcpy(tmp,name);
     tmp[returnValue]='\0';
     printf("user:%s is online,from ip:%s\n",tmp,\
             inet_ntoa(parement->client_ip.sin_addr));
     while(1)
     {
          //get the imformation that client send
          memset(buffer,'\0',sizeof(buffer));
          returnValue=recv(parement->client_sock,&buffer,1024,0);
          if(returnValue==-1)
          {
             myError("recv",__LINE__);
          }
          //the client was offline
          else if( returnValue==0)
          {
              printf("%sbye",name);
              break;
          }
          /*
           * find a socket that attact to client onlin
           * send message to client online
           */
          for(i=0;i<CHATNUM;i++)
          {
              if(sd[i]!=-1)
              {
                temp[0]='\0';
                strcat(temp,name);
                strcat(temp,buffer);
                if(send(sd[i],&temp,strlen(temp),0)==-1)
                {
                   myError("send",__LINE__);
                }
              }
          }
     }
     close(parement->client_sock);
     for(i=0;i<CHATNUM;i++)
     {
         if(sd[i]==parement->client_sock)
         {
            sd[i]=-1;
         }
     }
     pthread_exit(NULL);
}




int main()
{
    int i=0;
    int localSocket=0;
    int clientSocket=0;
    pthread_t pd;
    struct sockaddr_in server_ip;
    struct sockaddr_in client_ip;
    struct pthreadParement parement;
    socklen_t client_addr_size=0;
    //init socket
    memset(sd,-1,sizeof(sd));
    if((localSocket=socket(AF_INET,SOCK_STREAM,0))==-1)
    {
       myError("socket",__LINE__);
    }
    //set IP imformation
    server_ip.sin_family=AF_INET;
    server_ip.sin_port=htons(12345);
    server_ip.sin_addr.s_addr=INADDR_ANY;
    //bzero(&(server_ip.sin_zero),8);
    if(bind(localSocket,(struct sockaddr *) &server_ip,\
      sizeof(struct sockaddr ))==-1)
    {
       myError("bind",__LINE__);
    }
    if(listen(localSocket,10)==-1)
    {
       myError("listen",__LINE__);
    }
    while(1)
    {
       client_addr_size=sizeof(struct sockaddr_in);
       if((clientSocket=accept(localSocket,(struct sockaddr *) &client_ip,\
          &client_addr_size))==-1)
       {
          myError("accept",__LINE__);
       }
       //find a free socket
       while(sd[i]!=-1)
       {
          i=(i+1)%CHATNUM;
          continue;
        }
        sd[i]=clientSocket;
        parement.client_ip.sin_addr=client_ip.sin_addr;
        parement.client_sock=clientSocket;
        pthread_create(&pd,NULL,(void *)sendMsgToClient,&parement);
     }
     return 0;
};

/*客户端*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <pthread.h>

void dellMsgFromServer(int sd)
{
     char recvMsg[2000];
     int recvMsgLen=0;
     while(1)
     {
       if((recvMsgLen=recv(sd,&recvMsg,1024,0))<0)
       {
           perror("recv");
           exit(1);
       }
       recvMsg[recvMsgLen]='\0';
       printf("%s\n",recvMsg);
     }
     pthread_exit(NULL);
}
int main()
{
    char input[2000];
    char clientName[20];
    char acceptEnter;
    int sd;
    pthread_t pd;
    // set the server address
    struct sockaddr_in server_addr;

    server_addr.sin_family=AF_INET;
    server_addr.sin_port=htons(12345);
    if(inet_aton("127.0.0.1",&server_addr.sin_addr)==0)
    {
       perror("inet_aton");
       exit(1);
    }
    bzero(&(server_addr.sin_zero),8);
    if((sd=socket(AF_INET,SOCK_STREAM,0))<0)
    {
        perror("socket");
        exit(1);
    }
    if(connect(sd,(struct sockaddr *)&server_addr,\
       sizeof(struct sockaddr_in))!=0)
    {
       perror("connect");
       exit(1);
    }
    printf("please input your name,note:your name lenth not over 20 byte\n");
    fgets(clientName,1024,stdin);
    clientName[strlen(clientName)-1]='\0';
    if(send(sd,&clientName,strlen(clientName),0)<0)
    {
       perror("send");
       exit(1);
    }
    if(pthread_create(&pd,NULL,(void *)dellMsgFromServer,sd)!=0)
    {
       perror("pthread_create");
       exit(1);
    }
    printf("you can input to chat now\n");
    printf("note:input not over 1024\n");
    while(1)
    {
       input[0]='\0';
       fgets(input,1024,stdin);
       input[strlen(input)-1]='\0';
       if(send(sd,&input,strlen(input),0)<0)
       {
          perror("send");
          exit(1);
       }
    }
    close(sd);
    return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值