zoukankan      html  css  js  c++  java
  • 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
    ", inet_ntop(AF_INET,&peer.sin_addr, buf, 1024), ntohs(peer.sin_port));
    
          }
    
          while(1)
          {
              memset(buf, '', sizeof(buf));
              ssize_t size = read(accept_fd, buf, sizeof(buf) - 1);
    
              if(size > 0)
              {
                  printf("client#: %s
    ", buf);
              }
              else if(size == 0)
              {
                  printf("read is done...
    ");
                  break;
              }
              else 
              {
                  perror("read");
                  break;
              }
              printf("server please enter: ");
              fflush(stdout);
              size = read(0, buf, sizeof(buf) - 1);
              if(size > 0)
              {
                  buf[size - 1] = '';
              }
              else if(size == 0)
              {
                  printf("read is done...
    ");
                  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, '', sizeof(buf));
              printf("client please enter: ");
              fflush(stdout);
              ssize_t size = read(0, buf, sizeof(buf) - 1);
              if(size > 0)
              {
                  buf[size - 1] = '';
              }
              else if(size == 0)
              {
                  printf("read is done...
    ");
                  break;
              }
              else
              {
                  perror("read");
                  return 4;
              }
    
              write(clt_sock, buf, strlen(buf));
              size = read(clt_sock, buf, sizeof(buf));
              if(size > 0)
              {
                  buf[size] = '';
              }
              else if(size == 0)
              {
                  printf("read is done...
    ");
                  break;
              }
              else 
              {
                  perror("read");
                  return 5;
              }
              printf("server: %s
    ", buf);
           }
          close(clt_sock);
          return 0;
    }

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

    技术不分国界
  • 相关阅读:
    详解javascript实现自定义事件
    详谈LABJS按需动态加载js文件
    SeaJS入门教程系列之SeaJS介绍(一)
    Underscore.js 入门
    Underscore.js (1.7.0)-集合(Collections)(25)
    Underscore.js (1.7.0)-函数预览
    js/jquery判断浏览器的方法小结
    ParNew收集器
    CMS(Concurrent Mark-Sweep)
    java集合类深入分析之Queue篇(Q,DQ)
  • 原文地址:https://www.cnblogs.com/angels-yaoyao/p/12443609.html
Copyright © 2011-2022 走看看