Thread: sending /proc/cpuinfo across the network one character at a time.

  1. #1
    Registered User
    Join Date
    Aug 2003
    Location
    Birmingham, UK
    Posts
    12

    sending /proc/cpuinfo across the network one character at a time.

    Greet's all, i've been playing about with creating sockets under linux. I have played about with an example from the book that i've been reading from so that it will open /proc/cpuinfo as read only and send the characters across the network to a client that connects to it, one at a time.

    What i receive, when trying to compile it is -
    testserver.c: In function `main':
    testserver.c:81: warning: passing arg 1 of `strlen' makes pointer from integer without a cast
    testserver.c:81: warning: passing arg 2 of `write' makes pointer from integer without a cast
    I'm not to sure what the problem is, would somebody be kind enough to help me out

    Here is the source code:-
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    
    int main(int argc, char *argv[]) {
      FILE *fp;
      char c;
      int simplesocket = 0;
      int simpleport = 0;
      int returnstatus = 0;
      struct sockaddr_in simpleserver;
    
      
      /*if argc does not have two arguemtns then print error message to screen */
      if( 2 != argc) {
        fprintf(stderr, "Usage: %s <port>\n",argv[0]);
        exit(1);
      }
       
    
      simplesocket = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);
      if(simplesocket == -1){
        fprintf(stderr, "could not create socket\n");
      exit(1);
      }
      else {
        printf("socket created\n"); 
      }
    
    
      simpleport = atoi(argv[1]);
    
      bzero(&simpleserver,sizeof(simpleserver));
      simpleserver.sin_family = AF_INET;
      simpleserver.sin_addr.s_addr = htonl(INADDR_ANY);
      simpleserver.sin_port = htons(simpleport);
    
      returnstatus = bind(simplesocket,(struct sockaddr *)&simpleserver,sizeof(simpleserver));
    
      if(returnstatus == 0 ){
      fprintf(stderr,"bind completed\n");
      }
      else {
        fprintf(stderr, "could not bind to address\n");
        close(simplesocket);
        exit(1);
      }
    
      returnstatus = listen(simplesocket, 5);
      if(returnstatus == -1) {
        fprintf(stderr, "cannot listen on socket\n");
        close(simplesocket);
        exit(1);
      }
       
      /*Open file for reading which will be put into variable c and sent accross the network
        one character at a time*/
    
        fp = fopen("/proc/cpuinfo","r");
        {
     
          struct sockaddr_in clientname = { 0 };
          int simplechildsocket = 0 ;
          int clientnamelength = sizeof(clientname);
    
          simplechildsocket = accept(simplesocket, (struct sockaddr *)&clientname,&clientnamelength);
    
          if(simplechildsocket == -1) {
    	fprintf(stderr, "cannot accept connections\n");
    	close(simplesocket);
    	exit(1);
          }
    
            while (c!= EOF)
    	  {
               c=getc(fp); 
               write(simplechildsocket,c,strlen(c));
               close(simplechildsocket);
    	  }
    
      }
      fclose(fp);
      close(simplesocket);
      return 0;
    }

    Thank you,
    Kevin Joy
    Last edited by kevinj; 06-11-2005 at 02:10 AM.
    www.unlocked-networks.com

  2. #2
    -AppearingOnThis..........
    Join Date
    May 2005
    Location
    Netherlands
    Posts
    44
    I think you want for your loop this instead:
    Code:
            while (c!= EOF)
    	  {
               c=getc(fp); 
               write(simplechildsocket,&c,1);
    	  }
    And lose the close() as you'd be closing a socket while you're trying to write to it.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Location
    Birmingham, UK
    Posts
    12
    Works Great.

    You are a diamond, Thank you very much buddy
    www.unlocked-networks.com

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    use send() function to send the data across socket. If you are planning to send the the data one byte at a time then then the size of the data sent by send() is 1. (1 byte, single char )

    Also, its a good idea to send a multi-byte data than a single byte at a time. That will be faster compared to sending one byte at a time. Less disk I/Os.

    you can read a line from the file and send it across

    Code:
    char buffer[514];
    
    FILE *fp;
    
    int bytes_sent;
    
    fp = fopen("/proc/cpuinfo","r");
    
    while ((fgets (buffer,sizeof(buffer),fp)) != NULL)
    {
       bytes_sent=send (simplechildsocket,(char *)buffer,sizeof(buffer),0);
      
      if (bytes_sent == -1)
     {
         perror("send");
     } else {
         printf("\nBytes Sent: %d\n",bytes_sent);
     }
    }

  5. #5
    Registered User
    Join Date
    Aug 2003
    Location
    Birmingham, UK
    Posts
    12
    Quote Originally Posted by nkhambal
    use send() function to send the data across socket. If you are planning to send the the data one byte at a time then then the size of the data sent by send() is 1. (1 byte, single char )

    Also, its a good idea to send a multi-byte data than a single byte at a time. That will be faster compared to sending one byte at a time. Less disk I/Os.

    you can read a line from the file and send it across

    Code:
    char buffer[514];
    
    FILE *fp;
    
    int bytes_sent;
    
    fp = fopen("/proc/cpuinfo","r");
    
    while ((fgets (buffer,sizeof(buffer),fp)) != NULL)
    {
       bytes_sent=send (simplechildsocket,(char *)buffer,sizeof(buffer),0);
      
      if (bytes_sent == -1)
     {
         perror("send");
     } else {
         printf("\nBytes Sent: %d\n",bytes_sent);
     }
    }
    Greets nkhambal, thanks for your reply. I see the logic behind sending a chunk of data rather than 1byte at a time. I have amended the source using send() as you suggested but receive odd output from a client that connects -

    rocessor : 0
    qìó{@òÎ@òÎ@ Ï@°}@Ðó@T@W@M@V@Ò@ôok@Ò@¹
    à*@LôàP@pW@Q@@ô|¨Q@M@Ëô{@òÎT@ Ï@°}@Ðó@T@W@M@,Q0õoàôàP@0W@àôpõO@Ò@à*@p@T@Äõ$õ¤@/@I
    @÷ÄõÑ @
    v@v@$õ@
    v@ÐõM@¡O@õ¾¢@,Q@0W@Ψ@pw@?|@hõvendor_id : AuthenticAMD
    qìó{@òÎ@òÎ@ Ï@°}@Ðó@T@W@M@V@Ò@ôok@Ò@¹
    à*@LôàP@pW@Q@@ô|¨Q@M@Ëô{@òÎT@ Ï@°}@Ðó@T@W@M@,Q0õoàôàP@0W@àôpõO@Ò@à*@p@T@Äõ$õ¤@/@I
    @÷ÄõÑ @
    v@v@$õ@
    v@ÐõM@¡O@õ¾¢@,Q@0W@Ψ@pw@?|@hõcpu family : 6
    enticAMD
    qìó{@òÎ@òÎ@ Ï@°}@Ðó@T@W@M@V@Ò@ôok@Ò@¹
    à*@LôàP@pW@Q@@ô|¨Q@M@Ëô{@òÎT@ Ï@°}@Ðó@T@W@M@,Q0õoàôàP@0W@àôpõO@Ò@à*@p@T@Äõ$õ¤@/@I
    @÷ÄõÑ @
    v@v@$õ@
    v@ÐõM@¡O@õ¾¢@,Q@0W@Ψ@pw@?|@hõmodel : 8
    6
    enticAMD
    qìó{@òÎ@òÎ@ Ï@°}@Ðó@T@W@M@V@Ò@ôok@Ò@¹
    à*@LôàP@pW@Q@@ô|¨Q@M@Ëô{@òÎT@ Ï@°}@Ðó@T@W@M@,Q0õoàôàP@0W@àôpõO@Ò@à*@p@T@Äõ$õ¤@/@I
    @÷ÄõÑ @
    v@v@$õ@
    v@ÐõM@¡O@õ¾¢@,Q@0W@Ψ@pw@?|@hõmodel name : AMD Athlon(tm) XP 2600+
    qìó{@òÎ@òÎ@ Ï@°}@Ðó@T@W@M@V@Ò@ôok@Ò@¹
    à*@LôàP@pW@Q@@ô|¨Q@M@Ëô{@òÎT@ Ï@°}@Ðó@T@W@M@,Q0õoàôàP@0W@àôpõO@Ò@à*@p@T@Äõ$õ¤@/@I
    @÷ÄõÑ @
    v@v@$õ@
    v@ÐõM@¡O@õ¾¢@,Q@0W@Ψ@pw@?|@hõstepping : 1
    MD Athlon(tm) XP 2600+
    qìó{@òÎ@òÎ@ Ï@°}@Ðó@T@W@M@V@Ò@ôok@Ò@¹
    à*@LôàP@pW@Q@@ô|¨Q@M@Ëô{@òÎT@ Ï@°}@Ðó@T@W@M@,Q0õoàôàP@0W@àôpõO@Ò@à*@p@T@Äõ$õ¤@/@I
    @÷ÄõÑ @
    v@v@$õ@
    v@ÐõM@¡O@õ¾¢@,Q@0W@Ψ@pw@?|@hõcpu MHz : 2134.257
    on(tm) XP 2600+
    qìó{@òÎ@òÎ@ Ï@°}@Ðó@T@W@M@V@Ò@ôok@Ò@¹
    à*@LôàP@pW@Q@@ô|¨Q@M@Ëô{@òÎT@ Ï@°}@Ðó@T@W@M@,Q0õoàôàP@0W@àôpõO@Ò@à*@p@T@Äõ$õ¤@/@I
    @÷ÄõÑ @
    v@v@$õ@
    v@ÐõM@¡O@õ¾¢@,Q@0W@Ψ@pw@?|@hõcache size : 256 KB
    on(tm) XP 2600+
    qìó{@òÎ@òÎ@ Ï@°}@Ðó@T@W@M@V@Ò@ôok@Ò@¹
    à*@LôàP@pW@Q@@ô|¨Q@M@Ëô{@òÎT@ Ï@°}@Ðó@T@W@M@,Q0õoàôàP@0W@àôpõO@Ò@à*@p@T@Äõ$õ¤@/@I
    @÷ÄõÑ @
    v@v@$õ@
    v@ÐõM@¡O@õ¾¢@,Q@0W@Ψ@pw@?|@hõfdiv_bug : no
    6 KB
    on(tm) XP 2600+
    qìó{@òÎ@òÎ@ Ï@°}@Ðó@T@W@M@V@Ò@ôok@Ò@¹
    à*@LôàP@pW@Q@@ô|¨Q@M@Ëô{@òÎT@ Ï@°}@Ðó@T@W@M@,Q0õoàôàP@0W@àôpõO@Ò@à*@p@T@Äõ$õ¤@/@I
    @÷ÄõÑ @
    v@v@$õ@
    v@ÐõM@¡O@õ¾¢@,Q@0W@Ψ@pw@?|@hõhlt_bug : no
    6 KB
    on(tm) XP 2600+
    qìó{@òÎ@òÎ@ Ï@°}@Ðó@T@W@M@V@Ò@ôok@Ò@¹
    à*@LôàP@pW@Q@@ô|¨Q@M@Ëô{@òÎT@ Ï@°}@Ðó@T@W@M@,Q0õoàôàP@0W@àôpõO@Ò@à*@p@T@Äõ$õ¤@/@I
    @÷ÄõÑ @
    v@v@$õ@
    v@ÐõM@¡O@õ¾¢@,Q@0W@Ψ@pw@?|@hõf00f_bug : no
    6 KB
    on(tm) XP 2600+
    qìó{@òÎ@òÎ@ Ï@°}@Ðó@T@W@M@V@Ò@ôok@Ò@¹
    à*@LôàP@pW@Q@@ô|¨Q@M@Ëô{@òÎT@ Ï@°}@Ðó@T@W@M@,Q0õoàôàP@0W@àôpõO@Ò@à*@p@T@Äõ$õ¤@/@I
    @÷ÄõÑ @
    v@v@$õ@
    v@ÐõM@¡O@õ¾¢@,Q@0W@Ψ@pw@?|@hõcoma_bug : no
    6 KB
    on(tm) XP 2600+
    qìó{@òÎ@òÎ@ Ï@°}@Ðó@T@W@M@V@Ò@ôok@Ò@¹
    à*@LôàP@pW@Q@@ô|¨Q@M@Ëô{@òÎT@ Ï@°}@Ðó@T@W@M@,Q0õoàôàP@0W@àôpõO@Ò@à*@p@T@Äõ$õ¤@/@I
    @÷ÄõÑ @
    v@v@$õ@
    v@ÐõM@¡O@õ¾¢@,Q@0W@Ψ@pw@?|@hõfpu : yes
    o
    6 KB
    on(tm) XP 2600+
    qìó{@òÎ@òÎ@ Ï@°}@Ðó@T@W@M@V@Ò@ôok@Ò@¹
    à*@LôàP@pW@Q@@ô|¨Q@M@Ëô{@òÎT@ Ï@°}@Ðó@T@W@M@,Q0õoàôàP@0W@àôpõO@Ò@à*@p@T@Äõ$õ¤@/@I
    @÷ÄõÑ @
    v@v@$õ@
    v@ÐõM@¡O@õ¾¢@,Q@0W@Ψ@pw@?|@hõfpu_exception : yes
    on(tm) XP 2600+
    qìó{@òÎ@òÎ@ Ï@°}@Ðó@T@W@M@V@Ò@ôok@Ò@¹
    à*@LôàP@pW@Q@@ô|¨Q@M@Ëô{@òÎT@ Ï@°}@Ðó@T@W@M@,Q0õoàôàP@0W@àôpõO@Ò@à*@p@T@Äõ$õ¤@/@I
    @÷ÄõÑ @
    v@v@$õ@
    v@ÐõM@¡O@õ¾¢@,Q@0W@Ψ@pw@?|@hõcpuid level : 1
    es
    on(tm) XP 2600+
    qìó{@òÎ@òÎ@ Ï@°}@Ðó@T@W@M@V@Ò@ôok@Ò@¹
    à*@LôàP@pW@Q@@ô|¨Q@M@Ëô{@òÎT@ Ï@°}@Ðó@T@W@M@,Q0õoàôàP@0W@àôpõO@Ò@à*@p@T@Äõ$õ¤@/@I
    @÷ÄõÑ @
    v@v@$õ@
    v@ÐõM@¡O@õ¾¢@,Q@0W@Ψ@pw@?|@hõwp : yes
    : 1
    es
    on(tm) XP 2600+
    qìó{@òÎ@òÎ@ Ï@°}@Ðó@T@W@M@V@Ò@ôok@Ò@¹
    à*@LôàP@pW@Q@@ô|¨Q@M@Ëô{@òÎT@ Ï@°}@Ðó@T@W@M@,Q0õoàôàP@0W@àôpõO@Ò@à*@p@T@Äõ$õ¤@/@I
    @÷ÄõÑ @
    v@v@$õ@
    v@ÐõM@¡O@õ¾¢@,Q@0W@Ψ@pw@?|@hõflags : fpu vme de pse tsc msr p ae mce cx8 apic sep mtrr pge mca cmov pat pse36 mmx fxsr sse syscall mmxext 3dnowext 3dnow
    W@M@V@Ò@ôok@Ò@¹
    à*@LôàP@pW@Q@@ô|¨Q@M@Ëô{@òÎT@ Ï@°}@Ðó@T@W@M@,Q0õoàôàP@0W@àôpõO@Ò@à*@p@T@Äõ$õ¤@/@I
    @÷ÄõÑ @
    v@v@$õ@
    v@ÐõM@¡O@õ¾¢@,Q@0W@Ψ@pw@?|@hõbogomips : 4259.84
    pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 mmx fxsr sse syscall mmxext 3 dnowext 3dnow
    W@M@V@Ò@ôok@Ò@¹
    à*@LôàP@pW@Q@@ô|¨Q@M@Ëô{@òÎT@ Ï@°}@Ðó@T@W@M@,Q0õoàôàP@0W@àôpõO@Ò@à*@p@T@Äõ$õ¤@/@I
    @÷ÄõÑ @
    v@v@$õ@
    v@ÐõM@¡O@õ¾¢@,Q@0W@Ψ@pw@?|@hõ=
    gomips : 4259.84
    pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 mmx fxsr sse syscall mmxext 3 dnowext 3dnow
    W@M@V@Ò@ôok@Ò@¹
    à*@LôàP@pW@Q@@ô|¨Q@M@Ëô{@òÎT@ Ï@°}@Ðó@T@W@M@,Q0õoàôàP@0W@àôpõO@Ò@à*@p@T@Äõ$õ¤@/@I
    @÷ÄõÑ @
    v@v@$õ@
    v@ÐõM@¡O@õ¾¢@,Q@0W@Ψ@pw@?|@hõ
    This is the amended source code :-

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    
    int main(int argc, char *argv[]) {
      FILE *fp;
      char buffer[512];
      int bytes_sent;
      int simplesocket = 0;
      int simpleport = 0;
      int returnstatus = 0;
      struct sockaddr_in simpleserver;
    
      
      /*if argc does not have two arguemtns then print error message to screen */
      if( 2 != argc) {
        fprintf(stderr, "Usage: %s <port>\n",argv[0]);
        exit(1);
      }
       
    
      simplesocket = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);
      if(simplesocket == -1){
        fprintf(stderr, "could not create socket\n");
      exit(1);
      }
      else {
        printf("socket created\n"); 
      }
    
    
      simpleport = atoi(argv[1]);
    
      bzero(&simpleserver,sizeof(simpleserver));
      simpleserver.sin_family = AF_INET;
      simpleserver.sin_addr.s_addr = htonl(INADDR_ANY);
      simpleserver.sin_port = htons(simpleport);
    
      returnstatus = bind(simplesocket,(struct sockaddr *)&simpleserver,sizeof(simpleserver));
    
      if(returnstatus == 0 ){
      fprintf(stderr,"bind completed\n");
      }
      else {
        fprintf(stderr, "could not bind to address\n");
        close(simplesocket);
        exit(1);
      }
    
      returnstatus = listen(simplesocket, 5);
      if(returnstatus == -1) {
        fprintf(stderr, "cannot listen on socket\n");
        close(simplesocket);
        exit(1);
      }
          {
     
          struct sockaddr_in clientname = { 0 };
          int simplechildsocket = 0 ;
          int clientnamelength = sizeof(clientname);
    
          simplechildsocket = accept(simplesocket, (struct sockaddr *)&clientname,&clientnamelength);
    
          if(simplechildsocket == -1) {
    	fprintf(stderr, "cannot accept connections\n");
    	close(simplesocket);
    	exit(1);
          }
    
       fp = fopen("/proc/cpuinfo","r");
       while ((fgets (buffer,sizeof(buffer),fp)) != NULL)
       {
       bytes_sent=send (simplechildsocket,(char *)buffer,sizeof(buffer),0);
         if (bytes_sent == -1)
          {
           perror("send");
          } else {
       printf("\nBytes Sent: %d\n",bytes_sent);
     }
    }
    
          
      }
      fclose(fp);
      close(simplesocket);
      return 0;
    }
    Regards,
    Kevin joy
    www.unlocked-networks.com

  6. #6
    Registered User
    Join Date
    Aug 2003
    Location
    Birmingham, UK
    Posts
    12
    Quote Originally Posted by kevinj
    Greets nkhambal, thanks for your reply. I see the logic behind sending a chunk of data rather than 1byte at a time. I have amended the source using send() as you suggested but receive odd output from a client that connects -



    This is the amended source code :-

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    
    int main(int argc, char *argv[]) {
      FILE *fp;
      char buffer[512];
      int bytes_sent;
      int simplesocket = 0;
      int simpleport = 0;
      int returnstatus = 0;
      struct sockaddr_in simpleserver;
    
      
      /*if argc does not have two arguemtns then print error message to screen */
      if( 2 != argc) {
        fprintf(stderr, "Usage: %s <port>\n",argv[0]);
        exit(1);
      }
       
    
      simplesocket = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);
      if(simplesocket == -1){
        fprintf(stderr, "could not create socket\n");
      exit(1);
      }
      else {
        printf("socket created\n"); 
      }
    
    
      simpleport = atoi(argv[1]);
    
      bzero(&simpleserver,sizeof(simpleserver));
      simpleserver.sin_family = AF_INET;
      simpleserver.sin_addr.s_addr = htonl(INADDR_ANY);
      simpleserver.sin_port = htons(simpleport);
    
      returnstatus = bind(simplesocket,(struct sockaddr *)&simpleserver,sizeof(simpleserver));
    
      if(returnstatus == 0 ){
      fprintf(stderr,"bind completed\n");
      }
      else {
        fprintf(stderr, "could not bind to address\n");
        close(simplesocket);
        exit(1);
      }
    
      returnstatus = listen(simplesocket, 5);
      if(returnstatus == -1) {
        fprintf(stderr, "cannot listen on socket\n");
        close(simplesocket);
        exit(1);
      }
          {
     
          struct sockaddr_in clientname = { 0 };
          int simplechildsocket = 0 ;
          int clientnamelength = sizeof(clientname);
    
          simplechildsocket = accept(simplesocket, (struct sockaddr *)&clientname,&clientnamelength);
    
          if(simplechildsocket == -1) {
    	fprintf(stderr, "cannot accept connections\n");
    	close(simplesocket);
    	exit(1);
          }
    
       fp = fopen("/proc/cpuinfo","r");
       while ((fgets (buffer,sizeof(buffer),fp)) != NULL)
       {
       bytes_sent=send (simplechildsocket,(char *)buffer,sizeof(buffer),0);
         if (bytes_sent == -1)
          {
           perror("send");
          } else {
       printf("\nBytes Sent: %d\n",bytes_sent);
     }
    }
    
          
      }
      fclose(fp);
      close(simplesocket);
      return 0;
    }
    Regards,
    Kevin joy
    Interesting, I changed the size of the buffer to 30 bytes, seem's to have fixed the problem. Seem's as though it was sending more characters needed. Problem fixed. Thanks guys.
    www.unlocked-networks.com

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
       while ((fgets (buffer,sizeof(buffer),fp)) != NULL)
       {
       bytes_sent=send (simplechildsocket,(char *)buffer,sizeof(buffer),0);
    fgets retrieves one line of input, or as much as will fit in the buffer. Therefore, the buffer may not be fill. Using sizeof(buffer), you send the entire buffer. By shortening the buffer, you reduce the effects of the bug because not as much garbage is sent. You should use strlen instead.
    Code:
       bytes_sent=send (simplechildsocket,(char *)buffer,strlen(buffer),0);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 07-22-2002, 02:36 PM
  2. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM
  3. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM
  5. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM