Client-Server Data Read Problem

This is a discussion on Client-Server Data Read Problem within the C Programming forums, part of the General Programming Boards category; Hi All, I'm trying to reading in data from a file and send that data to a server, but I'm ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    47

    Client-Server Data Read Problem

    Hi All,

    I'm trying to reading in data from a file and send that data to a server, but I'm getting a data read error. Here's the following error:

    $ gcc *Client* -o *Client*
    udpClient.c: In function `main':
    udpClient.c:76: warning: comparison between pointer and integer
    Undefined first referenced
    symbol in file
    socket /var/tmp/cc05IzIT1.o
    gethostbyname /var/tmp/cc05IzIT1.o
    bind /var/tmp/cc05IzIT1.o
    sendto /var/tmp/cc05IzIT1.o
    inet_ntoa /var/tmp/cc05IzIT1.o
    ld: fatal: Symbol referencing errors. No output written to udpClient.c
    $

    No output written to the client file.

    Here's the code for the client file. It's a little lengthy. Any help would be appreciated:

    Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>     /* memset */
    #include <sys/time.h>   /* select() */
    
    #define REMOTE_SERVER_PORT 4500
    #define MAX_MSG 100
    
    // ----------------------------------
    // --------- MAIN -------------------
    // ----------------------------------
    
    int main (int argc, char *argv[])
    {
       int sd, rc, i;
       struct sockaddr_in cliAddr, remoteServAddr;
       struct hostent *h;
       
        // Get Data
        FILE *data;
        char c;
    	
        data = fopen("data.txt", "t");
    
       // check command line arguments
       if (argc < 3)
       {
          printf("usage : %s <server> <data1> ... <dataN> \n", argv[0]);
          exit(1);
       }
    
       // get server IP address (no check if input is IP address or DNS name)
       h = gethostbyname(argv[1]);
       if (h == NULL)
       {
          printf("%s: unkown host '%s' \n", argv[0], argv[1]);
          exit(1);
       }
    
       printf("%s: sending data to '%s' (IP: %s) \n", argv[0], h->h_name, inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));
    
      remoteServAddr.sin_family = h->h_addrtype;
      memcpy((char *) &remoteServAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
      remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT);
    
      // socket creation
      sd = socket(AF_INET, SOCK_DGRAM, 0);
      if (sd > 0)
      {
          printf("%s: cannot open socket \n", argv[0]);
          exit(1);
      }
    
      // bind any port
      cliAddr.sin_family = AF_INET;
      cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
      cliAddr.sin_port = htons(4502);
    
      rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
      if (rc < 0)
      {
          printf("%s: cannot bind port \n", argv[0]);
          exit(1);
      }
      
      // send data
      for (i = 2; 1 < argv[i]; i++)
      {
         if (data == NULL) printf("File doesn't exist\n");
       	 else {
          do {
             c = getc(data);    /* get one character from the file */
             putchar(c);         /* display it on the monitor       */
    	     rc = sendto(sd, argv[i], strlen(argv[i]) + 1, 0, (struct sockaddr *) &remoteServAddr, sizeof(remoteServAddr));		 
          } while (c != EOF);    /* repeat until EOF (end of file)  */
       }	  
       
          if (rc < 0)
          {
             printf("%s: cannot send data \n", argv[0]);
             close(sd);
             exit(1);
          }
    
      }
    
      return 1;
    
    }
    Thanks

  2. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    mind telling me which one is line 76?
    hello, internet!

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    Here's line 76:

    for (i = 2; 1 < argv[i]; i++)

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    Thank you for the advice. I entered what you suggested and got the following:

    $ gcc *Client* -o Client
    udpClient.c: In function `main':
    udpClient.c:87: warning: comparison between pointer and integer
    udpClient.c: At top level:
    udpClient.c:101: parse error before `return'
    $

    Still can't get this file to compile

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    OK, I fixed the argument, it was a typeo.

    Still getting one error in which I can't figure out.


    $ gcc *Client* -o Client
    udpClient.c:101: parse error before `return'

    Any ideas?

    Thanks for helping.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    Wow -- another typeo problem. Well, all typeos fix. Now when I compile I get this.

    $ gcc *Client* -o Client
    Undefined first referenced
    symbol in file
    socket /var/tmp/ccZPnO__1.o
    gethostbyname /var/tmp/ccZPnO__1.o
    bind /var/tmp/ccZPnO__1.o
    sendto /var/tmp/ccZPnO__1.o
    inet_ntoa /var/tmp/ccZPnO__1.o
    ld: fatal: Symbol referencing errors. No output written to Client
    $

    Here's the revised code:

    Code:
    /* fpont 12/99  */
    /* pont.net     */
    /* udpClient.c  */
    
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>     /* memset */
    #include <sys/time.h>   /* select() */
    
    #define REMOTE_SERVER_PORT 4500
    #define MAX_MSG 100
    
    // ----------------------------------
    // --------- MAIN -------------------
    // ----------------------------------
    
    int main (int argc, char *argv[])
    {
       int sd, rc, i;
       struct sockaddr_in cliAddr, remoteServAddr;
       struct hostent *h;
       
        // Get Data
    	FILE *fp1;
    	char oneword[100], filename[25];
    	char *c;
    
       	printf("Enter filename -> ");
    	scanf("%s", filename);            /* read the desired filename  */
       	fp1 = fopen(filename, "r");
    
       do {
          c = fgets(oneword, 100, fp1);  /* get one line from the file */
          if (c != NULL)
             printf("%s", oneword);      /* display it on the monitor  */
       } while (c != NULL);              /* repeat until NULL          */
    
       fclose(fp1);
    
       // check command line arguments
       if (argc < 3)
       {
          printf("usage : %s <server> <data1> ... <dataN> \n", argv[0]);
          exit(1);
       }
    
       // get server IP address (no check if input is IP address or DNS name)
       h = gethostbyname(argv[1]);
       if (h == NULL)
       {
          printf("%s: unkown host '%s' \n", argv[0], argv[1]);
          exit(1);
       }
    
       printf("%s: sending data to '%s' (IP: %s) \n", argv[0], h->h_name, inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));
    
      remoteServAddr.sin_family = h->h_addrtype;
      memcpy((char *) &remoteServAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
      remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT);
    
      // socket creation
      sd = socket(AF_INET, SOCK_DGRAM, 0);
      if (sd > 0)
      {
          printf("%s: cannot open socket \n", argv[0]);
          exit(1);
      }
    
      // bind any port
      cliAddr.sin_family = AF_INET;
      cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
      cliAddr.sin_port = htons(4502);
    
      rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
      if (rc < 0)
      {
          printf("%s: cannot bind port \n", argv[0]);
          exit(1);
      }
      
      // send data
      for (i = 2; 1 < argc; i++)
      {
         rc = sendto(sd, argv[i], strlen(argv[i]) + 1, 0, (struct sockaddr *) &remoteServAddr, sizeof(remoteServAddr));	  
    
         if (rc < 0)
          {
             printf("%s: cannot send data \n", argv[0]);
             close(sd);
             exit(1);
          }
    
      }
    
      return 1;
    
    }
    Would truly appreciate any help here.

  7. #7
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by bob2509
    Here's line 76:

    for (i = 2; 1 < argv[i]; i++)
    what exactly do you want to do with this line? whatever it is, i guarantee you're not accomplishing it
    hello, internet!

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    OK, got it working. Only it will not open the socket. Any ideas why?

    Here's the output:

    $ udpClient db1-atb data.txt
    udpClient: sending data to 'www' (IP: ***.***.***.**)
    udpClient: cannot open socket
    $

    The server is running. I don't have a good grip of sockets yet so I'm not sure what's going on here.

    Thanks

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    Nevermind all. Got it working. Thanks for all your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. server client problem
    By rudeboy in forum Networking/Device Communication
    Replies: 5
    Last Post: 05-17-2008, 12:41 AM
  2. server client application - (i really need your help)
    By sarahnetworking in forum C Programming
    Replies: 3
    Last Post: 03-01-2008, 09:54 PM
  3. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-07-2007, 11:24 PM
  4. TCP/IP client & echo server
    By Jordban in forum C++ Programming
    Replies: 2
    Last Post: 06-06-2005, 06:39 PM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21