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