admin 发表于 2022-5-24 18:36:55

Socket实现多人聊天室

//server.c
#include <winsock2.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#pragma comment (lib, "ws2_32.lib")

#define Max_client 8   ///The number of max clients connected.
#define Max_idlength 10 ///The biggest length of user id
#define Max_buff 256
#define Max_name 10
#define Max_pwd 10
#define Max_id 10

SOCKET clientSocket;
int login_voucher;
char User_id;
int k = 1;
char *account_file = "D:/code/C/socket/basin/file_transmission/account.txt";
char buff;
char user_name;

void cleanbuff();
void communication(LPVOID n);
void add_s_n_s(char *s1,int id,char *s2);
void Login(LPVOID n);


int main()
{
    WSADATA wsaData;
    WSAStartup(MAKEWORD(2, 2), &wsaData) != 0;
    if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
    {
      printf("The version of WSADATA is wrong!\n");
      return -1;
    }
    printf("The version of WSADATA is available!\n");
    SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (serverSocket == INVALID_SOCKET)
    {
      printf("Create the Socket unsuccessfully!\n");
      return -1;
    }
    printf("Create the Socket successfully!\n");

    SOCKADDR_IN addr = { 0 };

    addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
    addr.sin_family = AF_INET;
    addr.sin_port = htons(6000);
    int r = bind(serverSocket, (SOCKADDR*)&addr, sizeof(addr));
    if (r == -1)
    {
      printf("Fail to bind!\n" );
      return -1;
    }
    printf("Binded!\n" );

    r = listen(serverSocket, 10);
    if (r == -1)
    {
      printf("Fail to listen!\n" );
      return -1;
    }
    printf("Listened!\n" );
    SOCKADDR_IN cAddr = { 0 };
    int len = sizeof(cAddr);
    int i = 0;
    while (i < Max_client)
    {
      clientSocket = accept(serverSocket, (SOCKADDR*)&cAddr, &len);
      k++;
      if (clientSocket == SOCKET_ERROR)
      {
            printf("The client is not available!\n");
            closesocket(serverSocket);
            WSACleanup();
            return -1;
      }
      printf("The client %s has connected with server!\n",inet_ntoa(cAddr.sin_addr));
      CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)communication, (LPVOID)i, NULL, NULL);
    }


    return 0;
}

void communication(LPVOID n)
{
    Login(n);
    int r;
    int i = (int)n;
    if(login_voucher==1)
    {
      return ;
    }
    cleanbuff();
    strcpy(buff,User_id);
    send(clientSocket,buff,strlen(buff),NULL);
    while (1)
    {
      cleanbuff();
      r = recv(clientSocket, buff, Max_buff-1, NULL);
      if (r > 0)
      {
            printf("%s\n",buff);
            for (int j = 0; j < k; j++)
            {
                if(j!=i-1)
                  send(clientSocket, buff, strlen(buff), NULL);
            }
      }

    }
}

void Login(LPVOID n)
{
    FILE *fp;
    int read_fp=0;
    int i= (int)n;
    char user_id,user_pwd,user_name;
    fp = fopen(account_file,"rb");
    if(fp==NULL)
    {
      printf("The file %s does not exit!",account_file);
      fclose(fp);
      return;
    }
    recv(clientSocket,buff,Max_buff-1,0);
    printf("The client want to login the account whose ID is %s\n",buff);
    while(1)
    {
      read_fp=fscanf(fp,"%s %s %s",user_id,user_pwd,user_name);
      if(read_fp==-1)
      {
            cleanbuff();
            sprintf(buff,"N");
            send(clientSocket,buff,strlen(buff),0);
            cleanbuff();
            sprintf(buff,"Can't find the account!");
            send(clientSocket,buff,strlen(buff),0);
            fclose(fp);
            return 0;
      }
      if(strcmp(user_id,buff)==0)
      {
            cleanbuff();
            sprintf(buff,"Y");
            send(clientSocket,buff,strlen(buff),0);
            cleanbuff();
            sprintf(buff,"Please input the password of the account!");
            send(clientSocket,buff,strlen(buff),0);
            fclose(fp);
            break;
      }
    }
    int pwd_time =1;
    while(pwd_time<4)
    {
      cleanbuff();
      recv(clientSocket,buff,Max_buff-1,0);
      printf("%s\n",buff);
      if (strcmp(buff,user_pwd)==0)
      {
            cleanbuff();
            sprintf(buff,"Y");
            send(clientSocket,buff,strlen(buff),0);
            cleanbuff();
            sprintf(buff,"Login the account successfully!");
            send(clientSocket,buff,strlen(buff),0);
            cleanbuff();
            recv(clientSocket,buff,Max_buff-1,0); ///As a buffer between two transmissions
            printf("%s\n",buff);
            printf("One account has login,ID:%s PWD:%s NAME:%s\n",user_id,user_pwd,user_name);
            cleanbuff();
            strcpy(buff,user_name);
            send(clientSocket,buff,strlen(buff),0);
            break;
      }
      cleanbuff();
      sprintf(buff,"N");
      send(clientSocket,buff,strlen(buff),0);
      pwd_time += 1;
      cleanbuff();
      sprintf(buff,"Please input the password again!");
      send(clientSocket,buff,strlen(buff),0);
    }
    if(pwd_time<4)
    {
      return 1;
    }
    sprintf(buff,"Login the account unsuccessfully!");
    send(clientSocket,buff,strlen(buff),0);
    return 0;
}
void cleanbuff()
{
    memset(buff,0,Max_buff);
    return ;
}
页: [1]
查看完整版本: Socket实现多人聊天室