C语言socket connect()函数(初始化套接字上的连接)(未完)(如何测试socket是否已经断开,如何判断socket是否断开)
生活随笔
收集整理的這篇文章主要介紹了
C语言socket connect()函数(初始化套接字上的连接)(未完)(如何测试socket是否已经断开,如何判断socket是否断开)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
參考文章: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是否断开)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是posix?(Portable O
- 下一篇: linux ubuntu pkg-con