Here are my updated versions of my client and server code.
fileserver.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include "cnaiapi.h"
int recvln(connection, char *, int);
int readln(char *, int);
int
main(int argc, char *argv[])
{
int len;
computer comp;
connection conn;
FILE *fp;
char ch;
char * buffer;
size_t result;
if (argc != 2) {
(void) fprintf(stderr, "usage: %s <appnum>\n", argv[0]);
exit(1);
}
(void) printf("Chat Server Waiting For Connection.\n");
/* wait for a connection from a chatclient */
conn = await_contact((appnum) atoi(argv[1]));
if (conn < 0)
exit(1);
(void) printf("Chat Connection Established.\n");
// 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);
}
// obtain file size:
fseek (fp , 0 , SEEK_END);
len = ftell (fp);
rewind (fp);
// allocate memory to contain the whole file:
buffer = (char*) malloc (sizeof(char)*len);
if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
// copy the file into the buffer:
result = fread (buffer,1,len,fp);
if (result != len) {fputs ("Reading error",stderr); exit (3);}
char buf[1024];
while (fgets(buf, sizeof(buffer), fp) != NULL)
if (send(conn, buffer, strlen(buf), 0) < 0)
// handle error
// send(conn, buffer,len, 0); // Needed if above while loop doesn't send the file to client?
//fprintf (fp, "%s", buffer); // Needed if above while loop doesn't send the file to client?
/*if (send(conn, data, len, 0) < 0) {
perror("send");
// exit or return from function as appropriate.
}*/
// 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;
}
fileclient.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include "cnaiapi.h"
#define MAX_CNAME 10
#define BUFFSIZE 256
int recvln(connection, char *, int);
int readln(char *, int);
int
main(int argc, char *argv[])
{
FILE *fp;
char ch;
computer comp;
connection conn;
char cname[MAX_CNAME];
int len, received, expect;
char buff[BUFFSIZE];
if (argc != 3) {
(void) fprintf(stderr, "usage: %s <compname> <appnum>\n",
argv[0]);
exit(1);
}
/* convert the compname to binary form comp */
comp = cname_to_comp(argv[1]);
if (comp == -1)
exit(1);
/* make a connection to the fileserver */
conn = make_contact(comp, (appnum) atoi(argv[2]));
if (conn < 0)
exit(1);
(void) printf("Chat Connection Established.\n");
/* Input File Name to use */
printf ("Enter The name of the file to retrieve. \n");
//len = readln( cname, MAX_CNAME); //This line does not do anything for now sice we are only using 1 file (log.txt)
/* read and print same no. of bytes from echo server */
expect = len;
for (received = 0; received < expect;) {
len = recv(conn, buff, (expect - received) < BUFFSIZE ?
(expect - received) : BUFFSIZE, 0);
if (len < 0) {
send_eof(conn);
return 1;
}
(void) write(STDOUT_FILENO, buff, len);
received += len;
}
(void) printf("\n");
(void) fflush(stdout);
/*// OPEN THE FILE => LOAD THE CONTENT OF THE FILE IN MEMORY
fp = fopen("log.txt", "w"); //Here we have to open the file that was entered 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);
}*/
/*if (send(conn, buff, len, 0) < 0) {
perror("send");
// exit or return from function as appropriate.
}*/
// PRINT THE FILE INTO THE SCREEN Not sure if I need this?????????????????
while (1){
ch = fgetc(fp);
if (ch == EOF){
// HERE WE FOUND THE END OF THE FILE
break;
}
printf("%c",ch);
}
fclose(fp);
return 0;
}
await_contact.c
Code:
/* await_contact.c */
#include "cnaiapi.h"
/*-----------------------------------------------------------------------
* await_contact - accept a connection on port a. If no master sock is
* already open for the port, create one and record it in the port-to-
* socket table.
*-----------------------------------------------------------------------
*/
connection
await_contact(appnum a)
{
struct sockaddr_in sockaddr, csockaddr;
int sock, newsock, i, csockaddrlen;
static int p2sinit = 0;
static struct port2sock p2s[P2S_SIZE];
cnaiapi_init();
if (a == 0)
return -1;
#if defined(LINUX) || defined(SOLARIS)
pthread_mutex_lock(&await_contact_mutex);
#elif defined(WIN32)
WaitForSingleObject(await_contact_mutex, INFINITE);
#endif
if (p2sinit == 0) {
(void) memset(p2s, 0, sizeof(p2s));
p2sinit = 1;
}
/* look up master socket in port-to-socket table */
for (sock = -1, i = 0; i < P2S_SIZE; i++) {
if (p2s[i].port == a) {
sock = p2s[i].sock;
break;
}
}
if (sock == -1) {
/* open new master socket */
/* look for room in p2s table */
for (i = 0; i < P2S_SIZE; i++) {
if (p2s[i].port == 0)
break;
}
if (i == P2S_SIZE) {
/* no room left in p2s */
#if defined(LINUX) || defined(SOLARIS)
pthread_mutex_unlock(&await_contact_mutex);
#elif defined(WIN32)
ReleaseMutex(await_contact_mutex);
#endif
return -1;
}
sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock < 0) {
#if defined(LINUX) || defined(SOLARIS)
pthread_mutex_unlock(&await_contact_mutex);
#elif defined(WIN32)
ReleaseMutex(await_contact_mutex);
#endif
return -1;
}
(void) memset(&sockaddr, 0, sizeof(struct sockaddr_in));
sockaddr.sin_family = AF_INET;
sockaddr.sin_port = htons(a);
sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock, (struct sockaddr *) &sockaddr,
sizeof(struct sockaddr_in)) < 0 ||
listen(sock, LISTEN_Q_LEN) < 0) {
#if defined(LINUX) || defined(SOLARIS)
close(sock);
pthread_mutex_unlock(&await_contact_mutex);
#elif defined(WIN32)
closesocket(sock);
ReleaseMutex(await_contact_mutex);
#endif
return -1;
}
/* record master socket in table */
p2s[i].sock = sock;
p2s[i].port = a;
}
#if defined(LINUX) || defined(SOLARIS)
pthread_mutex_unlock(&await_contact_mutex);
#elif defined(WIN32)
ReleaseMutex(await_contact_mutex);
#endif
csockaddrlen = sizeof(struct sockaddr_in);
newsock = accept(sock, (struct sockaddr *) &csockaddr, &csockaddrlen);
return newsock;
}
make_contact.c
Code:
/* make_contact.c */
#include "cnaiapi.h"
/*-----------------------------------------------------------------------
* make_contact - open a new TCP connection to the specified IP address
* (c) and port e(a).
*-----------------------------------------------------------------------
*/
connection
make_contact(computer c, appnum a)
{
struct sockaddr_in sockaddr;
int sock;
cnaiapi_init();
sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock < 0)
return -1;
(void) memset(&sockaddr, 0, sizeof(struct sockaddr_in));
sockaddr.sin_family = AF_INET;
sockaddr.sin_port = htons(a);
sockaddr.sin_addr.s_addr = c;
if (connect(sock, (struct sockaddr *) &sockaddr, sizeof(struct sockaddr_in)) < 0) {
#if defined(LINUX) || defined(SOLARIS)
close(sock);
#elif defined(WINDOWS)
closesocket(sock);
#endif
return -1;
}
return sock;
}
I might be confusing multiple pieces of code that do the same thing in my client and server code. After the sever establishes a connection with the client (client then asks for the file name) the server closes its connection but the client stays open. This is my biggest concern right now. Then another concern is what code do I need (might already have it) that accepts the contents from the server and prints it to the terminal?
Thanks again for all your help!!!