Basic usage steps of SocketBasic usage steps of Socket

The basic steps of using Socket

 

 

1. To use Socket, you first need to import these system header files

  #import <sys/socket.h>

  #import <netinet/in.h>

  #import <arpa/inet.h>

2. Then it is to write the Socket code. Let's take a look at the steps required to implement a simple Socket communication:

  >1. Create a client Socket;

  >2. Create a server socket;

  >3. Connect two Sockets;

  >4. The client Socket sends information to the server (the process of sending a request);

  >5. The server responds to the client's request and returns data to the client (the process of the server's response);

  >6. The client accepts the data returned by the server;

  >7. Close Socket;

3. The following is a detailed Socket code implementation.

 

  1. Create a client socket

  //Parameter 1: Protocol field IP protocol type to comply with ! AF_INET:IPv4 AF_INET6:IPv6

  //Parameter 2: Port type : TCP:SOCK_STREAM UDP:SOCK_DGRAM

  //  Parameter 3:  The selected protocol type Generally, passing will automatically select the protocol type according to the second parameter !

  //  Return value If the return value > 0, it indicates that the  Socket  was created successfully !

  int SocketNumber = socket (AF_INET, SOCK_STRESM, 0);

  

  

  2.  Create a server-side  Socket

      structsockaddr_in serverAddress; 

     //  host  / port

      //  Set the  IP  protocol type that the server socket  follows to  IPv4 

      serverAddress. sin_family  =  AF_INET ;

    

    //  The  IP  address of the server  socket 

    serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");

    

    //  Set the port number of the server socket  ;

    //  The maximum value of the port number is  65535; 09~1024  is the port number occupied by the system by default ! Do not set it !

    //  If you need to manually set the port number generally set the value between  1024 ~ 65535  !

    serverAddress.sin_port = htons(3456);

    

    

    3.  Connect two  Sockets

    //  Parameter 1:   Socket of the client

    //  Parameter 2:  Server-side  Socket/ structure  (const struct sockaddr *)&serverAddress  cast type

    //  Parameter 3:  The length of the second parameter ! sizeof(serverAddress)  calculates the length The length of the memory address is calculated !

    //  Return value If the return value is , the connection is successful !

    //  Return value  != 0, which means the connection failed !

    //  If you want to connect successfully , you must monitor the port on the server side in real time !

    //  Monitor server port : nc -lk 12345

    int conn = connect(SocketNumber, (conststructsockaddr *)&serverAddress, sizeof(serverAddress));  

 

        4.  Send information to the server

    //  Parameter 1:  Client  Socket

    //  Parameter 2: void *  Data passed to the server ! msg.UTF8String  is the data type that directly converts  OC  data into  C  language !

    //  Parameter 3: The length of the data passed to the server by the size_t  lock !

    //  Parameter 4:  Pass 0  and wait for the server to respond with data !

    

    NSString *msg = @"hello socket";

    

    send(SocketNumber, msg.UTF8Stringstrlen(msg.UTF8String), 0);

    

     5. Accept the data returned by the server !

    //  Parameter 1: Client  Socket: The server determines which client data to return to .

    //  Parameter 2:void *: The address ( area ) of receiving the data returned by the server

    //  Parameter 3: size_t: the length of the accepted address

    //  Parameter 4: Pass 0  and wait for the server to return data !

    //  Return value is the data length returned by the server !

    

    ssize_t buffer[1024];

    

    ssize_t length = recv(SocketNumber, buffer,sizeof(buffer), 0);

    

    //  Get the data returned by the server ( take the required data from the  buffer  )

    

    //  According to binary data , concatenate strings

    // Bytes:  The data stream ( bit stream / byte ) passed in the network

    NSString *returnMsg = [[NSStringallocinitWithBytes:buffer length:length encoding:NSUTF8StringEncoding]; 

    

    6. After a request ends ( after receiving a response ), you need to manually close the Socket!

    close(SocketNumber);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325526080&siteId=291194637