linux网络编程之connect函数

NAME

    connect - connect a socket

SYNOPSIS

    #include <sys/socket.h>

    int connect(int socket, const struct sockaddr *address, socklen_t address_len);


DESCRIPTION

    The connect() function shall attempt to make a connection on a socket. The function takes the following arguments:

    socket
        Specifies the file descriptor associated with the socket.
    address
        Points to a sockaddr structure containing the peer address. The length and format of the address depend on the address family of the socket.
    address_len
        Specifies the length of the sockaddr structure pointed to by the address argument.


    If the socket has not already been bound to a local address, connect() shall bind it to an address which, unless the socket's address family is AF_UNIX, is an unused local address.

    If the initiating socket is not connection-mode, then connect() shall set the socket's peer address, and no connection is made. For SOCK_DGRAM sockets, the peer address identifies where all datagrams are sent on subsequent send() functions, and limits the remote sender for subsequent recv() functions. If address is a null address for the protocol, the socket's peer address shall be reset.

    If the initiating socket is connection-mode, then connect() shall attempt to establish a connection to the address specified by the address argument. If the connection cannot be established immediately and O_NONBLOCK is not set for the file descriptor for the socket, connect() shall block for up to an unspecified timeout interval until the connection is established. If the timeout interval expires before the connection is established, connect() shall fail and the connection attempt shall be aborted. If connect() is interrupted by a signal that is caught while blocked waiting to establish a connection, connect() shall fail and set errno to [EINTR], but the connection request shall not be aborted, and the connection shall be established asynchronously.

   

    The socket in use may require the process to have appropriate privileges to use the connect() function.

RETURN VALUE

    Upon successful completion, connect() shall return 0; otherwise, -1 shall be returned and errno set to indicate the error.






name
connect - 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);


Description
The 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 .

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 specified by 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.
Return Value
If the connection or binding succeeds, zero is returned. On error, -1 is returned, and errno is set appropriately.





Making a Connection

In making a connection, the client makes a connection while the server waits for and accepts the connection.
Here we discuss what the client program must do with the connect function, which is declared in sys/socket.h.
— Function: int connect (int socket, struct sockaddr *addr, socklen_t length)

    The connect function initiates a connection from the socket with file descriptor socket to the socket whose address is specified by the addr and length arguments. (This socket is typically on another machine, and it must be already set up as a server.)  

    Normally, connect waits until the server responds to the request before it returns. You can set nonblocking mode on the socket socket to make connect return immediately without waiting for the response.  

    The normal return value from connect is 0. If an error occurs, connect returns -1. The following errno error conditions are defined for this function:

    EBADF
        The socket socket is not a valid file descriptor.
    ENOTSOCK
        File descriptor socket is not a socket.
    EADDRNOTAVAIL
        The specified address is not available on the remote machine.
    EAFNOSUPPORT
        The namespace of the addr is not supported by this socket.
    EISCONN
        The socket socket is already connected.
    ETIMEDOUT
        The attempt to establish the connection timed out.
    ECONNREFUSED
        The server has actively refused to establish the connection.
    ENETUNREACH
        The network of the given addr isn't reachable from this host.
    EADDRINUSE
        The socket address of the given addr is already in use.
    EINPROGRESS
        The socket socket is non-blocking and the connection could not be established immediately. You can determine when the connection is completely established with select;  Another connect call on the same socket, before the connection is completely established, will fail with EALREADY.
    EALREADY
        The socket socket is non-blocking and already has a pending connection in progress  .

    This function is defined as a cancellation point in multi-threaded programs, so one has to be prepared for this and make sure that allocated resources (like memory, files descriptors, semaphores or whatever) are freed even if the thread is canceled.
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值