一、程序功能

 (1)客户从标准输入读入一行文本行,并写给服务器;

 (2)服务器从网络输入读入这行文本,并回射给客户;

 (3)客户从网络输入读入这行回射文本,并显示在标准输出上

二、服务器程序

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sys/socket.h>

#define SERV_PORT 9999
#define MAXLINE 4096
#define LISTENQ 1024
#define SA struct sockaddr

void str_echo(int sockfd);

int main(int argc, char *argv[]) {
    int listenfd, connfd;
    pid_t childpid;
    socklen_t clilen;
    struct sockaddr_in cliaddr, servaddr;

    listenfd = socket(AF_INET, SOCK_STREAM, 0);

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(SERV_PORT);

    bind(listenfd, (SA *)&servaddr, sizeof(servaddr));

    listen(listenfd, LISTENQ);

    for ( ; ; ) {
        clilen = sizeof(cliaddr);
        connfd = accept(listenfd, (SA *)&cliaddr, &clilen);

        if ( (childpid = fork()) == 0) {
            close(listenfd);
            str_echo(connfd);
            exit(0);
        }
        close(connfd);
    }
    close(listenfd);
    exit(0);
}

void str_echo(int sockfd) {
    ssize_t n;
    char buf[MAXLINE];

    again:
    while ( (n = read(sockfd, buf, MAXLINE)) > 0) {
        writen(sockfd, buf, n);
        bzero(buf, sizeof(buf));
    }
    if (n < 0 && errno == EINTR) {
        goto again;
    } else if (n < 0) {
        perror("read");
    }
}

三、客户端程序

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define SA struct sockaddr
#define SERV_PORT 9999
#define MAXLINE 4096

void str_cli(FILE *fp, int sockfd);

int main(int argc, char *argv[]) {
    int sockfd;
    struct sockaddr_in servaddr;

    if (argc != 2) {
        printf("usage: tcpcli <IPaddress>\n");
        exit(0);
    }
    
    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(SERV_PORT);
    inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

    connect(sockfd, (SA *)&servaddr, sizeof(servaddr));

    str_cli(stdin, sockfd);
    
    close(sockfd);
    exit(0);
}


void str_cli(FILE *fp, int sockfd) {

    char sendline[MAXLINE], recvline[MAXLINE];

    while(fgets(sendline, MAXLINE, fp) != NULL) {
        writen(sockfd, sendline, strlen(sendline));

        if (read(sockfd, recvline, MAXLINE) < 0) {
            return;
        }
        fputs(recvline, stdout);
        bzero(recvline, sizeof(recvline));
    }
}