The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Client/Server Communication
Discuss Client/Server Communication in the C Programming forum on Dev Shed. Client/Server Communication C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 20th, 2012, 06:35 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 4
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;
}
|

October 20th, 2012, 06:43 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 71
Time spent in forums: 1 Day 7 h 39 m 39 sec
Reputation Power: 1
|
|
|
So do you have a question?
|

October 20th, 2012, 06:46 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 4
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
|

October 22nd, 2012, 07:16 PM
|
|
|
|
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.
|

October 22nd, 2012, 08:18 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|