Thread: Unix: Server does not works as intened

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    67

    Unix: Server does not works as intened

    Hey,

    given this code, I coded many mistakes:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <pthread.h>
    
    
    #define BUFLEN 255
    #define MAX_CONNECTIONS 128
    
    
    void* job_read(void * p);
    void* job_write(void * p);
    
    
    int socket_ids[MAX_CONNECTIONS];
    char endprogramm = 0;
    int open_cnncts;
    pthread_mutex_t mutex;
    
    
    void error(const char* msg){
        perror(msg);
        exit(1);
    }
    
    
    int main(int argc, char* argv[]) {
        if(argc < 2){
            fprintf(stderr, "You must provide a port number");
            exit(EXIT_FAILURE);
        }
        int sockfd, portnum;
        
        if(pthread_mutex_init(&mutex, NULL)<0){
            error("Could not initialize Mutex");
        }
        pthread_t readthreads[MAX_CONNECTIONS];
        pthread_t writethreads[MAX_CONNECTIONS];
        
        struct sockaddr_in serv_add, cli_adr;
        socklen_t clilen;
        
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if(sockfd < 0){
            error("Error opening socket.");
        }
        bzero((char*)&serv_add, sizeof(struct sockaddr_in));
        portnum = atoi(argv[1]);
        
        serv_add.sin_family = AF_INET;
        serv_add.sin_addr.s_addr = INADDR_ANY;
        serv_add.sin_port = htons(portnum);
        
        if(bind(sockfd, (struct sockaddr*) (&serv_add), sizeof(serv_add)) < 0){
            error("Binding failed.");
        }
        
    
    
        for(open_cnncts = 0; !endprogramm; open_cnncts++){
            listen(sockfd, MAX_CONNECTIONS);
            clilen = sizeof(cli_adr);
            
            int newsockfd = accept(sockfd, (struct sockaddr*) &cli_adr, &clilen);
            printf("Client connected.\n");
            socket_ids[open_cnncts] = newsockfd;
            pthread_t read_thread;
            pthread_t write_thread;
            pthread_create(&read_thread, NULL, job_read((void*)&newsockfd), NULL);
            pthread_create(&write_thread, NULL, job_write((void*)&newsockfd), NULL);
            
            readthreads[open_cnncts] = read_thread;
            writethreads[open_cnncts] = write_thread;
            
        }
        close(sockfd);
        for(; open_cnncts != 0; open_cnncts--){
            close(socket_ids[open_cnncts]);
            pthread_join(writethreads[open_cnncts], NULL);
            pthread_join(readthreads[open_cnncts], NULL);
        }
        pthread_mutex_destroy(&mutex);
        return 0;
    
    
    }
    
    
    void* job_read(void * p){
        int* socketp = (int*)p;
        int newsockfd = (*socketp);
        size_t n;
        char buffer[BUFLEN];
        while(!endprogramm){
            bzero(buffer, BUFLEN);
            n = read(newsockfd, buffer, BUFLEN);
            if(!n){
                error("Reading Failed");
            }
            printf("Client: %s\n", buffer);
            
            pthread_mutex_lock(&mutex);
            for(int i = 0; i < open_cnncts; i++){
                n = write(socket_ids[i], buffer, strlen(buffer));
                if(n < 0){
                    error("Writing failed");
                }
            }
            pthread_mutex_unlock(&mutex);
        }
        return NULL;
    }
    
    
    void* job_write(void * p){
        int* socketp = (int*)p;
        int newsockfd = (*socketp);
        size_t n;
        char buffer[BUFLEN];
        while(!endprogramm) {
            bzero(buffer, BUFLEN);
            fgets(buffer, BUFLEN, stdin);
            n = write(newsockfd, buffer, strlen(buffer));
            if(n < 0){
                error("Writing failed");
            }
            int i = strncmp("Bye", buffer, 3);
            if(i == 0){
                break;
            }
        }
        endprogramm = 1;
        return NULL;
    }
    This code just does not yet work.
    The first phenomenon I see here:

    Code:
     for(open_cnncts = 0; !endprogramm; open_cnncts++){        listen(sockfd, MAX_CONNECTIONS);
            clilen = sizeof(cli_adr);
            
            int newsockfd = accept(sockfd, (struct sockaddr*) &cli_adr, &clilen);
            printf("Client connected.\n");
            socket_ids[open_cnncts] = newsockfd;
            pthread_t read_thread;
            pthread_t write_thread;
            pthread_create(&read_thread, NULL, job_read((void*)&newsockfd), NULL);
            pthread_create(&write_thread, NULL, job_write((void*)&newsockfd), NULL);
            
            readthreads[open_cnncts] = read_thread;
            writethreads[open_cnncts] = write_thread;
            
        }
    "Client connected.", does not get printed, but the Server is able to receive messages from the client. However unable to send messages. When the server, however, receives a message, the message and "Client connected" gets printed...

    What would your idea be to this particular bug.

    Here is the Client:

    'Untitled Post' | TextUploader.com

    EDIT: I have spotted the bug.

    Code:
    for(open_cnncts = 0; !endprogramm; open_cnncts++){
            listen(sockfd, MAX_CONNECTIONS);
            clilen = sizeof(cli_adr);
    
    int newsockfd = accept(sockfd, (struct sockaddr*) &cli_adr, &clilen);
            printf("Client connected.\n");
            socket_ids[open_cnncts] = newsockfd;
            pthread_t read_thread;
            pthread_t write_thread;
            pthread_create(&read_thread, NULL, job_read((void*)&newsockfd), NULL);
            pthread_create(&write_thread, NULL, job_write((void*)&newsockfd), NULL);
    
            readthreads[open_cnncts] = read_thread;
            writethreads[open_cnncts] = write_thread;
    
        }
    Replace with:

    Code:
    for(open_cnncts = 0; !endprogramm; open_cnncts++){
            listen(sockfd, MAX_CONNECTIONS);
            clilen = sizeof(cli_adr);
    
    int newsockfd = accept(sockfd, (struct sockaddr*) &cli_adr, &clilen);
            printf("Client connected.\n");
            socket_ids[open_cnncts] = newsockfd;
            pthread_t read_thread;
            pthread_t write_thread;
            pthread_create(&read_thread, NULL, job_read((void*)&socket_ids[open_cnncts]), NULL);
            pthread_create(&write_thread, NULL, job_write((void*)&socket_ids[open_cnncts]), NULL);
    
            readthreads[open_cnncts] = read_thread;
            writethreads[open_cnncts] = write_thread;
    
        }
    However this does not make much sense, why this fixes the first bug?
    Last edited by SuchtyTV; 07-14-2019 at 04:51 AM.

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    441
    These lines:
    Code:
    pthread_create(&read_thread, NULL, job_read((void*)&newsockfd), NULL); 
    pthread_create(&write_thread, NULL, job_write((void*)&newsockfd), NULL);
    Are WRONG. They should be:
    Code:
    pthread_create( &read_thread, NULL, job_read, &newsockfd );
    pthread_create( &write_thread, NULL, job_write, &newsockfd );
    Notice the 3rd argument... You are calling the function, but pthread_create requires a pointer to a function. The way it is pthread_create is receiving NULL after the function returns and, since you aren't checking for errors, no threads are created.
    Last edited by flp1969; 07-14-2019 at 06:00 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux/Unix time server
    By Annonymous in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-19-2011, 03:33 PM
  2. unix makefile won't work but works in Dev C++
    By jk1998 in forum C++ Programming
    Replies: 1
    Last Post: 06-09-2007, 03:54 PM
  3. Works on PC, not on UNIX
    By Decrypt in forum C++ Programming
    Replies: 14
    Last Post: 04-20-2006, 01:13 PM
  4. works in Borland, does not work in Unix T_T
    By dot_rain in forum C Programming
    Replies: 1
    Last Post: 03-14-2004, 10:22 AM
  5. Server Client on UNIX
    By Wisefool in forum C Programming
    Replies: 5
    Last Post: 10-23-2003, 04:05 PM

Tags for this Thread