#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Client/Server Communication


    Hello all, me and a buddy are stuck and were wondering if anybody could lead us in the right direction. We are currently working with a program where we established connection between a client and a server. What we need is for the client to request from the server, the server sends and the client is able to read and alter the file. i know we need to use send and receive as well as having a constant open connection. Can anybody help us in the right direction. Your help is greatly appreciated.

    Code:
    client
    #include <stdio.h>
    #include <stdlib.h>            //Added
    #include "cnaiapi.h"            //Added
     
     
    #define MAX_CNAME               10                    //Added
     
     
    int recvln(connection, char *, int);        //Added
    int readln(char *, int);            //Added
     
    int                                          //Added
    main(int argc, char *argv[])             //Added
    {
        FILE *fp;
        char ch;
        computer    comp;               //Added
        connection    conn;               //Added
        char            cname[MAX_CNAME];   //Added
        int        len;            //Added
     
        if (argc != 3) {                                                         //Added
            (void) fprintf(stderr, "usage: %s <compname> <appnum>\n",        //Added
                       argv[0]);                     //Added
            exit(1);                             //Added
        }                                      //Added
     
        /* convert the compname to binary form comp */                           //Added
     
        comp = cname_to_comp(argv[1]);                         //Added
        if (comp == -1)                                 //Added
            exit(1);                             //Added
     
        /* Input File Name to use */                        //Added     
        printf ("Enter The name of the file to retrieve. \n");                //Added
        len = readln( cname, MAX_CNAME);                    //Added
     
        /* make a connection to the chatserver */                   //Added
     
        conn = make_contact(comp, (appnum) atoi(argv[2]));            //Added
        if (conn < 0)                                 //Added
            exit(1);                                //Added
     
        (void) printf("Chat Connection Established.\n");                        //Added
     
     
     
     
        // OPEN THE FILE => LOAD THE CONTENT OF THE FILE IN MEMORY
        fp = fopen("log.txt", "r");        //Here we have to open the file that was enter to retrieve above and len instead of log.txt does not work 
        if (fp == NULL){
            printf("We were not able to open the file\n");
            exit(0);
        }
     
        // PRINT THE FILE INTO THE SCREEN
        /*while (1){
            ch = fgetc(fp);
            if (ch == EOF){
                // HERE WE FOUND THE END OF THE FILE
                break;
            }
            printf("%c",ch);
        }*/
     
        fclose(fp);
        return 1;
    }
    Code:
    #include <stdio.h>
    #include <stdlib.h>                          //Added
    #include "cnaiapi.h"                 //Added
     
    int recvln(connection, char *, int);       //Added
    int readln(char *, int);                   //Added
     
    int                                        //Added
    main(int argc, char *argv[])           //Added
    {
        int        len;                 //Added
        computer comp;                               //Added
        connection  conn;                            //Added    
        FILE *fp;
        char ch;
     
        if (argc != 2) {                                                       //Added
            (void) fprintf(stderr, "usage: %s <appnum>\n", argv[0]);       // Added
            exit(1);                              // Added
        }                                     // Added
     
     
        (void) printf("Chat Server Waiting For Connection.\n");     //Added
     
        /* wait for a connection from a chatclient */                //Added
     
        conn = await_contact((appnum) atoi(argv[1]));           //Added
        if (conn < 0)                           //Added
            exit(1);                         //Added
         
        (void) printf("Chat Connection Established.\n");         //Added
     
     
     
        // OPEN THE FILE => LOAD THE CONTENT OF THE FILE IN MEMORY
        fp = fopen("log.txt", "r");
        if (fp == NULL){
            printf("We were not able to open the file\n");
            exit(0);
        }
     
            (void) send(conn, fp,len, 0);  //  I NEED HELP HERE !?!?!?!?!?!?!?
     
        // PRINT THE FILE INTO THE SCREEN
        /*while (1){
            ch = fgetc(fp);
            if (ch == EOF){
                // HERE WE FOUND THE END OF THE FILE
                break;
            }
            printf("%c",ch);
        }
     
        fclose(fp);   */
        return 1;
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    So do you have a question?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by G4143
    So do you have a question?
    Yes, its stated above. We are having trouble keeping the connection open as well as using send and receive
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    It appears as though you're using the Douglas Comers' CNAIAPI library. The only documentation for this library is found in his Computer-networks-Internets book. I'd post a link to the book but I'm a new user and the forum won't allow new users to post links.

    Are you required to use this CNAIAPI library for your application? IMHO, it's a very obscure library. I don't believe too many people are familiar enough with it to be of any assistance to you.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    Since you are talking about creating a connection and trying to keep it open, I assume that you are wanting to use tcp instead of udp datagrams. It is trivial to keep a tcp connection open: simply connect and it will remain connected until you perform a graceful shutdown.

    My sockets programming pages start at http://pgm.dwise1.net/sockets/index.html; the first page includes a site map. No special libraries needed, just simple sockets, though I also show how to convert UNIX sockets code to Winsock.

IMN logo majestic logo threadwatch logo seochat tools logo