linux支持并发的服务器回射程序实例

实例一:不支持并发,单服务器---单客户端

/*************************************************************************
	> File Name: ser01.c
	> Author: 
	> Mail: 
	> Created Time: 2015年11月22日 星期日 10时22分17秒
 ************************************************************************/

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

int main()
{
   //int socket(int domain, int type, int protocol);
    int sockfd;
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    
    struct sockaddr_in seraddr;
    seraddr.sin_family = AF_INET;
    seraddr.sin_port = htons(8001);
    seraddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    //int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
    bind(sockfd, (struct sockaddr *)&seraddr, sizeof(seraddr));

    listen(sockfd, 128);

    struct sockaddr cliaddr;
    socklen_t clilen;
    int connfd;
    connfd = accept(sockfd, (struct sockaddr *)&cliaddr, &clilen);
    
    char recvbuf[2014] = {0};
    int recvcount = 0;

    while(1){
        recvcount = read(connfd, recvbuf,1024); 
        if (recvcount == 0){
            printf("read is over.\n");
            printf("the client is over.\n");
            exit(0);
        }
        else if (recvcount == -1){
            perror("read error.\n");
            exit(EXIT_FAILURE);
        }
        fputs(recvbuf, stdout);
        write(connfd, recvbuf, strlen(recvbuf));
        memset(recvbuf, 0, 1024);
    }


    exit(0);
}

 

/*************************************************************************
	> File Name: cli01.c
	> Author: 
	> Mail: 
	> Created Time: 2015年11月22日 星期日 11时56分05秒
 ************************************************************************/

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


int main()
{
    
    int sockfd, ret;
    struct sockaddr_in seraddr;
    seraddr.sin_family = AF_INET;
    seraddr.sin_port = htons(8001);
    seraddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    ret = connect(sockfd, (struct sockaddr*)&seraddr, sizeof(seraddr));
    if(ret == -1){
        perror("bind error.\n");
        exit(EXIT_FAILURE);
    }
    
    char recvbuf[1024] = {0};
    char sendbuff[1024] = {0};

    while(fgets(sendbuff, 1024, stdin) != NULL){
        write(sockfd, sendbuff, strlen(sendbuff));
        read(sockfd, recvbuf, 1024);
        fputs(recvbuf, stdout);
        memset(recvbuf, 0, 1024);
        memset(sendbuff, 0, 1024);
    }
    
    exit(0);
}

server:

client:

 

实例二:支持多并发,单服务器----多客户端

/*************************************************************************
	> File Name: ser02.c
	> Author: 
	> Mail: 
	> Created Time: 2015年11月22日 星期日 10时22分17秒
 ************************************************************************/

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

int main()
{
   //int socket(int domain, int type, int protocol);
    int sockfd;
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    
    struct sockaddr_in seraddr;
    seraddr.sin_family = AF_INET;
    seraddr.sin_port = htons(8001);
    seraddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    //int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
    bind(sockfd, (struct sockaddr *)&seraddr, sizeof(seraddr));

    listen(sockfd, 128);


    struct sockaddr_in cliaddr;
    socklen_t clilen;
    
    int connfd;
    char recvbuf[2014] = {0};
    int recvcount = 0;

    while(1){
        connfd = accept(sockfd, (struct sockaddr *)&cliaddr, &clilen);
        if(connfd == -1){
            perror("accept error.\n");
            exit(EXIT_FAILURE);
        }
        pid_t pid = fork();
    
        if(pid == 0){
            while(1){
                recvcount = read(connfd, recvbuf,1024); 
                if (recvcount == 0){
                    printf("%s:%d is over.\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
                    exit(0);
                }
                else if (recvcount == -1){
                    perror("read error.\n");
                    exit(EXIT_FAILURE);
                }
                printf("%d receive from %s:%d - ", getpid(), inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
                fputs(recvbuf, stdout);
                write(connfd, recvbuf, strlen(recvbuf));
                memset(recvbuf, 0, 1024);
            }
        }
        else if(pid > 0){
                
        }
        else if(pid < 0){
            perror("fork error.\n");
            exit(EXIT_FAILURE);
        }
    }

    exit(0);
}

 

/*************************************************************************
	> File Name: cli02.c
	> Author: 
	> Mail: 
	> Created Time: 2015年11月22日 星期日 11时56分05秒
 ************************************************************************/

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


int main()
{
    
    int sockfd, ret;
    struct sockaddr_in seraddr;
    seraddr.sin_family = AF_INET;
    seraddr.sin_port = htons(8001);
    seraddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    ret = connect(sockfd, (struct sockaddr*)&seraddr, sizeof(seraddr));
    if(ret == -1){
        perror("bind error.\n");
        exit(EXIT_FAILURE);
    }
    
    char recvbuf[1024] = {0};
    char sendbuff[1024] = {0};

    while(fgets(sendbuff, 1024, stdin) != NULL){
        write(sockfd, sendbuff, strlen(sendbuff));
        read(sockfd, recvbuf, 1024);
        printf("receive from %s:%d - ", inet_ntoa(seraddr.sin_addr), ntohs(seraddr.sin_port));
        fputs(recvbuf, stdout);
        memset(recvbuf, 0, 1024);
        memset(sendbuff, 0, 1024);
    }
    
    exit(0);
}

server:

client1:

client2:

client3:

server(此时三个客户端退出):

转载于:https://www.cnblogs.com/yxk529188712/p/4985920.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值