APUE学习笔记——socket通信 阻塞IO

服务器端:

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

#define BACKLOG 10

int main(){
    struct sockaddr_in server_sockaddr,client_sockaddr;
    int sockfd,clientfd;

    socklen_t clientsize;
    
    server_sockaddr.sin_family = AF_INET;
    server_sockaddr.sin_port = htons(9000);
    server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    bzero(&(server_sockaddr.sin_zero),8);   
 
    if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){
        perror("socket() error");
        exit(-1);
    }
    
    if(bind(sockfd,(struct sockaddr *)&server_sockaddr,sizeof(struct sockaddr)) == -1){
        perror("bind() error");
        exit(-1);
    }
    
    if(listen(sockfd,BACKLOG) == -1){
        perror("listen() error");
        exit(-1);
    }
    
    pid_t pid;
    ssize_t recvsize,sendsize;
    char buf[128];
    while(1){
        clientsize = sizeof(struct sockaddr);
        if((clientfd = accept(sockfd,(struct sockaddr*)&client_sockaddr,&clientsize)) == -1){
            perror("accept() error");
            exit(-1);
        }
        pid = fork();
        if(pid == 0){
            close(sockfd);
            while(1){
                if((recvsize = recv(clientfd,buf,sizeof(buf),0)) == -1){
                    perror("recv() error");
                    exit(-1);
                }
                if(recvsize == 0 || strncmp(buf,"quit",4) == 0)
                     exit(0);//child process exit
                printf("Received: %s",buf);
                bzero(buf,0);
                printf("Input message to send client: ");
                fgets(buf,128,stdin);
                if((sendsize = send(clientfd,buf,sizeof(buf),0)) == -1){
                    perror("send() error");
                    exit(-1);
                }
            }
        }
    }
    close(sockfd);
    return 0;
}


客户端:

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

int main(){
    struct sockaddr_in server_addr;
    int sockfd;
    
    if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){
        perror("socket() error");
        exit(-1);
    }
        
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(9000);
    inet_pton(AF_INET,"127.0.0.1",&server_addr.sin_addr);
    bzero(&(server_addr.sin_zero),8);
    

    if(connect(sockfd,(struct sockaddr*)&server_addr,sizeof(struct sockaddr)) == -1){
        perror("connect() error");
        exit(-1);
    }
    ssize_t sendsize,recvsize;
    char buf[128];
    while(1){
        printf("Input message to send server: ");
        bzero(buf,0);
        fgets(buf,128,stdin);
        if((sendsize = send(sockfd,buf,sizeof(buf),0)) == -1){
            perror("send() error");
            exit(-1);
        }
        if(sendsize == 0 || strncmp(buf,"quit",4) == 0) 
            break;
        bzero(buf,0);
        if((recvsize = recv(sockfd,buf,sizeof(buf),0)) == -1){
            perror("recv() error");
            exit(-1);
        }
        printf("Received: %s",buf);
    }
    close(sockfd);
    return 0;
}

简单说明:客户端就是循环与服务端通信没啥可以解释的,服务端使用while循环来accept多个客户端的访问请求,但具体的通信操作交给子进程,父进程只负责监听客户端的请求。具体例子参考UNIX网络编程(套接字)

发布了101 篇原创文章 · 获赞 26 · 访问量 46万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览