Solved

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

Posted on 2009-05-10
21
660 Views
Last Modified: 2013-12-26
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

0
Comment
Question by:augwest
  • 12
  • 8
21 Comments
 
LVL 10

Expert Comment

by:Korbus
ID: 24349999
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
0
 
LVL 1

Expert Comment

by:sundar62
ID: 24350058
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

0
 
LVL 10

Author Comment

by:augwest
ID: 24350194
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?
0
 
LVL 1

Expert Comment

by:sundar62
ID: 24350232
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
0
 
LVL 10

Author Comment

by:augwest
ID: 24350247
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
0
 
LVL 1

Expert Comment

by:sundar62
ID: 24350293
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.
0
 
LVL 10

Author Comment

by:augwest
ID: 24350330
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?
0
 
LVL 1

Expert Comment

by:sundar62
ID: 24350340
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
0
 
LVL 10

Author Comment

by:augwest
ID: 24350444
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
0
 
LVL 1

Expert Comment

by:sundar62
ID: 24350544
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) */

}
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 10

Author Comment

by:augwest
ID: 24350647
while it is better it only seems to print the first line of txt file not the whole thing
 
0
 
LVL 1

Accepted Solution

by:
sundar62 earned 500 total points
ID: 24350720
Could you try the attached code? Also could you post the text file you are sending?

#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;
        break;

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

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

  } /* while (1) */

}

0
 
LVL 10

Author Comment

by:augwest
ID: 24350767
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
0
 
LVL 10

Author Comment

by:augwest
ID: 24350893
now im not even getting 1 line im getting ???????????????
0
 
LVL 1

Expert Comment

by:sundar62
ID: 24350904
I am unable to see the file. Could you post the contents? anyway, The code on my side works fine.
0
 
LVL 10

Author Comment

by:augwest
ID: 24350923
actually im still just getting 1 line
0
 
LVL 10

Author Comment

by:augwest
ID: 24350927
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.
0
 
LVL 1

Expert Comment

by:sundar62
ID: 24351025
Apparently I am seeing all the 4 lines here. Can you post your "server.c" code?
0
 
LVL 10

Author Comment

by:augwest
ID: 24351034
the server.c code is the code you post three or so posts up
0
 
LVL 10

Author Comment

by:augwest
ID: 24351084
what are is your client.c look like?
0
 
LVL 10

Author Closing Comment

by:augwest
ID: 31579976
thanks for the help
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now