linux에서 p2p 기능 구현

15650 단어
목적: 클라이언트와 서버의 대등한 통신을 실현한다
서버 측:
/*************************************************************************
    > File Name: test.c
    > Author: kinght
    > Mail: [email protected] 
    > Created Time: Wed 20 Jun 2018 06:58:02 PM PDT
 ************************************************************************/

#include
#include    //connect,send,recv,setsockopt 
#include       

#include      // sockaddr_in, "man 7 ip" ,htons
#include              //poll,pollfd
#include    //inet_addr,inet_aton
#include         //read,write
#include          //gethostbyname

#include          //perror
#include 
#include          //errno

#include           // memset
#include 
#include 
#include
#ifdef HAVE_NETINET_IN_H
#include 
#endif

#define ERR_EXIT(m)\
    do\
{\
    perror(m);\
    exit(EXIT_FAILURE);\
}while(0)

ssize_t readn(int fd, void *buf, size_t count)
{
    size_t nleft = count;
    ssize_t nread;
    char *bufp = (char*)buf;
    while(nleft > 0)
    {
       if((nread= read(fd,bufp,nleft)< 0))
       {
           if(errno = EINTR)
           {
               continue;
            }
           else
           {
                 return -1;
            }
        }
      else if(nread = 0)
      {
        return count - nleft;
      }
      bufp += nread;
      nleft -= nread;
     }
     return count;
}
ssize_t writen(int fd, void *buf, size_t count)
{
     size_t nleft = count;
     ssize_t nwriteen;
     char *bufp = (char*)buf;
     while(nleft > 0)
      {
        if((nwriteen= write(fd,bufp,nleft)< 0))
         {
           if(errno = EINTR)
            {
         continue;
         }
        else
        {
          return -1;
         }
       }
      else if(nwriteen = 0)
          {
            continue;
          }
         bufp += nwriteen;
         nleft -= nwriteen;
       }
     return count;

}
void doServiceConn(int conn)
{
          char recvbuf[1024];
      while(true)
      {
       memset(recvbuf,0,sizeof(recvbuf));
       int ret = read(conn,recvbuf,sizeof(recvbuf));
       if(ret == 0)
       {
           printf("client close");
           break;
       }
       else if(ret == -1)
       {
           ERR_EXIT("read");
       }

       write(conn,recvbuf,ret);
       printf(recvbuf,stdout);
      } 

}
int main(void)
{
    int listenfd;
    if((listenfd =socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))<0)
    {
        ERR_EXIT("ERROR");
    }
    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);
    int on = 1;
    if(setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on))< 0)
    {
    ERR_EXIT("SETSOCKOPT");
    }
    if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))< 0)
    {
        ERR_EXIT("error");
    }
    if(listen(listenfd,SOMAXCONN)<0)
    {
        ERR_EXIT("listen");
    }
    struct sockaddr_in peeraddr;
    socklen_t peerlen = sizeof(peeraddr);
    int conn;
        pid_t pid;
        while(1)
        {
          conn = accept(listenfd,(struct sockaddr*)&peeraddr,&peerlen);
      if(conn < 0)
      {
       ERR_EXIT("accept");
      }
      printf("ip= %s,port = %d
"
,inet_ntoa(peeraddr.sin_addr),ntohs(peeraddr.sin_port)); pid = fork(); if(pid == -1) ERR_EXIT("fork"); if(pid == 0) { char sendbuf[1024]={0}; while(fgets(sendbuf,sizeof(sendbuf),stdin)!= NULL) { write(conn,sendbuf,strlen(sendbuf)); memset(sendbuf,0,sizeof(sendbuf)); } exit(EXIT_SUCCESS); } else { char recvbuf[1024]; while(1) { memset(recvbuf,0,sizeof(recvbuf)); int ret= read(conn,recvbuf,sizeof(recvbuf)); if(ret == -1) ERR_EXIT("read"); else if(ret == 0) { printf("peer close"); break; } fputs(recvbuf,stdout); } exit(EXIT_SUCCESS); } } return 0; }

클라이언트 코드:
/*************************************************************************
    > File Name: client.c
    > Author: kinght
    > Mail: [email protected] 
    > Created Time: Wed 20 Jun 2018 06:58:02 PM PDT
 ************************************************************************/

#include
#include    //connect,send,recv,setsockopt 
#include       

#include      // sockaddr_in, "man 7 ip" ,htons
#include              //poll,pollfd
#include    //inet_addr,inet_aton
#include         //read,write
#include          //gethostbyname

#include          //perror
#include 
#include          //errno

#include           // memset
#include 
#include 
#include
#ifdef HAVE_NETINET_IN_H
#include 
#endif

#define ERR_EXIT(m)\
    do\
{\
    perror(m);\
    exit(EXIT_FAILURE);\
}while(0)

int main(void)
{
    int sock;
    if((sock =socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))<0)
    {
        ERR_EXIT("ERROR");   
    }
    struct sockaddr_in servaddr;
    memset(&servaddr,0,sizeof(servaddr));
    servaddr.sin_family=AF_INET;
    servaddr.sin_port = htons(5188);
    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    if(connect(sock,(struct sockaddr*)&servaddr,sizeof(servaddr))< 0)
    {
    ERR_EXIT("connect");
    }
    pid_t pid;
    pid = fork();
    if(pid == -1)
    {
        ERR_EXIT("pid");
    }
    else if(pid == 0)
    {
        char recvbuf[1024]={0};
        while(1)
        {
            memset(recvbuf,0,sizeof(recvbuf));
            int ret = read(sock,recvbuf,sizeof(recvbuf));
            if(ret == -1)
                ERR_EXIT("read");
            else if(ret == 0)
            {
                printf("peer close");
            }
            fputs(recvbuf,stdout);
        }
    }
    else
    {
        char sendbuf[1024]={0};
        while(fgets(sendbuf,sizeof(sendbuf),stdin)!=NULL)
        {
            write(sock,sendbuf,strlen(sendbuf));
            memset(sendbuf,0,sizeof(sendbuf));
        }

    }
    return 0;
}

좋은 웹페이지 즐겨찾기