Unix Network Programming Episode 27

#include "unp.h"

static int read_cnt;
static char *read_ptr;
static char read_buf[MAXLINE];

static ssize_t my_read(int fd, char *ptr)
{
    if(read_cnt<=0)
    {
        again:
            if((read_cnt=read(fd, read_buf,sizeof(read-buf)))<0)
            {
                if(errno==EINTR)
                    goto again;
                return -1;
            }
            else if(read_cnt==0)
            {
                return 0;
            }
            read_ptr=read_buf;
    }
    
    read_cnt--;
    *ptr=*read_ptr++;
    return 1;
}

ssize_t readline(int fd, vod *vptr, size_t maxlen)
{
    ssize_t n,rc;
    char c, *ptr;

    ptr=vptr;
    for(n=1;n

Better version of readline function

Elementary TCP Sockets

Introduction

This chapter describes the elementary socket functions required to write a complete TCP client and server. We will first describe all the elementary socket functions that we will be using and then develop the client and server in the next chapter. We will work with this client and server throughout the text, enhancing it many times.

We will also describe concurrent servers, a common Unix technique for providing concurrency when numerous clients are connected to the same server at the same time. Each client connection causes the server to fork a new process just for that client. In this chapter, we consider only the one-process-per-client model using fork, but we will consider a different one-thread-per-client model when we describe threads in Chapter 26.

你可能感兴趣的:(Unix,Network,Programming)