php中文网 | cnphp.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 561|回复: 0

Socket实现多人聊天室

[复制链接]

3138

主题

3148

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

UID
1
威望
0
积分
7946
贡献
0
注册时间
2021-4-14
最后登录
2024-11-21
在线时间
763 小时
QQ
发表于 2022-5-24 18:36:55 | 显示全部楼层 |阅读模式
[mw_shl_code=cpp,true]//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[Max_client];
int login_voucher[Max_client];
char User_id[Max_client][Max_idlength];
int k = 1;
char *account_file = "D:/code/C/socket/basin/file_transmission/account.txt";
char buff[Max_buff];
char user_name[Max_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[i++] = accept(serverSocket, (SOCKADDR*)&cAddr, &len);
        k++;
        if (clientSocket[i - 1] == 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[i-1]==1)
    {
        return ;
    }
    cleanbuff();
    strcpy(buff,User_id[i-1]);
    send(clientSocket[i-1],buff,strlen(buff),NULL);
    while (1)
    {
        cleanbuff();
        r = recv(clientSocket[i - 1], 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[j], buff, strlen(buff), NULL);
            }
        }

    }
}

void Login(LPVOID n)
{
    FILE *fp;
    int read_fp=0;
    int i= (int)n;
    char user_id[Max_id],user_pwd[Max_pwd],user_name[Max_name];
    fp = fopen(account_file,"rb");
    if(fp==NULL)
    {
        printf("The file %s does not exit!",account_file);
        fclose(fp);
        return;
    }
    recv(clientSocket[i-1],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[i-1],buff,strlen(buff),0);
            cleanbuff();
            sprintf(buff,"Can't find the account!");
            send(clientSocket[i-1],buff,strlen(buff),0);
            fclose(fp);
            return 0;
        }
        if(strcmp(user_id,buff)==0)
        {
            cleanbuff();
            sprintf(buff,"Y");
            send(clientSocket[i-1],buff,strlen(buff),0);
            cleanbuff();
            sprintf(buff,"Please input the password of the account!");
            send(clientSocket[i-1],buff,strlen(buff),0);
            fclose(fp);
            break;
        }
    }
    int pwd_time =1;
    while(pwd_time<4)
    {
        cleanbuff();
        recv(clientSocket[i-1],buff,Max_buff-1,0);
        printf("%s\n",buff);
        if (strcmp(buff,user_pwd)==0)
        {
            cleanbuff();
            sprintf(buff,"Y");
            send(clientSocket[i-1],buff,strlen(buff),0);
            cleanbuff();
            sprintf(buff,"Login the account successfully!");
            send(clientSocket[i-1],buff,strlen(buff),0);
            cleanbuff();
            recv(clientSocket[i-1],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[i-1],buff,strlen(buff),0);
            break;
        }
        cleanbuff();
        sprintf(buff,"N");
        send(clientSocket[i-1],buff,strlen(buff),0);
        pwd_time += 1;
        cleanbuff();
        sprintf(buff,"Please input the password again!");
        send(clientSocket[i-1],buff,strlen(buff),0);
    }
    if(pwd_time<4)
    {
        return 1;
    }
    sprintf(buff,"Login the account unsuccessfully!");
    send(clientSocket[i-1],buff,strlen(buff),0);
    return 0;
}
void cleanbuff()
{
    memset(buff,0,Max_buff);
    return ;
}
[/mw_shl_code]

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|php中文网 | cnphp.com ( 赣ICP备2021002321号-2 )

GMT+8, 2024-11-22 03:13 , Processed in 0.285952 second(s), 34 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

申明:本站所有资源皆搜集自网络,相关版权归版权持有人所有,如有侵权,请电邮(fiorkn@foxmail.com)告之,本站会尽快删除。

快速回复 返回顶部 返回列表