Thread: TCP/IP server/client programm +files

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

    TCP/IP server/client programm +files

    Hello all , i have a homework that is related with tcp/ip server/client. The project says :

    We sent from client to server , 2 file names , server opens them and sends back the data from the two files back to client and client prints them on the screen.

    The code i made is

    //client

    Code:
     #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h> 
    #define SERVER "127.0.0.1"
    #define TCP_PORT 6000
    
    void error(const char *msg)
    {
        perror(msg);
        exit(0);
    }
    
    int main(int argc, char *argv[])
    {
        int sockfd, n;
        struct sockaddr_in serv_addr;
        char buffer[256];
        /*if (argc < 3) {
           fprintf(stderr,"usage %s hostname port\n", argv[0]);
           exit(0);
        }*/
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0) 
            error("ERROR opening socket");
        bzero((char *) &serv_addr, sizeof(serv_addr));
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_addr.s_addr=inet_addr(SERVER);
        serv_addr.sin_port = htons(TCP_PORT);
        if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
            error("ERROR connecting");
        //printf("Please enter the message: ");
        bzero(buffer,256);
        //fgets(buffer,255,stdin);
       strcpy(buffer,"file1.txt");
       strcat(buffer," ");
       strcat(buffer,"file2.txt");
        n = write(sockfd,buffer,strlen(buffer));
        if (n < 0) 
             error("ERROR writing to socket");
        bzero(buffer,256);
        n = read(sockfd,buffer,255);
        if (n < 0) 
             error("ERROR reading from socket");
        printf("%s\n",buffer);
        close(sockfd);
        return 0;
    }
    //server
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h> 
    #include <sys/socket.h>
    #include <netinet/in.h>
    #define server 6000
    
     void dostuff(int); 
    void error(const char *msg)
    {
        perror(msg);
        exit(1);
    }
    
    int main(int argc, char *argv[])
    {
         int sockfd, newsockfd, portno, pid;
         socklen_t clilen;
         struct sockaddr_in serv_addr, cli_addr;
    
         sockfd = socket(AF_INET, SOCK_STREAM, 0);
         if (sockfd < 0) 
            error("ERROR opening socket");
         bzero((char *) &serv_addr, sizeof(serv_addr));
         portno = server;
         serv_addr.sin_family = AF_INET;
         serv_addr.sin_addr.s_addr = INADDR_ANY;
         serv_addr.sin_port = htons(portno);
         if (bind(sockfd, (struct sockaddr *) &serv_addr,
                  sizeof(serv_addr)) < 0) 
                  error("ERROR on binding");
         listen(sockfd,5);
         clilen = sizeof(cli_addr);
         while (1) {
             newsockfd = accept(sockfd, 
                   (struct sockaddr *) &cli_addr, &clilen);
             if (newsockfd < 0) 
                 error("ERROR on accept");
             pid = fork();
             if (pid < 0)
                 error("ERROR on fork");
             if (pid == 0)  {
                 close(sockfd);
                 dostuff(newsockfd);
                 exit(0);
             }
             else close(newsockfd);
         } /* end of while */
         close(sockfd);
         return 0; /* we never get here */
    }
    
    void dostuff (int sock)
    { 
       int n;
       char buffer[256];
      FILE *pFile1,*pFile2;
      long lSize1,lSize2;
      char * buffer1,*buffer2,*buffer3;
      size_t file1str,file2str;
      char *file1,*file2;
          
       n = read(sock,buffer,255);
       if (n < 0) error("ERROR reading from socket");
       sscanf(buffer,"%s %s",file1,file2);
    	
            //opening and writing the first file in a buffer1
    	pFile1 = fopen ( file1, "rb" );
    	if (pFile1==NULL) {fputs ("File error",stderr); }
    
      	
      	fseek (pFile1 , 0 , SEEK_END);
      	lSize1 = ftell (pFile1);
     	 rewind (pFile1);
    
      	
      	buffer1 = (char*) malloc (sizeof(char)*lSize2);
     	 if (buffer1 == NULL) {fputs ("Memory error",stderr);}
    
     	 file1str = fread (buffer1,1,lSize2,pFile1);
     	 if (file1str!= lSize2) {fputs ("Reading error",stderr);}
    
    	 fclose (pFile1);
    
            //opening and writing the second  file in  buffer2
    	pFile2 = fopen ( file2, "rb" );
     	 if (pFile2==NULL) {fputs ("File error",stderr); }
    
    
      	fseek (pFile2 , 0 , SEEK_END);
     	 lSize2 = ftell (pFile2);
      	rewind (pFile2);
    
      	
      	buffer2 = (char*) malloc (sizeof(char)*lSize2);
      	if (buffer2 == NULL) {fputs ("Memory error",stderr); }
    
      	file2str = fread (buffer2,1,lSize2,pFile2);
      	if (file2str != lSize2) {fputs ("Reading error",stderr); }
    	
    	fclose(pFile2);
    	
    	printf("%s  %s",buffer1, buffer2);
    
        strcpy(buffer3,buffer1);
        strcat(buffer3,buffer2);
       n = write(sock,buffer3,256);
       if (n < 0) error("ERROR writing to socket");
    }


    Now my problem is . Server is up and running! No problem with this. When i try to connect the client , client returns me an empty line and after it closes.


    Can you help me with this? I think i have made some mistakes passing the string in fopen or something and i am not familiar with debugging >.>
    Last edited by rounsmith; 04-03-2011 at 05:04 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Your server is not fine. Yes it runs, but it has a number of problems, mostly in dostuff. Your client, on the other hand, seems to be fine. A couple of notes first:
    1. Don't cast malloc
    2. bzero is deprecated. Use memset(foo, 0, size) instead
    3. You can resue the same code for reading file1 and file2 in dostuff() since it's basically identical. Move everything from the file open to file close, with appropriate error checks, into a function. Have it return the contents of the file in a malloc'ed buffer, or NULL on error.


    Can you help me with this? I think i have made some mistakes passing the string in fopen or something and i am not familiar with debugging >.>
    You need to get familiar with debugging. There's no two ways about it. You would have solved this in about 10 minutes if you knew how to use a debugger instead of waiting 5 hours for me to help you. Good debugging skills, including using a debugger like GDB, are invaluable to a programmer. Check out our tutorial, and Google around for some others. The GDB website has a lot of good info too.

    //server
    Code:
    void dostuff (int sock)
    { 
       int n;
       char buffer[256];
      FILE *pFile1,*pFile2;
      long lSize1,lSize2;
      char * buffer1,*buffer2,*buffer3;
      size_t file1str,file2str;
      char *file1,*file2;  // You need a place to store the file names.  Try file1[100]
          
       n = read(sock,buffer,255);
       if (n < 0) error("ERROR reading from socket");
       sscanf(buffer,"%s %s",file1,file2);
        
            //opening and writing the first file in a buffer1
        pFile1 = fopen ( file1, "rb" );
        if (pFile1==NULL) {fputs ("File error",stderr); }
        // You shouldn't check the size of or read from a NULL file pointer.
        // skip all this if pFile1 is NULL
          
          fseek (pFile1 , 0 , SEEK_END);
          lSize1 = ftell (pFile1);
          rewind (pFile1);
    
          
          buffer1 = (char*) malloc (sizeof(char)*lSize2);  // lSize1
          if (buffer1 == NULL) {fputs ("Memory error",stderr);}
          // Again, you need to skip the following code if buffer1 is NULL
          // otherwise you will probably seg fault, trying to read into a NULL buffer
    
          file1str = fread (buffer1,1,lSize2,pFile1);  // lSize1
          if (file1str!= lSize2) {fputs ("Reading error",stderr);}  // lSize1
    
         fclose (pFile1);
    
            //opening and writing the second  file in  buffer2
        pFile2 = fopen ( file2, "rb" );
          if (pFile2==NULL) {fputs ("File error",stderr); }
         // You shouldn't check the size of or read from a NULL file pointer.
         // skip all this if pFile1 is NULL
    
          fseek (pFile2 , 0 , SEEK_END);
          lSize2 = ftell (pFile2);
          rewind (pFile2);
    
          
          buffer2 = (char*) malloc (sizeof(char)*lSize2);
          if (buffer2 == NULL) {fputs ("Memory error",stderr); }
          // Again, you need to skip the following code if buffer1 is NULL
          // otherwise you will probably seg fault, trying to read into a NULL buffer
     
          file2str = fread (buffer2,1,lSize2,pFile2);
          if (file2str != lSize2) {fputs ("Reading error",stderr); }
        
        fclose(pFile2);
        
        printf("%s  %s",buffer1, buffer2);
    
        // You need to allocate sufficient space for buffer3 before writing to it
        strcpy(buffer3,buffer1);
        strcat(buffer3,buffer2);
       n = write(sock,buffer3,256);  // You want to write strlen(buffer3)
       if (n < 0) error("ERROR writing to socket");
    }
    Last edited by anduril462; 04-03-2011 at 10:23 AM. Reason: Removed excess code

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Thank you very much ! i actually found out how it works now... and made a like nice debugging to find logical mistakes (putting printf in some lines to see if i get the correct messages etc). Thank you for the response though very much !!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-11-2009, 06:45 AM
  2. Header files and multiple definitions
    By sjweinberg in forum C++ Programming
    Replies: 16
    Last Post: 07-17-2009, 05:59 PM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM