Link to home
Start Free TrialLog in
Avatar of augwest
augwestFlag for United States of America

asked on

Client-Server application to have a server display the contents of a file

I need help with this program yes its an assignment,

However I am getting a lot of erros I think I have the code right though the teacher said i have somethings to fix I have gone crazy trying to figure this out but I figured I would ask for some guidence

Below is my error codes with my server and client codes attached

client.c:1: error: syntax error before '<' token
client.c: In function `main':
client.c:32: error: `servaddr' undeclared (first use in this function)
client.c:32: error: (Each undeclared identifier is reported only once
client.c:32: error: for each function it appears in.)
client.c:45: error: `fp' undeclared (first use in this function)
client.c:49: error: `ERROR' undeclared (first use in this function)
client.c:53: error: `numLines' undeclared (first use in this function)
client.c:54: error: `line' undeclared (first use in this function)
client.c:54: error: `MAX_LINE' undeclared (first use in this function)
nova> gcc server.c
server.c: In function `main':
server.c:27: error: syntax error before '{' token
server.c: At top level:
server.c:33: error: syntax error before '.' token
server.c:43: error: syntax error before numeric constant
server.c:43: warning: data definition has no type or storage class
server.c:48: error: `cliAddr' undeclared here (not in a function)
server.c:48: warning: data definition has no type or storage class
server.c:49: error: `sd' undeclared here (not in a function)
server.c:49: error: initializer element is not constant
server.c:49: warning: data definition has no type or storage class
server.c:50: error: syntax error before "if"
server.c:53: warning: parameter names (without types) in function declaration
server.c:53: warning: data definition has no type or storage class
server.c:54: error: syntax error before "return"
server.c:63: warning: parameter names (without types) in function declaration
server.c:63: warning: data definition has no type or storage class
server.c:64: error: syntax error before numeric constant
server.c:64: warning: conflicting types for built-in function 'exit'
server.c:64: warning: data definition has no type or storage class
server.c:72: error: `count' undeclared here (not in a function)
server.c:72: warning: data definition has no type or storage class
server.c:73: warning: data definition has no type or storage class
server.c:75: error: syntax error before '}' token
server.c:72: error: storage size of `line' isn't known
server.c:73: error: storage size of `line' isn't known

Server
 
#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> /* close */
 
#define SUCCESS 0
#define ERROR 1
 
#define END_LINE 0x0A
#define SERVER_PORT 3080
#define MAX_MSG 100
 
int main (int argc, char *argv[]) 
{
  int sd, newSd, cliLen;
  ssize_t nread, count;
 
  struct sockaddr_in cliAddr, servAddr;
  char line[MAX_MSG]; 
 
     /* create socket */ 
  if ( (sd = socket(AF_INET, SOCK_STREAM, 0)) < 0
 
  {
    perror("cannot open socket ");
    return ERROR;
  }
 
  /* bind server port */
  servAddr.sin_family = AF_INET;
  servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servAddr.sin_port = htons(SERVER_PORT); 
  if(bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr))<0) 
  {
    perror("cannot bind port ");
    return ERROR;
  }
 
  /* begin listening on the socket */ 
  listen(sd,5); 
 
  while(1) 
  {
    printf("%s: waiting for data on port TCP %u\n",argv[0],SERVER_PORT); 
    cliLen = sizeof(cliAddr);
    newSd = accept(sd, (struct sockaddr *) &cliAddr, &cliLen);
    if (newSd < 0)
	{
	perror("cannot accept connection ");
	close(sd);
	return ERROR;
	}
	//new code starts here
    else
	{
	//a new child process is created
         if ((pid = fork()) < 0)
	{
		printf("Fork error\n");
		close(newSd);
		exit(1);
	}
	else if (pid == 0) /* child */
	/* initialize line buffer to 0 */
	memset(line, 0x0, MAX_MSG);
        break;
  }
 
  line[count++] = ',';
  line[count++] = ' ';
 
  } /* while(read) */
  } /* while (1) */
 
}
 
 
Client
 
#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> /* close */
 
#define SERVER_PORT 3080
#define MAX_MSG 100
 
int main (int argc, char *argv[]) {
  int sd, rc, i;
  struct sockaddr_in servAddr;
 
  if(argc < 3) 
  {
    printf("usage: %s <IPAddr> <data1> <data2> ... <dataN>\n",argv[0]);
    exit(1);
  }
 
  /* create socket */
  if ( (sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) 
  {
    perror("cannot open socket ");
    exit(1);
  }
 
  /* initialization of sockaddress_in structure */
  servAddr.sin_family = AF_INET;
  servAddr.sin_port = htons(SERVER_PORT);
  if (inet_pton (AF_INET, argv[1], &servaddr.sin_addr) <= 0)
  {
    perror("inet_pton error");
    exit(1);
  }
 
  /* connect to server */ 
  if (connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
  {
    perror("cannot connect ");
    exit(1);
  }
 
  if ((fp = fopen(argv[2], "r")) == NULL)
	{
	printf("ERROR:  Can't open file for reading\n");
	close(sd);
	return ERROR;
	}
  else
	{
	numLines = 0;
	while (fgets(line, MAX_LINE, fp) != NULL)
	{
		if (write(sd, line, strlen(line)) < 0)
		{
			perror("cannot send data ");
			close(sd);
			return ERROR;
		}
		numLines++;
	}
	printf("File %s has been sent (%d lines)\n", argv[2], numLines);
	fclose(fp);
	close(sd);
 
}
 
 
 
return 0;
 
}

Open in new window

Avatar of Korbus
Korbus

client.c:32: error: `servaddr' undeclared (first use in this function)
    you spelled it wrong- variables are case-sesitive

client.c:45: error: `fp' undeclared (first use in this function)
client.c:53: error: `numLines' undeclared (first use in this function)
client.c:54: error: `line' undeclared (first use in this function)
you need to declare the variable before you can use it.  This is so that the complier knows what TYPE the variable is.

client.c:49: error: `ERROR' undeclared (first use in this function)
you defined this in server, not client

client.c:54: error: `MAX_LINE' undeclared (first use in this function)
you have not defined this macro/variable
also, Couple of extra "}" in the server code. Attached modified ones -
Client
 
#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> /* close */
 
#define SERVER_PORT 3080
#define MAX_MSG 100
#define MAX_LINE 900
 
#define ERROR 1
 
int main (int argc, char *argv[]) {
  int sd, rc, i, numLines;
  struct sockaddr_in servAddr;
 
  FILE *fp = NULL;
  char line[MAX_MSG];
 
  if(argc < 3)
  {
    printf("usage: %s <IPAddr> <data1> <data2> ... <dataN>\n",argv[0]);
    exit(1);
  }
 
  /* create socket */
  if ( (sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  {
    perror("cannot open socket ");
    exit(1);
  }
 
  /* initialization of sockaddress_in structure */
  servAddr.sin_family = AF_INET;
  servAddr.sin_port = htons(SERVER_PORT);
  if (inet_pton (AF_INET, argv[1], &servAddr.sin_addr) <= 0)
  {
    perror("inet_pton error");
    exit(1);
  }
 
  /* connect to server */
  if (connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
  {
    perror("cannot connect ");
    exit(1);
  }
 
  if ((fp = fopen(argv[2], "r")) == NULL)
        {
        printf("ERROR:  Can't open file for reading\n");
        close(sd);
        return ERROR;
        }
  else
        {
        numLines = 0;
        while (fgets(line, MAX_LINE, fp) != NULL)
        {
                if (write(sd, line, strlen(line)) < 0)
                {
                        perror("cannot send data ");
                        close(sd);
                        return ERROR;
                }
                numLines++;
        }
        printf("File %s has been sent (%d lines)\n", argv[2], numLines);
        fclose(fp);
        close(sd);
 
}
 
 
 
return 0;
 
}
 
 
Server
 
#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> /* close */
 
#define SUCCESS 0
#define ERROR 1
 
#define END_LINE 0x0A
#define SERVER_PORT 3080
#define MAX_MSG 100
 
int main (int argc, char *argv[])
{
  int sd, newSd, cliLen;
  ssize_t nread, count;
  pid_t pid;
 
  struct sockaddr_in cliAddr, servAddr;
  char line[MAX_MSG];
 
     /* create socket */
  if ( (sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
 
  {
    perror("cannot open socket ");
    return ERROR;
  }
 
  /* bind server port */
  servAddr.sin_family = AF_INET;
  servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servAddr.sin_port = htons(SERVER_PORT);
  if(bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr))<0)
  {
    perror("cannot bind port ");
    return ERROR;
  }
 
  /* begin listening on the socket */
  listen(sd,5);
 
  while(1)
  {
    printf("%s: waiting for data on port TCP %u\n",argv[0],SERVER_PORT);
    cliLen = sizeof(cliAddr);
    newSd = accept(sd, (struct sockaddr *) &cliAddr, &cliLen);
    if (newSd < 0)
        {
        perror("cannot accept connection ");
        close(sd);
        return ERROR;
      }
        //new code starts here
    else
        {
        //a new child process is created
         if ((pid = fork()) < 0)
        {
                printf("Fork error\n");
                close(newSd);
                exit(1);
        }
        else if (pid == 0) /* child */
        /* initialize line buffer to 0 */
        memset(line, 0x0, MAX_MSG);
        break;
        }
 
  line[count++] = ',';
  line[count++] = ' ';
 
  } /* while (1) */
 
}

Open in new window

Avatar of augwest

ASKER

sundar62 thankyou that code seems to have cleared out the errors i was getting

One thing that  Iwas trying to figure out is do you know how I would call these program so they grab the text file name in command line
would it be something like

gcc server.c client.c filename

would that be right?
Line number 53 in client.c opens a file for reading. So this is the code that expects a filname as an argument. Once you have compiled client.c with an executable say "client". Then you can all that program say

client <give an ip address> filename
Avatar of augwest

ASKER

What this is suppose to do is open the file pass the content of the file to the server and the server should display it.

However it just seems to be hanging
First you have to start the "server". Assuming your server.c executable is called "server". type the following in the command line -

server

Next start the client - client <ip address where server is running> filename

It should work.
Avatar of augwest

ASKER

i think maybe my arguements are wrong in the being of client and server to bring in the file name and stuff

any ideas on this?
Please look at the attached link - You will have to add printf statements in the server code to see what its getting from client.

http://www.sfu.ca/~vwchu/cclientserver.html
Avatar of augwest

ASKER

ive seen that but im still not sure how to make the server print the file content it definitely seems to be sending it but doesnt seem to print out on the server side
Ok. The following server.c does the job fo you (but please go thru the previous link I sent again. everything is in there).

#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> /* close */

#define SUCCESS 0
#define ERROR 1

#define END_LINE 0x0A
#define SERVER_PORT 3080
#define MAX_MSG 100

int main (int argc, char *argv[])
{
  int n, sd, newSd, cliLen;
  ssize_t nread, count;
  pid_t pid;

  long lRecvd,lEchoed;
  int iRecvdp, iEchoedp;

  struct sockaddr_in cliAddr, servAddr;
  char line[MAX_MSG];
  char buf[1000];

  iRecvdp = 0;
  iEchoedp = 0;
     /* create socket */
  if ( (sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)

  {
    perror("cannot open socket ");
    return ERROR;
  }

  /* bind server port */
  servAddr.sin_family = AF_INET;
  servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servAddr.sin_port = htons(SERVER_PORT);
  if(bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr))<0)
  {
    perror("cannot bind port ");
    return ERROR;
  }

  /* begin listening on the socket */
  listen(sd,5);

  while(1)
  {
    printf("%s: waiting for data on port TCP %u\n",argv[0],SERVER_PORT);
    cliLen = sizeof(cliAddr);
    newSd = accept(sd, (struct sockaddr *) &cliAddr, &cliLen);
    n = recv(newSd, buf, sizeof(buf), 0);
    lRecvd = (long)n;
    lEchoed = 0l;

    if (n < 0)
        {
        perror("cannot accept connection ");
        close(sd);
        return ERROR;
        }
        //new code starts here
    else
        {

        iRecvdp++;
        iEchoedp++;

        write(1,"Client says:\n", 13);
        write(1,buf,n);

        lEchoed += (long) send(newSd, buf, n, 0);
        n = recv(newSd, buf, sizeof(buf), 0);

        lRecvd += (long)n;
        //a new child process is created
         if ((pid = fork()) < 0)
        {
                printf("Fork error\n");
                close(newSd);
                exit(1);
        }
        else if (pid == 0) /* child */
        /* initialize line buffer to 0 */
        memset(line, 0x0, MAX_MSG);
        break;
        }

  line[count++] = ',';
  line[count++] = ' ';

  } /* while (1) */

}
Avatar of augwest

ASKER

while it is better it only seems to print the first line of txt file not the whole thing
 
ASKER CERTIFIED SOLUTION
Avatar of sundar62
sundar62

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of augwest

ASKER

This is my file
I am sending this file to the server
There server is reading what the client
Has sent This was a neat project.
 
this is the txt file i am sending 4 lines
Avatar of augwest

ASKER

now im not even getting 1 line im getting ???????????????
I am unable to see the file. Could you post the contents? anyway, The code on my side works fine.
Avatar of augwest

ASKER

actually im still just getting 1 line
Avatar of augwest

ASKER

Sundar62 the file name is text.txt
the connects are these four lines below
This is my file
I am sending this file to the server
There server is reading what the client
Has sent This was a neat project.
Apparently I am seeing all the 4 lines here. Can you post your "server.c" code?
Avatar of augwest

ASKER

the server.c code is the code you post three or so posts up
Avatar of augwest

ASKER

what are is your client.c look like?
Avatar of augwest

ASKER

thanks for the help