linux下实现p2p功能

目的:实现客户端与服务器的对等通信

服务器端:

/*************************************************************************
    > 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\n",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;
}

你可能感兴趣的:(linux下实现p2p功能)