Intro Network programming HELP!!

Intro Network programming HELP!!

Post by Dav » Tue, 23 Oct 2001 06:07:47



Hello everyone,
  I am new to networking and network programming, I have several years
experience programmin in Pascal and Java but not C which is a pain
since most unix programming stuff is in C.  Ok so that's why Im here,
I am trying to do a project for a class and I am having considerable
trouble,

My assignment is the following:

Create a networking application using the socket interface to
implement a "character count" service over TCP.
The client code and server code must be written

APPLICATION LAYER:
  The client side of the application connects to the server.  the
client then reads characters from the terminal until it reads a
new-line character.  The client sends all characters entered to the
server(but the client does not send the new line characeter).

The server reads the characters, counts the characters, and returns
the count back to the client.  The process repeats until the client
send "--".

SESSION LAYER:

The NULL character servers as the session-layer ending of each string
of characters (the NULL character is not included in the total count
of characters).  When the client wants to end all communication, it
sends two minus signs, followed by a NULL characters.

PRESENTATION LAYER:
  The client sends ASCII characters to the server.  The server returns
a NULL-Terminates ASCII string containing count.  so, if the client
sends 102 characters, the server would return the NULL-Terminated
ASCII strin "102."

Here is an example output:

$rcount sparky   (sparky name of place to connect)
hello
5
how long is this string????
28
--abc
5
--

$

So I understand the basics, socket, connect, bind ,listen, accept, and
have them coded for the client and server, however my problem comes in
when I actually have to read or write something.  I'm just not
familiar enough with C to understand how to read lines and write them
back and forth from client to server.

I would like to use fgets()  while using read() and write() with char
arrays
something to the extent of this
//client code

do
{
  if (fgets(line, BUFSIZ, stdin) == NULL){
        perror("Error Reading Line Input");
        exit(1);
   }
   length =strlen(line);
   if (line[length-1]=='\n'){ /* take off the \n append an null */
   line[length-1]='\0';
   }
   if(write(fd, line, length) != length) {
      perror("write failed!\n");
      exit(1);
   }
   //for now just exit after first run
   done = 1;
  }while(done != 1);

   if (read(fd, buf, 1) == -1) {
       perror("read failed!\n");
       exit(1);
               }
   printf("%c", buf[0]);
...

but it isnt working correctly  I guess I just dont know how to read
and write corectly...
Can anyone explain this stuff to me?
It would be soooo helpful if you could
Please replay to this message with any tips or any example code that
you think could be helpful or please email me

Thank you for your time!
Dave

 
 
 

Intro Network programming HELP!!

Post by Paul Roterin » Tue, 23 Oct 2001 08:36:56



>    if(write(fd, line, length) != length) {
>       perror("write failed!\n");
>       exit(1);
>    }

This condition is not necessarily an error.  It is perfecly reasonable
for write() to write less than length bytes to the socket.  You must
call write repeatedly until either the entire message has been written
or write() returns an error condition (-1).

Quote:>    if (read(fd, buf, 1) == -1) {
>        perror("read failed!\n");
>        exit(1);
>                }

See comments for write(), above.  The same situation applies to read().

 
 
 

Intro Network programming HELP!!

Post by Dav » Wed, 24 Oct 2001 00:13:29




> >    if(write(fd, line, length) != length) {
> >       perror("write failed!\n");
> >       exit(1);
> >    }

> This condition is not necessarily an error.  It is perfecly reasonable
> for write() to write less than length bytes to the socket.  You must
> call write repeatedly until either the entire message has been written
> or write() returns an error condition (-1).

> >    if (read(fd, buf, 1) == -1) {
> >        perror("read failed!\n");
> >        exit(1);
> >                }

> See comments for write(), above.  The same situation applies to read().

What if it is writing back the wrong byte?  The server app, recieves
the information from read correctly but when it is time to write it
back it does not write back what I want it to, I get wierd things.
here is a snippit from the server code:

int
          perform_actions(conn_fd)
          int conn_fd;
          {
                //someof these variables are not used..please note

               int count = 0;
                int done = 0;    /* indicates all data is read        */
               int where = 0;   /* points to where we are in buffer
*/
               int nbytes;      /* the number of bytes read        
*/
               char leave[] = "e";

               char buf[20];/* buffer to hold the name           */
               char county[20];     /* used to figure out if the  */
               char readin[20]; /* specified user is logged   */
               char endall[] ="--";
                FILE *fp;             /* onto the system            */
                FILE *buffer;
               /* Read the user name from the client.  */

        do {    
                 done = 0;
               if ((nbytes = read(conn_fd, &buf[where],20-where)) < 0) {
                         perror("read of data failed! on the server
side");
                         exit(1);
                    }
                if (strcmp(buf,endall) == 0)
                {
                        printf("got terminated string EXIT!");
                        exit(0);
                                }

                        printf("%d is the number of bytes\n",nbytes);      
                        where += nbytes;
                        count += nbytes;        

                        if (buf[where-1] == '\0') {
                               done = 1;
                               printf("%s all done\n", buf);
                        }              
                        if (buf[20] != '\0') {
                                where = 0;
                        }

                        printf("Finished First Line of input!\n");

                        count = count -1;
                        sprintf(county, "%d\0", count);
                        printf("%s this is the count\n", county);
                        if (write(conn_fd, county, 20) < 0) {
                                perror("write failed!");
                                exit(1);}
                         count = 0;
                 if ((nbytes = read(conn_fd, &buf[where],20-where)) < 0) {
                         perror("read of data failed! on the server
side");
                         exit(1);
                    }
                                if (strcmp(buf,endall) == 0)
                                {
                                        printf("got terminated string EXIT!");
                                        exit(0);
                                }
                   } while (!done);

Can anyone tell me why this loop does not return the correct bytes on
the write? My client gets back weird things?  Basically what happens
is I enter the information from the client and the first line I enter
works, (still strange response from server) however after that I can
enter something on the client but there is no respones from the
server!
Pleae help! Thanks
dave

 
 
 

Intro Network programming HELP!!

Post by red floy » Wed, 24 Oct 2001 18:02:13





> > >    if(write(fd, line, length) != length) {
> > >       perror("write failed!\n");
> > >       exit(1);
> > >    }

> > This condition is not necessarily an error.  It is perfecly reasonable
> > for write() to write less than length bytes to the socket.  You must
> > call write repeatedly until either the entire message has been written
> > or write() returns an error condition (-1).

> > >    if (read(fd, buf, 1) == -1) {
> > >        perror("read failed!\n");
> > >        exit(1);
> > >                }

> > See comments for write(), above.  The same situation applies to read().

> What if it is writing back the wrong byte?  The server app, recieves
> the information from read correctly but when it is time to write it
> back it does not write back what I want it to, I get wierd things.
> here is a snippit from the server code:

According to your spec, it's sending you back NUL terminated ASCII.
So you need to read the socket into a char array until you get a '\0'
character.