c code for a server that creates a log file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nosia
    New Member
    • Mar 2013
    • 14

    c code for a server that creates a log file

    hello everyone
    i am trying to write a c code for a server that recieve msgs from a client and put that msg in a log file dedicated for that client along with the msg reception time and client IP addrs.......
    The log file must be created if it doesn't already exist.....
    here is a code that i wnat to customize and it already opens a communication channel between server and client......
    here is my code:


    Code:
    #include "stdio.h"
    #include "stdlib.h"
    #include "sys/socket.h"
    #include "sys/types.h"
    #include "netinet/in.h"
    #include "error.h"
    #include "strings.h"
    #include "unistd.h"
    #include "arpa/inet.h"
    
    #define ERROR    -1
    #define MAX_CLIENTS    4
    #define MAX_DATA    1024
    
    
    main(int argc, char **argv)
    {
        struct sockaddr_in server;
        struct sockaddr_in client;
        int sock;
        int new,i;
        int sockaddr_len = sizeof(struct sockaddr_in);
        int data_len;
        char data[MAX_DATA];
        char temp[MAX_DATA];
        
        
        if((sock = socket(AF_INET, SOCK_STREAM, 0)) == ERROR)
        {
            perror("server socket: ");
            exit(-1);
        }
            
        server.sin_family = AF_INET;
        server.sin_port = htons(atoi(argv[1]));
        server.sin_addr.s_addr = INADDR_ANY;
        bzero(&server.sin_zero, 8);
                
        if((bind(sock, (struct sockaddr *)&server, sockaddr_len)) == ERROR)
        {
            perror("bind : ");
            exit(-1);
        }
        
        if((listen(sock, MAX_CLIENTS)) == ERROR)
        {
            perror("listen");
            exit(-1);
        }
        printf("\nThe TCPServer Waiting for client on port %d\n",ntohs(server.sin_port));
            fflush(stdout);
        
        while(1) // Better signal handling required
        {
            if((new = accept(sock, (struct sockaddr *)&client, &sockaddr_len)) == ERROR)
            {
                perror("accept");
                exit(-1);
            }
            
            printf("New Client connected from port no %d and IP %s\n", ntohs(client.sin_port), inet_ntoa(client.sin_addr));
    
            data_len = 1;
        
                    
            while(data_len)
            {
                data_len = recv(new, data, MAX_DATA, 0);
                printf("\nRecieved mesg from client: %s", data);
                
                for( i = 0; data[ i ]; i++)
                {
                    if(data[i]=='a' || data[i]=='e' || data[i]=='i' ||data[i]=='o' || data[i]=='u' )
                        data[ i ] = toupper( data[ i ] );
                    else
                    data[ i ] = data[ i ] ;
                    
                }    
                
                if(data_len)
                {
                    
                    send(new, data, data_len, 0);
                    data[data_len] = '\0';
                    printf("\nSent mesg to client: %s", data);
                }
                
            }
    
            printf("Client disconnected\n");
            
            close(new);
            
        }
    
        close(sock);
    
            
    }
    Last edited by Banfa; Mar 12 '13, 09:23 AM. Reason: Added code tags
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    What is the problem?

    Comment

    • Nosia
      New Member
      • Mar 2013
      • 14

      #3
      the problem is that this code only recieve msg from client and set vowel letters to upper case.....i want it to recieve msg and put it in a log file along with the client IP address and time of msg reception...
      tanks for care....

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Well you know how to use printf it is a small step from printf to fprintf, fopen, fclose (and other file handling functions).

        Are you really saying you know how to open a socket but not how to open a file?

        Comment

        • Nosia
          New Member
          • Mar 2013
          • 14

          #5
          thanks Banfa i know how to open a socket but i tried hard to open a file using fopen and fclose and it compiled successfully but when executed it gives an error of "core dumped" type.......
          that is why am asking for help.......


          thanks...

          Comment

          • Nosia
            New Member
            • Mar 2013
            • 14

            #6
            #include "stdio.h"
            #include "stdlib.h"
            #include "sys/socket.h"
            #include "sys/types.h"
            #include "netinet/in.h"
            #include "error.h"
            #include "strings.h"
            #include "unistd.h"
            #include "arpa/inet.h"
            #include <fcntl.h>
            #include <unistd.h>





            #define ERROR -1
            #define MAX_CLIENTS 4
            #define MAX_DATA 1024
            #define true 1

            main(int argc, char **argv)
            {
            struct sockaddr_in server;
            struct sockaddr_in client;
            int sock;
            int new,i,fd;
            int sockaddr_len = sizeof(struct sockaddr_in);
            int data_len;
            char data[MAX_DATA];
            char temp[MAX_DATA];


            if((sock = socket(AF_INET, SOCK_STREAM, 0)) == ERROR)
            {
            perror("server socket: ");
            exit(-1);
            }

            server.sin_fami ly = AF_INET;
            server.sin_port = htons(atoi(argv[1]));
            server.sin_addr .s_addr = INADDR_ANY;
            bzero(&server.s in_zero, 8);

            if((bind(sock, (struct sockaddr *)&server, sockaddr_len)) == ERROR)
            {
            perror("bind : ");
            exit(-1);
            }

            if((listen(sock , MAX_CLIENTS)) == ERROR)
            {
            perror("listen" );
            exit(-1);
            }
            printf("\nThe TCPServer Waiting for client on port %d\n",ntohs(ser ver.sin_port));
            fflush(stdout);


            fd=open("test.l og",O_WRONLY | O_APPEND | O_CREAT,0644);
            if (!fd)
            {
            perror("file not open \n");
            return -1;
            }
            while(1) // Better signal handling required
            {
            if((new = accept(sock, (struct sockaddr *)&client, &sockaddr_le n)) == ERROR)
            {
            perror("accept" );
            exit(-1);
            }

            printf("New Client connected from port no %d and IP %s\n", ntohs(client.si n_port), inet_ntoa(clien t.sin_addr));

            data_len = 1;


            while(data_len)
            {
            data_len = recv(new, data, MAX_DATA, 0);
            printf("\nRecie ved mesg from client: %s", data);

            for( i = 0; data[ i ]; i++)
            {

            write(fd,data,M AX_DATA);
            bzero(&data,MAX _DATA);

            }

            }

            printf("Client disconnected\n" );

            close(new);

            }
            close(fd);
            close(sock);


            }



            here is the customized code but when executed it gives an error of type "core dumped"



            help please..

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              Code:
              data_len = recv(new, data, MAX_DATA, 0);
              printf("\nRecieved mesg from client: %s", data);
              Are you confident that the received data is a null-terminated string?

              Code:
              for( i = 0; data[ i ]; i++)
              {
                 write(fd,data,MAX_DATA);
                 bzero(&data,MAX_DATA);
              }
              This code iterates for each non-null character in data[], but each time it writes the entire array to the log file (regardless of how many bytes were actually received) and fills the data array with zeroes.

              Comment

              • Nosia
                New Member
                • Mar 2013
                • 14

                #8
                yes that is right and i solved the problem by taking out the for loop and use sprintf function......
                thank you donbock.....

                Comment

                Working...