Thread: elementary networking problem

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    6

    elementary networking problem

    I started with networking today..THis piece of code is giving me some problem, can some one help me??


    client side:

    Code:
      
      //sockfd is the socket descriptor that I am using to send and recv data. 
     
     int c,i=1;
     FILE *fp;
     char *buf1;
     cahr *buf11; 
     buf1=malloc(sizeof(char)*50);//the starting address of hte space allocated is in buf1
     
     buf11=buf1; //store the starting address in buf11
     fp=fopen("text.c","r");
     
     while((c=getc(fp))!=EOF)
          {
               *buf1=c;
                 buf1++;
                 i++;
    
          }
      fclose(fp);
      *buf1= '\0' ;              // IS THIS REQD??   
    
      buf1=buf11; 
              //buf1 is now pointing to the starting point of the segment allocated. 
             //i+1 is the total number of bytes to be sent
      size=send(sockfd,buf1,i+1,0);
      printf("bytes sent : %d\n",size);
      printf("bytes to be sent %d\n",i+1);

    server side :

    Code:
      // new_fd is the socket descriptor returned by accept()
      
      int size;
      char *buf2;
      buf2= malloc(sizeof(char)*50);
     
      size=recv(new_fd,buf2,50,0);
      printf("bytes recvd :%d\n",size);
      prirntf("%s",buf2);


    I am able to recv only 4 or 5 characters ,though the size parameter(in the CLIENT side) has the right value ..and I get a segemntation fault..can some one help me out

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Well you need to mess around with your code to zero in on the problem. The first thing I would do is change the server code to:

    Code:
    int size;
    char *buf2;
    buf2= malloc(sizeof(char)*50);
    memset(buf2,0,50);
    size=recv(new_fd,buf2,49,0);
    printf("bytes recvd :%d\n",size);
    printf("%s",buf2);
    This ensures that your buffer is null terminated before you print it out. This will probably get rid of your seg fault. The biggest problem I see in your client code is that the can easily get overwritten. You might want to do a simple check like:
    Code:
     while((c=getc(fp))!=EOF)
          {
               *buf1=c;
                 buf1++;
                 if(i++ > 50)
                     break;
    
          }

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Careful you're not sending too much. In the OP's code, the send()'s buffer length is off by 1, sending one garbage byte. (You initialize it to 1, instead of 0. If I feed it a blank file, i is never incremented, so i=1, and i+1 = 2 in send(), but you're only sending \0.)

    Bithub, your code doesn't fix the segfault - it will be one iteration too late, as you've already stored the value. The best way in my opinion is to rewrite the loop to something better, perhaps:
    Code:
    for(i = 0; (c = fgetc(fp)) != EOF && i < 49; ++i)
    {
      buf[i] = c;
    }
    // i represents bytes read. At most, i = 49, and 1 byte remains for your \0.
    buf[i++] = '\0';
    size = send(sockfd, buf, i, 0);
    In your code, let variable i represent something. The best I can see is "bytes read, +1", which is too much. Simplify to "bytes read", as my above example...
    EDIT: My example was waaay out of wack. Fixed. *hopes noone saw...*
    Last edited by Cactus_Hugger; 06-26-2006 at 10:26 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Need Homework (Multi-threading & Networking)
    By Kurisu33 in forum C++ Programming
    Replies: 4
    Last Post: 12-23-2006, 06:00 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM