/* written by Mike Cook, optimized by Mich Cook June 1998*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define MAXLINE 512
#define MAXSIZE 128
char *pname;
/* the readn, writen, and readline methods were taken from the book Unix Network Programming by W. Richard Stevens copy 1990 */
int readn(fd, ptr, nbytes)
register int fd;
register char *ptr;
register int nbytes;
{
int nleft, nread;
nleft = nbytes;
while (nleft > 0) {
nread = read(fd, ptr, nleft);
if (nread < 0)
return nread;
else if (nread == 0)
break;
nleft -= nread;
ptr += nread;
}
return(nbytes - nleft);
}
int writen(fd, ptr, nbytes)
register int fd;
register char *ptr;
register int nbytes;
{
int nleft, nwritten;
nleft = nbytes;
while (nleft > 0) {
nwritten = write(fd, ptr, nleft);
if (nwritten <= 0)
return(nwritten);
nleft -= nwritten;
ptr += nwritten;
}
return(nbytes - nleft);
}
/* Read a line from a descriptor. Read the line one byte at a time, looking for '\n'. The line, along with '\n', is stored in the buffer, and is then followed with a '\0'. The number of characters (excluding '\0') is returned. */
int readline(fd, ptr, maxlen)
register int fd;
register char *ptr;
register int maxlen;
{
int n, rc;
char c; /* Stores the character read by readn */
for (n = 1; n < maxlen; n++) {
if ( (rc = readn(fd, &c, 1)) == 1) {
*ptr++ = c;
if (c == '\n')
break;
}
else if (rc == 0) {
if (n == 1)
return 0; /* EOF, no data read */
else
break; /* EOF, some data was read */
}
else
return -1; /* error */
}
*ptr = 0;
return n;
}
/* Read the contents of the FILE *fp, write each line to the stream socket (to the server process), and then read a line back from the socket and write it to the standard output. */
str_cli2(fp, sockfd)
register FILE *fp;
register int sockfd;
{
int n, i, spaces, sum;
char inputline[MAXLINE], prompt[41], *ptr;
int intsRead[MAXSIZE];
while (fgets(inputline, MAXLINE, fp) != NULL) {
spaces = 0;
ptr = inputline;
n = strlen(inputline);
for (i = 0; i < n; i++) {
if (inputline[i] == ' ')
spaces++;
}
spaces++; /* The number of integers on the line read */
intsRead[0] = spaces;
for (i = 1; i <= spaces; i++) {
if (i == spaces)
sscanf(ptr, "%d", &intsRead[i]);
else {
sscanf(ptr, "%d ", &intsRead[i]);
while (*ptr != ' ') {
ptr++;
}
ptr++;
}
}
if (spaces == 1) /* quit the prog. yeah.... */
return;
if (writen(sockfd, (char *)intsRead, (sizeof(int) * (intsRead[0] + 1))) != (sizeof(int) * (intsRead[0] + 1))) {
perror("str_cli: writen error on socket");
return;
}
n = readn(sockfd, (char *) &sum, sizeof(int));
if (n < 0) {
perror("str_cli: readline error");
return;
}
printf("\nThe sum of the integers entered is: %d\n", sum);
printf("\n");
readn(sockfd, prompt, 40);
prompt[40] = '\0';
printf("%s", prompt);
}
if (ferror(fp)) {
perror("str_cli: error reading file");
return;
}
}
/* Add up the sum of the integers sent to the stream socket (the server process) and
write that sum as a string to the socket. (input comes as binary) */
void add_ints2(sockfd)
int sockfd;
{
int n, i, sum;
int nums[MAXSIZE];
int *ptr;
char *message = "Please enter some more integers to add: ";
for ( ; ; )
{
sum = 0;
ptr = nums;
n = readn(sockfd, (char *)nums, sizeof(int));
ptr++;
if (n == 0)
return; /* EOF, connection terminated */
else if (n < 0) {
perror("add_ints2: read error");
return;
}
else if (n != sizeof(int)) {
perror("add_ints2: four bytes were not read");
return;
}
for (i = 1; i <= nums[0]; i++) {
n = readn(sockfd, (char *)ptr, sizeof(int));
if (n == 0) {
return; /* EOF, connection terminated */
}
else if (n < 0) {
perror("add_ints2: read error in for loop\n");
break;
}
else if (n != sizeof(int)) {
perror("add_ints2: four bytes were not read in loop\n");
break;
}
sum += *ptr;
ptr++;
}
if (writen(sockfd, (char *)&sum, sizeof(int)) != sizeof(int)) {
perror("add_ints2: write error");
return;
}
if (writen(sockfd, message, strlen(message)) != strlen(message)) {
perror("add_ints: message write error. d'oh!");
return;
}
}
}
©1998 MRC