• socket 套接字


    网络:交换机,路由器,网线

    交换机:分配..

    路由器:找寻网络线路

    网络架构: 应用层 ---> 表示层 ---> 会话层 ---> 传输层 ---> 网络层 ---> 数据链路层 ----> 物理层

    TCP/IP 商用: 应用层 ---> 传输层 ---> 网络层 ---> 物理+数据链路

    socket 在传输层和应用层之间

    TCP/IP 传输流程:

      

    一次连接三次握手:客服端访问服务器端,服务器端响应,服务器访问客户端,客户端响应

    套接字之间的连接过程可以分为三个步骤:服务器监听,客户端请求,连接确认。

    //服务器端

    #import "AppDelegate.h"
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>

    @interface AppDelegate (){
        
        //服务器标志
        
        int server_flag;
        
        //客户端标志
        
        int client_flag;
        
        //地址
        struct sockaddr_in addr;
        
    }



    @end

    @implementation AppDelegate


    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        
        // 错误
        int error = -1;
        
    #pragma mark --- 服务器端设置 ---
        
        //创建服务器端 (IP类型,通信协议, )
        server_flag = socket(AF_INET, SOCK_STREAM, 0);
        
        //将服务器绑定到一个具体的计算机
        //端口
        addr.sin_port = htons(9006);
        //计算机
        addr.sin_addr.s_addr = INADDR_ANY;
        //IPV4
        addr.sin_family = AF_INET;
        
        //server_flag 绑定
        
        error = bind(server_flag, (struct sockaddr*)&addr, sizeof(addr));
        
        //最大连接数
        error = listen(server_flag, 100);
        
        printf("服务器启动成功! ");
        
        //等待
        
        while (YES) {
            //等待接收
            client_flag = accept(server_flag, NULL, NULL);
            
            
            //保存接收数据
            //字符数组
            char buff[1024] = {0};
            
            //接收数据长度
            long length = 0;
            
            length = recv(client_flag, buff, 1024, 0);
            
            buff[length] = '';
            printf("client asy : %s ",buff);
            
            //发送数据
            send(client_flag, "hello world", 80, 0);
            
            close(server_flag);
            
        }
        
        
        return YES;
    }

    //客户端

    #import "AppDelegate.h"
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>


    @interface AppDelegate ()
    {
        int client_flag ;
        
        struct sockaddr_in server;

    }

    @end

    @implementation AppDelegate


    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        
        
        int error = -1;
        
        client_flag = socket(AF_INET, SOCK_STREAM, 0);
        
        server.sin_family = AF_INET;
        server.sin_port = htons(9006);
        server.sin_addr.s_addr = inet_addr("172.18.16.158");
        
        error = connect(client_flag, (struct sockaddr*)&server, sizeof(server));
        send(client_flag, "鲁二蛋", 1024, 0);
        
        char buff[1024] = {0};
        
        long length = 0;
        
        
        
        length = recv(client_flag, buff, 1024, 0);
        
        buff[length] = '';
        printf("server say : %s ",buff);
        
    //    close(client_flag);
        
        return YES;
    }

  • 相关阅读:
    jenkins部署前端node项目实例
    阿里云云盘扩容数据盘_Linux
    输入子系统
    触摸屏设备驱动程序
    LCD设备驱动程序
    IIC设备驱动程序
    看门狗驱动程序
    RTC实时时钟驱动
    网络设备驱动程序数据结构
    Linux 设备驱动模型
  • 原文地址:https://www.cnblogs.com/Ager/p/5076953.html
Copyright © 2020-2023  润新知