码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Unix Network Programming Episode 80


    ‘tcp_connect’ Function

    We will now write two functions that use getaddrinfo to handle most scenarios for
    the TCP clients and servers that we write. The first function, tcp_connect, performs
    the normal client steps: create a TCP socket and connect to a server.

    #include "unp.h"
    int tcp_connect (const char *hostname, const char *service);
    
    • 1
    • 2
    #include "unp.h"
    
    int tcp_connect(const char *host, const char *serv)
    {
        int socfd, n;
        struct addrinfo hints, *res, *ressave;
    
        bzero(&hints, sizeof(struct addrinfo));
        hints.ai_family=AF_UNSPEC;
        hints.ai_socktype=SOCK_STREAM;
        
        if((n=getaddrinfo(host, serv, &hints, &res))!=0)
            err_quit("tcp_connect error for %s, %s: %s", host, serv, gai_strerror(n));
        
        ressave=res;
    
        do{
            sockfd=socket(res->ai_family, res->ai_socktype, res->ai_protocol);
            if(sockfd<0)
            {
                continue;
            }
            if(connect(sockfd, res->ai_addr, res->ai_addrlen)==0)
                break;
            Close(sockfd);
        }while((res=res->ai_next)!=null);
    
        if(res==NULL)
            err_sys("tcp_connect error for %s, %s",host, serv);
        
        freeaddrinfo(ressave);
    
        return sockfd;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    tcp_connect function: performs normal client steps

    #include "unp.h"
    
    int main(int argc, char **argc)
    {
        int sockfd, n;
        char recvline[MAXLINE+1];
        socklen_t len;
        struct sockaddr_storage ss;
    
        if(argc!=3)
            err_quit("usage: daytimetcpcli  ");
        
        sockfd=Tcp_connect(argv[1], argv[2]);
        len=sizeof(ss);
        Getpeername(sockfd, (SA *)&ss, &len);
        printf("connected to %s\n", Sock_ntop_host((SA *)&ss, len));
    
        while((n=Read(sockfd, recvline, MAXLINE))>0)
        {
            recvline[n]=0;
            Fputs(recvline,stdout);
        }
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    Daytime client recorded to use tcp_connect

    ‘tcp_listen’ Function

    Our next function, tcp_listen, performs the normal TCP server steps: create a TCP socket, bind the server’s well-known port, and allow incoming connection requests to be accepted. Figure 11.12 shows the source code.

    #include "unp.h"
    int tcp_listen (const char *hostname, const char *service, socklen_t *addrlenp);
    
    • 1
    • 2
  • 相关阅读:
    Matlab匿名函数教程
    2023年中国一次性塑料餐具市场供需现状、市场规模及行业发展前景分析[图]
    ZooKeeper之Java API的基本使用以及应用场景的实现
    【蓝桥杯物联网赛项学习日志】Day4 关于USART/UART
    MySQL日志及执行计划
    黑马点评-05缓存穿透问题及其解决方案,缓存空字符串或使用布隆过滤器
    JAVA MongoDB 连接以及增删改查
    Docker常用命令
    解决vue3 mitt路由跳转后 on事件获取不到值的奇葩问题解决
    使用控制台方式部署sentinel
  • 原文地址:https://blog.csdn.net/myfather103/article/details/82705198
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号