Many clients in UNIX Network Programming connect to the server and can reuse socket pairs.

Source: Internet
Author: User
Tags htons

Client and server program templates supported by network programming:

1. connect multiple clients to the server at the same time, that is, the service program can serve multiple clients at the same time;

2. The server supports socket pair reuse, that is, even in the time_wait status, the server can be restarted;

3. The server can check whether the client is disconnected;

4. The standard input of the client is displayed, and the server displays the content entered by the client from the standard output.

 

The program is as follows. Write down the template for reuse:

The client program is as follows:

/*************************************************************************    > File Name: p2pcli.c    > Author: ma6174    > Mail: [email protected]     > Created Time: Sun 05 Oct 2014 09:26:40 PM HKT ************************************************************************/#include<stdio.h>#include <errno.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <netinet/in.h>#include <string.h>#include <sys/socket.h>#include <arpa/inet.h>#define ERR_EXIT(m)         do         {             perror(m);             exit(EXIT_FAILURE);         }while(0)int main(int argc, char **argv){    int sockfd;    struct sockaddr_in servaddr;    if(argc != 2)    {        //printf("usage: p2pcli <IPaddress> ");        //exit(0);        ERR_EXIT("usage: p2pcli <IPaddress> ");    }    sockfd = socket(AF_INET, SOCK_STREAM, 0);    memset(&servaddr, 0, sizeof(servaddr));    servaddr.sin_family = AF_INET;    servaddr.sin_port = htons(5188);    servaddr.sin_addr.s_addr = inet_addr(argv[1]);    if(connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)    {        ERR_EXIT("connect");    }    //communication    char sendbuf[1024] = {0};    char recvbuf[1024] = {0};    while(fgets(sendbuf, sizeof(sendbuf), stdin) != NULL)    {        //send to server.        write(sockfd, sendbuf, strlen(sendbuf));        //read from server to display.        read(sockfd, recvbuf, sizeof(recvbuf));        //display        fputs(recvbuf, stdout);        memset(sendbuf, 0, sizeof(sendbuf));        memset(recvbuf, 0, sizeof(recvbuf));    }    close(sockfd);    return 0;}

The server program is as follows:

/*************************************************************************    > File Name: p2psrv.c    > Author: ma6174    > Mail: [email protected]     > Created Time: Sun 05 Oct 2014 08:27:06 PM HKT ************************************************************************/#include<stdio.h>#include <stdlib.h>#include <errno.h>#include <unistd.h>#include <sys/types.h>#include <netinet/in.h>#include <sys/socket.h>#include <string.h>#include <arpa/inet.h>#define ERR_EXIT(m)        do        {            perror(m);            exit(EXIT_FAILURE);        }while(0)void do_service(int connfd){    //communication    char recvbuf[1024];    while(1)    {        memset(recvbuf, 0, sizeof(recvbuf));        int ret = read(connfd, recvbuf, sizeof(recvbuf));        if(ret == 0)        {            printf("client close.\n");            break;        }        else if(ret == -1)        {            break;        }        else        {            fputs(recvbuf, stdout);            write(connfd, recvbuf, ret);        }    }}int main(){    int listenfd;    if( (listenfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)    {        ERR_EXIT("socket");    }    struct sockaddr_in servaddr;    memset(&servaddr, 0, sizeof(servaddr));    servaddr.sin_family = AF_INET;    servaddr.sin_port = htons(5188);    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);    //reuse address    int on = 1;    if(setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)))    {        ERR_EXIT("setsockopt");    }    if(bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)    {        ERR_EXIT("bind");    }    if(listen(listenfd, SOMAXCONN) < 0)    {        ERR_EXIT("listen");    }    struct sockaddr_in peeraddr;    socklen_t peerlen = sizeof(peeraddr);    int connfd;    //muti client    pid_t pid;    while(1)    {        if( (connfd = accept(listenfd, (struct sockaddr*)&peeraddr, &peerlen)) < 0 )        {            ERR_EXIT("accept");        }        printf("ip=%s, port=%d\n", inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port));        pid = fork();        if(pid == -1)        {            ERR_EXIT("fork");        }        else if(pid == 0)        {            //child            close(listenfd);            do_service(connfd);            exit(0);        }        else        {            close(connfd);        }    }    close(connfd);    close(listenfd);    return 0;}

 

Many clients in UNIX Network Programming connect to the server and can reuse socket pairs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.