C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 20th, 2012, 06:35 PM
David25 David25 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 4 David25 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 16 m 45 sec
Reputation 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;
}

Reply With Quote
  #2  
Old October 20th, 2012, 06:43 PM
G4143 G4143 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 71 G4143 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 39 m 39 sec
Reputation Power: 1
So do you have a question?

Reply With Quote
  #3  
Old October 20th, 2012, 06:46 PM
David25 David25 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 4 David25 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 16 m 45 sec
Reputation Power: 0
Quote:
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

Reply With Quote
  #4  
Old October 22nd, 2012, 07:16 PM
BobS0327 BobS0327 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 120 BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 19 h 7 m 57 sec
Reputation Power: 44
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.

Reply With Quote
  #5  
Old October 22nd, 2012, 08:18 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,252 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 19 h 26 m 40 sec
Reputation Power: 1985
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Client/Server Communication

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap