现在的位置: 首页 > 综合 > 正文

1.7 客户/服务器应用编程

2014年03月24日 ⁄ 综合 ⁄ 共 2980字 ⁄ 字号 评论关闭

下面是个例子,父进程调用 socketpair 生成套接口,再调用 fork 生成子进程。父进程代表服务器端,子进程代表客户端。

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

 

int main(int argc, char**argv)
{
        int z;/* 返回的状态码 */
        int s[2];/* 套接口对 */
        char *msgp;/* 消息指针 */
        int mlen;/* 消息长度 */
        char buf[80];/* 工作缓冲区 */
        pid_t chpid;/* 子进程pid */

        /*
        *生成本地套接口
        * */
        z = socketpair(AF_LOCAL, SOCK_STREAM, 0, s);

        if (z == -1){
                fprintf(stderr, "socketpair error:%s/n",strerror(errno));
                exit(1);
        }

        if ((chpid = fork()) == (pid_t)-1){
                fprintf(stderr, "fork error: %s/n",strerror(errno));
        } else if(chpid == 0) {/* 在子进程中 */
                char rxbuf[80];/* 接收缓冲区 */
                printf("Parent PID is %ld/n", (long)getppid());
                close(s[0]); /* 服务器端使用s[0] */
                s[0] = -1;
                /* 形成消息并记录长度 */
                msgp = "%A %d-%b-%Y %l:%M%p"; /* 时间格式 */
                mlen = strlen(msgp);

                printf("Child sending request:%s/n", msgp);
                fflush(stdout);
                /* 向服务器写入请求 */
                z = write(s[1], msgp, mlen);

                if (z < 0){
                        fprintf(stderr, "write error:%s/n", strerror(errno));
                        exit(1);
                }

                /* 关闭套接口,不再向套接口写入任何消息 */

               if (shutdown(s[1], SHUT_WR) == -1){
                        fprintf(stderr, "shutdown error:%s/n",strerror(errno));
                        exit(1);
                }
                /* 接收来自服务器端的应答 */
                z = read(s[1], rxbuf, sizeof(rxbuf));/* 注意第三个参数为最大size */
                if (z < 0){
                        fprintf(stderr, "read error:%s/n",strerror(errno));
                        exit(1);
                }
                rxbuf[z] = 0; /* 消息尾加上空字节 */
                /* 报告结果 */
                printf("Server returned:%s/n", rxbuf);
                fflush(stdout);

                close(s[1]);/* 关闭客户端 */
      } else {/* 父进程 */
                int status;/* 子进程终止码状态 */
                char txbuf[80];/* 应答缓冲区 */
                time_t td;/* 现在的日期 */

                printf("Child's PID is %ld/n", (long)chpid);
                fflush(stdout);

                close(s[1]);/* 客户使用s[1] */
                s[1] = -1;

                /* 等待来自客户的请求 */
                z = read(s[0], buf, sizeof(buf));
                if (z < 0){
                        fprintf(stderr, "read error:%s/n", strerror(errno));
                        exit(1);
                }
                buf[z] = 0;/* 加上串结束符 */

                time(&td);/* 获取现在的时间 */
                strftime(txbuf, sizeof(txbuf)/* 缓冲区 */
                        ,buf,           /* 输入格式 */
                        localtime(&td));/* 输入时间 */
                /* 将结果发送给客户端 */
                z = write(s[0], txbuf, strlen(txbuf));
                if (z < 0) {
                        fprintf(stderr, "write error:%s",strerror(errno));
                        exit(1);
                }
                /* 关闭套接口*/
                close(s[0]);
                /* 等待子进程退出 */
                waitpid(chpid, &status, 0);
        }

        return 0;
}

抱歉!评论已关闭.