Thread: Where I'm wrong in this C code

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Where I'm wrong in this C code

    Hi,
    I'm writing C application which uses Unix Domain Socket for communication. But I can't write the threads in the rigth way. Would you tell me where is my mistake and what is the appropriate way to write it.

    How it works:
    The idea it to have main thread wich is listening for incomming connections. When the main thread accept connection it passes it to second thread which performs the communication. The server is implemented as Linux daemon.

    Server:

    Code:
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <unistd.h>
    #include <syslog.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <sys/un.h>
    #include <signal.h>
    
    #include <pthread.h>
    
    #define NTHREADS 5
    
    void *main_thread_function(void *);
    void *thread_function(void *client_sockfd);
    
    	    int server_sockfd, client_sockfd;
    	    int server_len, client_len;
    	    struct sockaddr_un server_address;
    	    struct sockaddr_un client_address;
    
    void *main_thread_function(void *){
    
    		/*  Accept connection.  */
    		client_len = sizeof(client_address);
    		client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);
    
    		int i, j;
    
    		for(i=0; i < NTHREADS; i++)
    		   {
    		      pthread_create( &thread_id[i], NULL, thread_function, client_sockfd );
    		   }
    
    		   for(j=0; j < NTHREADS; j++)
    		   {
    		      pthread_join( thread_id[j], NULL); 
    		   }
    				
    
    void *thread_function(void *client_sockfd){
    				
    		/* Accept data */
    		char ch;		
    		read(client_sockfd, &ch, 1);
    		ch++;
    		write(client_sockfd, &ch, 1);
    		
    	}
    }
    
    int main(void) {
            
            /* Our process ID and Session ID */
            pid_t pid, sid;
            
            /* Fork off the parent process */
            pid = fork();
            if (pid < 0) {
                    exit(EXIT_FAILURE);
            }
            /* If we got a good PID, then
               we can exit the parent process. */
            if (pid > 0) {
                    exit(EXIT_SUCCESS);
            }
    
            /* Change the file mode mask */
            umask(0);
                    
            /* Open any logs here */        
                    
            /* Create a new SID for the child process */
            sid = setsid();
            if (sid < 0) {
                    /* Log the failure */
                    exit(EXIT_FAILURE);
            }
                    
            /* Change the current working directory */
            if ((chdir("/")) < 0) {
                    /* Log the failure */
                    exit(EXIT_FAILURE);
            }
            
            /* Close out the standard file descriptors */
            close(STDIN_FILENO);
            close(STDOUT_FILENO);
            close(STDERR_FILENO);
            
            /* Daemon-specific initialization goes here */
            
            /* The Big Loop */
            while (1) {
    	    
    
    	/*  Remove any old socket and create an unnamed socket for the server.  */
    
    	    unlink("/tmp/server_socket");
    	    server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
    
    	/*  Name the socket.  */
    
    	    server_address.sun_family = AF_UNIX;
    	    strcpy(server_address.sun_path, "/tmp/server_socket");
    	    server_len = sizeof(server_address);
    	    bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
    
    	/*  Create a connection queue and wait for clients.  */
    
    	    listen(server_sockfd, 5);
    
    	    signal(SIGCHLD, SIG_IGN);
    
    //	    while(1) {
    
    		
    
    	/* Create threads */
    
    		   pthread_t main_thread, thread_id[NTHREADS];
    		   
    		   pthread_create( &main_thread, NULL, thread_function, NULL );
    
    		  				
    //	    }
    
    		close(client_sockfd);
                        
            }
       exit(EXIT_SUCCESS);
    }
    Client:

    Code:
    /*  Make the necessary includes and set up the variables.  */
    
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <stdio.h>
    #include <sys/un.h>
    #include <unistd.h>
    #include <stdlib.h>
    
    int main()
    {
        int sockfd;
        int len;
        struct sockaddr_un address;
        int result;
        char ch = 'B';
    
    /*  Create a socket for the client.  */
    
        sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
    
    /*  Name the socket, as agreed with the server.  */
    
        address.sun_family = AF_UNIX;
        strcpy(address.sun_path, "/tmp/server_socket");
        len = sizeof(address);
    
    /*  Now connect our socket to the server's socket.  */
    
        result = connect(sockfd, (struct sockaddr *)&address, len);
    
        if(result == -1) {
            perror("oops: client1");
            exit(1);
        }
    
    /*  We can now read/write via sockfd.  */
    
        write(sockfd, &ch, 1);
        read(sockfd, &ch, 1);
        printf("char from server = %c\n", ch);
        close(sockfd);
        exit(0);
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    Does this even compile? Seems unlikely...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with my code?
    By x2x3i5x in forum C Programming
    Replies: 6
    Last Post: 09-28-2009, 11:52 AM
  2. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  3. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  4. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM