Linux C function to establish a connection network _

When using a socket function to create a socket and bind the address, you can use the connect function to establish a connection and the server.

#include <sys/types.h>
#include <sys/socket.h>
int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen);

Wherein the parameter is a sockfd socket to create a socket descriptor function returns the socket, addr parameter specifies the socket address of the remote server, including the IP address and port number of the server; addrlen parameter specifies the length of the socket address; after a successful call returns 0, otherwise -1.

Before establishing a connection call connect function, the client application code needs to specify the server process has a socket address,
the client usually does not specify their own socket address, Linux automatically from among a range of ports 1024 to 5000 an unused port number assigned to the client, and then combined with the port number and IP address of the machine into the socket with the address.

When a client calls the function connet to take the initiative to establish a connection, this function will start the 3-way handshake process TCP protocol, after a connection is established or when an error occurs, the function returns. Connection process may be error conditions are the following:

  • If the client does not receive the TCP protocol to its acknowledgment SYN segment, the function returns with an error, the error type is ETIMEOUT. Typically, TCP protocol after sending SYN segment fails, it sends multiple SYN segment, after all are the sending failed, the function returns with an error.
  • If the remote TCP protocol return a RST segment, the function returns immediately with an error, the error type is ECONNREFUSED. When the remote machine specified in the SYN segment destination port number of the server process when there is no waiting for a connection, the remote machine's TCP protocol will send a RST segment, the client reports to this error. TCP protocol client after receiving the data segment RST, SYN segment no longer be sent, the function returns immediately with an error.
  • If the client SYN segment results in generation of a router "Destination Unreachable" ICMP message type, then the function returns with an error, or the error type EHOSTUNREACH ENETUNREACH. Typically, TCP protocol after receiving this ICMP message, recording the message, and then sends a SYN segment continues several times, after all have failed transmission, TCP protocol check this ICMP message, the function returns with an error.

If you call the function connect fails, it should close the socket descriptor with the function close, can not re-use the socket descriptor to call the function connect.

[Example 3] function to establish a connection using the connect
application code using the PORT and REMOTE_IP to define a port number and an IP address, and then were calling socket and bind function to create a socket and bind the socket, and finally use the function to connect connecting argv [1] parameter specified IP address.

Examples of the application code as follows:

#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fentl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#define PORT 80  //定义一个端口号
#define REMOTE_IP "59.175.132.70"  //定义一个IP地址

int main(int argc, char *argv[])
{
    int sockfd;
    struct sockaddr_in addr;  //定义 IPV4 套接字地址数据结构 addr
    if(argc != 2)
    {
        printf("请输入正确的IP地址字符串。\n");
        return 2;
    }
    if(sockd = socket(A_INET, SOCK_STRAMOY, 0))<0) //建立一个 socket
    {
        printf("创建套接字失败!\n");
        return 1;
    }
    bzero(&addr,sizeof(struct sockaddr_in));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(argv[1]);  //从argv[1]中获得目标的IP地址
    if(connect(sockfd, (struct sockaddr *)(&addr), sizeof(struct sckaddr))<0)
    {
        printf("连接失败!\n");
        return;
    }
    else
    {
        printf("连接成功!\n");
    }
    return 0;
}

Published 70 original articles · won praise 131 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_43239560/article/details/103130452