参考文章:C网络编程socket之connect函数

需研究下这个函数超时多久才返回。。。

文章目录

  • 项目中注释解释
  • man 2 文档解释
  • 关于上面man 2 手册中所提到的connect()案例,在getaddrinfo(3)中
  • 20220411 基于连接的协议套接字确实只能成功connect一次,第二次connect就不行了,那我们如何测试与服务器是否连通?或者如何测试socket是否已经断开呢?
    • 测试判断socket是否已经断开

项目中注释解释

/* Open a connection on socket FD to peer at ADDR (which LEN bytes long).For connectionless socket types, just set the default address to send toand the only address from which to accept transmissions.Return 0 on success, -1 for errors.This function is a cancellation point and therefore not marked with__THROW.  */
extern int connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);

在套接字 FD 上打开一个连接以与 ADDR 对等(其中 LEN 字节长)。
对于无连接套接字类型,只需设置要发送到的默认地址和接受传输的唯一地址。
成功返回 0,错误返回 -1。

该函数是一个取消点,因此没有用 __THROW 标记。

man 2 文档解释

CONNECT(2)                                                           Linux Programmer's Manual                                                          CONNECT(2)NAMEconnect - initiate a connection on a socket   //初始化套接字上的连接SYNOPSIS#include <sys/types.h>          /* See NOTES */#include <sys/socket.h>int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen);DESCRIPTIONThe  connect()  system call connects the socket referred to by the file descriptor sockfd to the address specified by addr.  The addrlen argument specifies the size of addr.  The format of the address in addr is determined by the address space of the socket sockfd; see socket(2) for further details.//connect()系统调用将文件描述符sockfd引用的套接字连接到addr指定的地址。//addrlen参数指定addr的大小。//addr中地址的格式由socket sockfd的地址空间决定;详见socket(2)。If the socket sockfd is of type SOCK_DGRAM, then addr is the address to which datagrams are sent by default, and the only address from which datagrams  are received.  If the socket is of type SOCK_STREAM or SOCK_SEQPACKET, this call attempts to make a connection to the socket that is bound to the address spec‐ ified by addr.//如果套接字 sockfd 的类型为 SOCK_DGRAM,则 addr 是默认发送数据报的地址,也是接收数据报的唯一地址。 //如果套接字是 SOCK_STREAM 或 SOCK_SEQPACKET 类型,则此调用尝试与绑定到 addr 指定地址的套接字建立连接。Generally, connection-based protocol sockets may successfully connect() only once; connectionless protocol sockets may  use  connect()  multiple  times  to change  their  association.   Connectionless  sockets may dissolve the association by connecting to an address with the sa_family member of sockaddr set to AF_UNSPEC (supported on Linux since kernel 2.2).
//一般来说,基于连接的协议套接字可能只成功connect()一次;
//无连接协议套接字可以多次使用 connect() 来更改它们的关联。
//无连接套接字可以通过连接到将 sockaddr 的 sa_family 成员设置为 AF_UNSPEC 的地址来解除关联(自内核 2.2 起在 Linux 上受支持)。RETURN VALUEIf the connection or binding succeeds, zero is returned.  On error, -1 is returned, and errno is set appropriately.
//如果连接或绑定成功,则返回零。
//出错时,返回 -1,并适当设置 errno。ERRORSThe following are general socket errors only.  There may be other domain-specific error codes.//以下只是一般的套接字错误。 可能还有其他特定于域的错误代码。EACCES For UNIX domain sockets, which are identified by pathname: Write permission is denied on the socket file, or search permission is denied for one  of the directories in the path prefix.  (See also path_resolution(7).)//EACCES 对于由路径名标识的 UNIX 域套接字:套接字文件的写入权限被拒绝,或者路径前缀中的目录之一的搜索权限被拒绝。 (另见 path_resolution(7)。)EACCES, EPERMThe  user  tried  to  connect  to a broadcast address without having the socket broadcast flag enabled or the connection request failed because of a local firewall rule.//用户尝试在未启用套接字广播标志的情况下连接到广播地址,或者连接请求由于本地防火墙规则而失败。EADDRINUSELocal address is already in use.//本地地址已被使用。EADDRNOTAVAIL(Internet domain sockets) The socket referred to by sockfd had not previously been bound to an address  and,  upon  attempting  to  bind  it  to  an ephemeral  port,  it  was  determined  that  all  port  numbers  in  the  ephemeral  port  range  are  currently  in  use.   See  the  discussion of /proc/sys/net/ipv4/ip_local_port_range in ip(7).//(互联网域套接字) sockfd 引用的套接字以前没有绑定到地址,并且在尝试将其绑定到临时端口时,确定临时端口范围内的所有端口号当前都在使用中。 请参阅 ip(7) 中对 /proc/sys/net/ipv4/ip_local_port_range 的讨论。EAFNOSUPPORTThe passed address didn't have the correct address family in its sa_family field.//传递的地址在其 sa_family 字段中没有正确的地址族。EAGAIN Insufficient entries in the routing cache.   //路由缓存中的条目不足。EALREADYThe socket is nonblocking and a previous connection attempt has not yet been completed.    //套接字是非阻塞的,并且之前的连接尝试尚未完成。EBADF  The file descriptor is not a valid index in the descriptor table.    //文件描述符不是描述符表中的有效索引。ECONNREFUSEDNo-one listening on the remote address. //没有人监听远程地址。EFAULT The socket structure address is outside the user's address space.//套接字结构地址在用户地址空间之外。EINPROGRESSThe socket is nonblocking and the connection cannot be completed immediately.  It is possible to select(2) or poll(2) for  completion  by  selecting the  socket  for  writing.   After  select(2)  indicates writability, use getsockopt(2) to read the SO_ERROR option at level SOL_SOCKET to determine whether connect() completed successfully (SO_ERROR is zero) or unsuccessfully (SO_ERROR is one of the usual error codes listed here, explaining  the reason for the failure).//套接字是非阻塞的,连接不能立即完成。 //可以通过选择要写入的套接字来select(2)或poll(2)来完成。 //在 select(2) 指示可写后,使用 getsockopt(2) 读取 SOL_SOCKET 级别的 SO_ERROR 选项,以确定 connect() 是成功完成(SO_ERROR 为零)还是未成功完成(SO_ERROR 是此处列出的常见错误代码之一,解释了 失败的原因)。EINTR  The system call was interrupted by a signal that was caught; see signal(7).//系统调用被捕获的信号中断; 见信号(7)。EISCONNThe socket is already connected.//套接字已经连接。ENETUNREACHNetwork is unreachable.//网络不可达。ENOTSOCKThe file descriptor sockfd does not refer to a socket.//文件描述符 sockfd 不引用套接字。EPROTOTYPEThe  socket  type does not support the requested communications protocol.  This error can occur, for example, on an attempt to connect a UNIX domain datagram socket to a stream socket.//套接字类型不支持请求的通信协议。 例如,在尝试将 UNIX 域数据报套接字连接到流套接字时,可能会发生此错误。ETIMEDOUTTimeout while attempting connection.  The server may be too busy to accept new connections.  Note that for IP sockets the timeout may be  very  long when syncookies are enabled on the server.//尝试连接时超时。 服务器可能太忙而无法接受新连接。 //请注意,对于 IP 套接字,当服务器上启用 syncookies 时,超时可能会很长。CONFORMING TOPOSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD, (the connect() function first appeared in 4.2BSD).NOTESPOSIX.1  does  not  require  the inclusion of <sys/types.h>, and this header file is not required on Linux.  However, some historical (BSD) implementations required this header file, and portable applications are probably wise to include it.//POSIX.1 不需要包含 <sys/types.h>,Linux 上也不需要这个头文件。 但是,一些历史 (BSD) 实现需要此头文件,并且可移植应用程序可能明智地包含它。The third argument of connect() is in reality an int (and this is what 4.x BSD and libc4 and libc5 have).  Some POSIX confusion  resulted  in  the  present socklen_t, also used by glibc.  See also accept(2).//connect() 的第三个参数实际上是一个 int(这就是 4.x BSD 和 libc4 和 libc5 所具有的)。 一些 POSIX 混淆导致了现在的 socklen_t,glibc 也使用了它。 另请参阅接受 (2)。If connect() fails, consider the state of the socket as unspecified.  Portable applications should close the socket and create a new one for reconnecting.//如果 connect() 失败,则认为套接字的状态未指定。 //便携式应用程序应该关闭套接字并创建一个新的用于重新连接。EXAMPLEAn example of the use of connect() is shown in getaddrinfo(3).//getaddrinfo(3) 中显示了一个使用 connect() 的示例。SEE ALSOaccept(2), bind(2), getsockname(2), listen(2), socket(2), path_resolution(7)COLOPHONThis  page  is part of release 4.04 of the Linux man-pages project.  A description of the project, information about reporting bugs, and the latest versionof this page, can be found at http://www.kernel.org/doc/man-pages/.//此页面是 Linux 手册页项目 4.04 版的一部分。 可以在 http://www.kernel.org/doc/man-pages/ 找到项目的描述、关于报告错误的信息以及此页面的最新版本。Linux                                                                       2015-12-28                                                                  CONNECT(2)

关于上面man 2 手册中所提到的connect()案例,在getaddrinfo(3)中

20220411 基于连接的协议套接字确实只能成功connect一次,第二次connect就不行了,那我们如何测试与服务器是否连通?或者如何测试socket是否已经断开呢?


测试判断socket是否已经断开

这个方法有很多,参考

linux C语言 socket如何判断socket_fd对应的socket是否断开?(是否连通、是否正常连接)

C语言socket connect()函数(初始化套接字上的连接)(未完)(如何测试socket是否已经断开,如何判断socket是否断开)相关推荐

  1. C语言socket accept()函数(提取出所监听套接字的等待连接队列中第一个连接请求,创建一个新的套接字,并返回指向该套接字的文件描述符)

    文章目录 名称 使用格式 功能参数描述 参数 sockfd addr addrlen 返回值 示例 man 2 文档中的accept解释 错误处理 名称 accept() 接收一个套接字中已建立的连接 ...

  2. 进服务器显示系统缓冲区空间不足,由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作--解决方法...

    异常信息: System.Net.Sockets.SocketException (0x80004005): 由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作. 192.168.0.187:2 ...

  3. C语言 socket shutdown()函数(将与 sockfd 关联的套接字上的全双工连接全部或部分关闭)

    man 2 文档 [root@ubuntu /arnold_test/20220324_hikflow_demo__socket_server_test]102# man -f shutdown sh ...

  4. 糖儿飞教你学C++ Socket网络编程——5.套接字编程步骤与函数

    TCP是一个面向连接的传输层协议,提供高可靠性的字节流传输服务,主要用于一次传输要交换大量报文的情形.为了维护传输的可靠性,TCP增加了许多开销:例如确认.流量控制.计时器以及连接管理等.TCP协议的 ...

  5. bind()和connect()函数:绑定套接字并建立连接

    socket() 函数用来创建套接字,确定套接字的各种属性,然后服务器端要用 bind() 函数将套接字与特定的 IP 地址和端口绑定起来,只有这样,流经该 IP 地址和端口的数据才能交给套接字处理. ...

  6. Linux环境SOCKET编程1:套接字

    一.socket运行过程 服务器端先初始化Socket,然后与端口绑定(bind),对端口进行监听(listen),调用accept阻塞,等待客户端连接.在这时如果有个客户端初始化一个Socket,然 ...

  7. 套接字创建、连接和关闭函数

    下图是一对 TCP 客户与服务器进程之间发生的一些典型事件的时间表. [img]http://dl2.iteye.com/upload/attachment/0126/4398/db4635bf-09 ...

  8. linux网络编程之一般应用采用的协议和不同套接字的地址结构以及用户进程和内核通过哪些函数传递套接字的地址结构

    1.一般应用采用的协议 2.不同套接字的地址结构 3.用户进程和内核通过哪些函数传递套接字的地址结构 从进程到内核传递套接字的地址结构函数有3个 bind.connect.sendto函数 从内核到进 ...

  9. socket是什么?套接字是什么?

    网络编程就是编写程序使两台联网的计算机相互交换数据. 那么,这两台计算机之间用什么传输数据呢?首先你肯定先需要物理连接嘛. 在此基础上,只需要考虑如何编写数据传输程序.看似很麻烦,但实际上这点不用愁, ...

最新文章

  1. mysql遇到your password does not satisfy the current policy requirements
  2. 如何用cmd命令控制mysql数据库
  3. mysql 分支 XtraDB Percona MariaDB 简介
  4. 结对编程(黄金点游戏)
  5. 信息学奥赛一本通 1135:配对碱基链 | OpenJudge NOI 1.7 07
  6. 1小时搞懂设计模式之单例模式
  7. c语言进度,C语言实现动态进度条
  8. fiddler安装常见问题
  9. CPDA|数据分析师需要具备哪些基本功?
  10. 2022年煤矿安全监测监控特种作业证考试题库模拟考试平台操作
  11. 多图像 并行 浏览 放大 对比 MulimgViewer win10 ubuntu 多图片 多张图片
  12. 制作启动U盘 —— 大白菜U盘启动盘制作工具V5.1
  13. SAP介绍:概念、核心、开发语言、优缺点与集成
  14. 本地系统盘放到服务器上,怎么把本地盘挂到云服务器
  15. 【Unity Shader】聚光灯体积光效果的简单实现
  16. python如何变换环境
  17. 程序员的这 10 个坏习惯!你占了几个?
  18. matlab 悬架,基于MATLAB的汽车悬架仿真研究.doc
  19. 拓嘉启远:拼多多商家怎样处理投诉
  20. module ‘open3d‘ has no attribute ‘xxx‘

热门文章

  1. 本网站的幻灯片浏览很好看,不懂谁有这代码?
  2. 鸿蒙9月11日,鸿蒙2.0版9月11日正式发布,华为将迎来新的突破,具有历史意义!...
  3. thinkphp路由配置 php7.0,thinkphp3.2 路由设置方法
  4. 【PS】WBS结算到建工程问题
  5. php5.2通过saprfc扩展远程连接sap730成功案例
  6. ALV设置参考表和参考字段,自定义列名称可能失效
  7. 9种蔬菜吃不对胜似砒霜
  8. SAP财务报表不平之分析
  9. 谷歌为何要大力投资生命科学初创公司?
  10. SAP中会计凭证和物料凭证的对应关系