angel的博客

一个人的旅途

Linux一对一的通信

服务端:

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

const int port = 8080;
const char* ip = "127.0.0.1"; 

int main()
{
      int ser_sock = socket(AF_INET, SOCK_STREAM, 0); 
      if(ser_sock < 0)
      {

          perror("socket");
          return 1;
      }
      struct sockaddr_in addr; 
      addr.sin_family = AF_INET; 
      addr.sin_port = htons(port); 

      addr.sin_addr.s_addr = inet_addr(ip); 
      if(bind(ser_sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) 
      {             

           perror("bind"); 
           return 2;       
      } 


      int listen_sock = listen(ser_sock, 5);
      if(listen_sock < 0)
      {

          perror("listen");
          return 3;
      }


      struct sockaddr_in peer;
      socklen_t peer_len;
      char buf[1024];
      int accept_fd = accept(ser_sock, (struct sockaddr*)&peer, &peer_len);

      if(accept_fd < 0)
      {
          perror("accept");
          return 4;
      }
      else
      {
          printf("connected with ip: %s  and port: %d\n", inet_ntop(AF_INET,&peer.sin_addr, buf, 1024), ntohs(peer.sin_port));

      }

      while(1)
      {
          memset(buf, '\0', sizeof(buf));
          ssize_t size = read(accept_fd, buf, sizeof(buf) - 1);

          if(size > 0)
          {
              printf("client#: %s\n", buf);
          }
          else if(size == 0)
          {
              printf("read is done...\n");
              break;
          }
          else 
          {
              perror("read");
              break;
          }
          printf("server please enter: ");
          fflush(stdout);
          size = read(0, buf, sizeof(buf) - 1);
          if(size > 0)
          {
              buf[size - 1] = '\0';
          }
          else if(size == 0)
          {
              printf("read is done...\n");
              break;
          }
          else
          {
              perror("read");
              break;
          }   
          write(accept_fd, buf, strlen(buf));
      }
        close(ser_sock);
      return 0;
}
`

 ``客户端:



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

const int port = 8080;
const char* ip = "127.0.0.1"; 

int main()
{

      int clt_sock = socket(AF_INET, SOCK_STREAM, 0); 
      if(clt_sock < 0)
      {

          perror("socket");
          return 1;
      }


      struct sockaddr_in addr; 
      addr.sin_family = AF_INET; 
      addr.sin_port = htons(port); 

      addr.sin_addr.s_addr = inet_addr(ip); 



      socklen_t addr_len = sizeof(addr);
      int connect_fd = connect(clt_sock, (struct sockaddr*)&addr, addr_len);
      if(connect_fd < 0)
      {
          perror("connect");
          return 2;
      }
      char buf[1024];

      while(1)
      {
          memset(buf, '\0', sizeof(buf));
          printf("client please enter: ");
          fflush(stdout);
          ssize_t size = read(0, buf, sizeof(buf) - 1);
          if(size > 0)
          {
              buf[size - 1] = '\0';
          }
          else if(size == 0)
          {
              printf("read is done...\n");
              break;
          }
          else
          {
              perror("read");
              return 4;
          }

          write(clt_sock, buf, strlen(buf));
          size = read(clt_sock, buf, sizeof(buf));
          if(size > 0)
          {
              buf[size] = '\0';
          }
          else if(size == 0)
          {
              printf("read is done...\n");
              break;
          }
          else 
          {
              perror("read");
              return 5;
          }
          printf("server: %s\n", buf);
       }
      close(clt_sock);
      return 0;
}

一对一的通信,客户端接收数据,服务端就等待着客户端发送数据。

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LV_YONG/article/details/79966581
个人分类: UNIX高级环境编程
上一篇进程间通信
下一篇Linux一对多的通信
想对作者说点什么? 我来说一句

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

关闭
关闭