服务器客户聊天程序。显示在线用户

时间:2013-04-18 20:49:00

标签: c networking client chat

我正在尝试将服务器中的在线用户显示给客户端。我已经声明了一个client_struct,它在客户端连接时存储必要的信息。 每次用户通过身份验证并希望与服务器通信时,我也会分配子进程。 但是,当第二个用户连接到服务器时,客户端程序只显示1个当前在线用户并停止工作。 任何原因?现在已经连续4个小时破解了代码。

服务器

/*
    C socket server example
*/

#include<stdio.h>
#include<string.h>  //strlen
#include<sys/socket.h>
#include<arpa/inet.h>   //inet_addr
#include<unistd.h>  //write
struct client_struct{
    char ip[20];
    char username[20];
    char password[20];
    int busy;
    int online;
    int user_number;

};
struct client_struct client_database[25];
char users_online[20][20];
int user_count=0;
int main(int argc , char *argv[])
{
    int socket_desc , client_sock , c , read_size,i,j=0,not_valid=1,l;
    struct sockaddr_in server , client;
    char client_message[2000],username_database[25][25],password_database[25][25],str[INET_ADDRSTRLEN],username[20],password[20],garbage[20];
    unsigned long clients[25];

    strcpy(client_database[0].username,"rushabh");
    strcpy(client_database[0].password,"nagda");
    //client_database[0].busy=0;
    //client_database[0].online=0;
    //client_database[0].user_number=0;
    strcpy(client_database[1].username,"sahil");
    strcpy(client_database[1].password,"gill");
    //client_database[1].busy=0;
    //client_database[1].online=0;
    //client_database[1].user_number=0;
    strcpy(client_database[2].username,"panty");
    strcpy(client_database[2].password,"avi");
    //client_database[2].busy=0;
    //client_database[2].online=0;
    //client_database[2].user_number=0;

    //Create socket
    socket_desc = socket(AF_INET , SOCK_STREAM , 0);
    if (socket_desc == -1)
    {
        printf("Could not create socket");
    }
    puts("Socket created");

    //Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons( 8888 );

    //Bind
    if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
    {
        //print the error message
        perror("bind failed. Error");
        return 1;
    }
    puts("bind done");

    //Listen
    listen(socket_desc , 3);

    //Accept and incoming connection
    puts("Waiting for incoming connections...");
    c = sizeof(struct sockaddr_in);

    //accept connection from an incoming client
    while(1)
    {
        client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
        //Incrementing usercount
        inet_ntop(AF_INET, &(client.sin_addr), str, INET_ADDRSTRLEN); //extracting ip address
        printf("%s\n", str); // prints "192.0.2.33"
        //if(fork()==0) //remember that this is wrong!
        //{
            //printf("%u\n",client.sin_addr.s_addr);
            //clients[user_count]=client.sin_addr.s_addr;

            if (client_sock < 0)
            {
                perror("accept failed");
                return 1;
            }
            puts("Connection accepted");
            //User authentication
            write(client_sock,"Please enter username:",strlen("Please enter username\n"));
            recv(client_sock,username,20,0);

            write(client_sock,"Please enter password:",strlen("Please enter password\n"));
            recv(client_sock,password,20,0);

            //Authentication
            for(j=0;j<3;j++)
            {
                //printf("%s %s %s %s\n",client_database[j].username,username,client_database[j].password,password);
                if(!(strcmp(client_database[j].username,username)) ||
                    !(strcmp(client_database[j].password,password)))
                {
                    printf("yay!\n");
                    write(client_sock,"Authenticated\n",strlen("Authenticated\n"));
                    recv(client_sock,garbage,20,0);
                    client_database[j].online=1;
                    client_database[j].busy=0;
                    strcpy(client_database[j].ip,str);
                    not_valid=0;
                    break;

                }

            }   

            if(not_valid==1)
            {
                write(client_sock,"Invalid username or password\n",strlen("Invalid username or password\n"));
                recv(client_sock,garbage,20,0);
                return(0);
            }

            if(fork()==0)
            {
                write(client_sock,"The online users are",strlen("The online users are"));
                recv(client_sock,garbage,20,0);
                for(l=0;l<20;l++)
                {
                    if(client_database[l].online==1)
                    {
                        write(client_sock,client_database[l].username,strlen(client_database[l].username));
                        recv(client_sock,garbage,20,0);
                    }
                    else
                    {
                        printf("%s %d\n",client_database[l].username,client_database[l].online);
                        write(client_sock,"garbage",strlen("garbage"));
                        recv(client_sock,garbage,20,0);
                    }
                }

                //Receive a message from client

                while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 )
                {
                    //Send the message back to client
                    write(client_sock , client_message , strlen(client_message));
                    puts(client_message);
                    memset(&client_message[0], 0, sizeof(client_message));

                }

                if(read_size == 0)
                {
                    puts("Client disconnected");
                    fflush(stdout);
                }
                else if(read_size == -1)
                {
                    perror("recv failed");
                }
            }
        //}
    }
    return 0;
}

客户端

/*
    C ECHO client example using sockets
*/
#include<stdio.h>   //printf
#include<string.h>  //strlen
#include<sys/socket.h>  //socket
#include<arpa/inet.h>   //inet_addr

int main(int argc , char *argv[])
{
    int sock,i=0,username_counter=0;
    struct sockaddr_in server;
    char message[1000] , server_reply[2000],server_reply1[2000];

    //Create socket
    sock = socket(AF_INET , SOCK_STREAM , 0);
    printf("%d\n",sock);
    if (sock == -1)
    {
        printf("Could not create socket");
    }
    puts("Socket created");

    server.sin_addr.s_addr = inet_addr("127.0.0.1");
    server.sin_family = AF_INET;
    server.sin_port = htons( 8888 );

    //Connect to remote server
    if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
    {
        perror("connect failed. Error");
        return 1;
    }

    puts("Connected\n");

    while(1)
    {
        recv(sock,server_reply,2000,0); //rec username
        puts(server_reply);
        scanf("%s",message);
        send(sock,message,strlen(message),0);
        memset(&server_reply[0], 0, sizeof(server_reply));

        recv(sock,server_reply,2000,0); //recv password
        puts(server_reply);
        scanf("%s",message);
        send(sock,message,strlen(message),0);
        memset(&server_reply[0], 0, sizeof(server_reply));

        recv(sock,server_reply,2000,0); //auth
        puts(server_reply);
        send(sock,"foo",strlen("foo"),0);
        memset(&server_reply[0], 0, sizeof(server_reply));

        recv(sock,server_reply,2000,0); //online users
        puts(server_reply);
        send(sock,"foo",strlen("foo"),0);
        memset(&server_reply[0], 0, sizeof(server_reply));

        while(1)
        {
            if(username_counter==20)
            {
                break;
            }
            recv(sock,server_reply,2000,0);
            if(strcmp(server_reply,"garbage"))
                puts(server_reply);
            memset(&server_reply[0], 0, sizeof(server_reply));
            send(sock,"foo",strlen("foo"),0);
            username_counter++;
        }

        /*memset(&server_reply[0], 0, sizeof(server_reply));
        recv(sock,server_reply,2000,0);
        puts(server_reply);
        memset(&server_reply[0], 0, sizeof(server_reply));
        recv(sock,server_reply,2000,0);
        puts(server_reply);*/
        break;
    }
    printf("Authenticated\n");
    memset(&server_reply[0], 0, sizeof(server_reply));
    //keep communicating with server
    while(1)
    {
        printf("Enter message : ");
        scanf("%s" , message);

        //Send some data
        if( send(sock , message , strlen(message) , 0) < 0)
        {
            puts("Send failed");
            return 1;
        }

        //Receive a reply from the server
        if( recv(sock , server_reply , 2000 , 0) < 0)
        {
            puts("recv failed");
            break;
        }

        puts("Server reply :");
        puts(server_reply);

        memset(&server_reply[0], 0, sizeof(server_reply));
    }

    close(sock);
    return 0;
}

0 个答案:

没有答案