IOS bottom Socket programming understanding

Preface

Socket network programming is very important in any programming language, but the underlying socket is a pure C language, cross platform, familiar with the underlying interaction is an important step to improve their level of programming. This ring sum up slightly, if the children can use to interview on the end of a case of demo (IOS).

text

  • First clear Socket in the network model where: is the bridge between the application layer and the transport layer
    IOS bottom Socket programming understanding
    image
  • Review the network model: OSI seven layer network model:
    1 application layer.2. layer.3. layer.4. session transport layer.5..6. network layer data link layer physical layer of.7.
    TCP/IP four layer network model: application layer transport layer network layer. The network access layer.
  • HTTP is an application layer protocol: object oriented protocol (Hypertext Transfer Protocol), based on the TCP connection,
    1 features: support the client / server mode
    2 simple flexible
    every 3 client requests that are sent to the server to send in response to requests, after the end of active release commonly known as “short connection connection”
  • TCP protocol: transmission control protocol, providing connection oriented, reliable byte stream service, providing timeout retransmission, discarding duplicate data, testing data, traffic control and other functions. In the official data transceiver, must establish a reliable connection, namely: three. The first handshake Handshake: the client sends a syn packet to the server (syn=j), and in SYN_SEND state, waiting for the server to confirm;
    second handshake: syn packets received from the server, the client must confirm that SYN (ack=j+1), and at the same time sending a SYN packet (syn=k), namely SYN+ACK packets, the server into the SYN_RECV state;
    third Handshake: client server received the SYNACK packet, ACK confirmation packet sent to the server (ack=k+1), the packet is sent, the client and server into the ESTABLISHED state, complete the three handshake.
  • UDP protocol: user datagram protocol, non connection oriented, does not guarantee the reliability of data transmission service, no retransmission mechanism, so the transmission speed is very fast. Features: it does not establish a connection with each other, but directly sends packets in the past, UDP is suitable for the application environment of a transfer of only a small amount of data reliability requirements is not high.
  • Socket: also known as the “socket”, the application through the socket to the network to send a request or response, it is a TCP and UDP programming interface, with which to establish a TCP/UDP connection. Socket connection is called a long connection, in theory, the client and the server once the connection will not take the initiative to break up
  • HTTP protocol Socket –TCP connection connection: 1.HTTP protocol provides a package or display specific data;
    2.Socket provides the communication ability of the network connection;
    3.TCP connection provides how to transmit in the network;
    4.socket is a pure C language, cross platform;
    5.HTTP protocol is based on socket, the bottom is the use of socket;
    6 to create a Socket connection, you can specify the transport protocol used (TCP or UDP), when using TCP protocol connection, the Socket connection is a TCP connection.
  • TCP and UDP based on the difference between the 1 and 2 connected to the system resources required to connect
    (TCP more, less UDP
    3.UDP) program structure is relatively simple
    4.TCP flow model, UDP datagram mode
    5 reliability: TCP to ensure the correctness of data, UDP can not guarantee the accuracy of the data packet.
  • Socket
    IOS bottom Socket programming understanding
    image communication diagram to
    socket client programming example:
    0 #import &lt arpa/inet.h&gt into the header file; #import; < netinet/in.h> #import < sys/socket.h> socket @implementation ViewController 1 to create {int _clientSocket;} / * 1.AF_INET: IPv4 implementation of the IP protocol version 2.SOCK_STREAM: Specifies the Socket types for streaming socket the transport layer connection protocol: 3.IPPROTO_TCP protocol specified. Transmission mode of IPPROTO_TCP TCP transmission protocol, the return value is greater than 0 / _clientSocket successfully created = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); 2 to establish a connection (and server) / inside the terminal server netcat NC simulation command -lk 12345 parameters: a socket descriptor parameter two: a pointer to the data structure of SOCKADDR, including the destination port and IP address parameters three parameters: the length of two SOCKADDR, through sizeof (struct SOCKADDR) to obtain a return value of int -1 / struct sockaddr_in addr 0 failed success; / * * / fill sockaddr_in structure addr.sin_family = AF_INET; addr.sin_port=htons = inet_addr; (12345) addr.sin_addr.s_addr (“127.0.0.1”); int connectResult= (_clientSocket, connect (const struct SOCKADDR) & addr, sizeof (addr)); 3 (sending data to the server) / * 1 A parameter specifies the sender socket descriptor; second parameter specifies a buffer to store application data transmission; the number of characters of third parameters indicate the actual data to be transmitted; fourth parameters generally set to 0. The success of the actual returns the number of characters transmitted, failed to return – 1, char * * STR = “itcast”; ssize_t sendLen = send (_clientSocket, STR, strlen (STR), 4 (0); data transfers from the server) / the first parameter socket second parameters storage buffer length third parameter buffer data. The fourth parameter specifies the call, the 0 general return value successfully received the number of characters * / char *buf[1024]; ssize_t recvLen = recv (_clientSocket, buf, sizeof (buf), NSLog (0); @ —->%ld, recvLen); close (_clientSocket) 5 close connection;

6.demo packaging method:

/ / connect - (void) connectToServer: (NSString *) IP port: (int port) {_clientSocket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); struct sockaddr_in addr; / * * / addr.sin_family fill in the structure of sockaddr_in = AF_INET; addr.sin_port = htons; addr.sin_addr.s_addr = (port) inet_addr (ip.UTF8String); int (_clientSocket = connect, connectResult (const * struct SOCKADDR & addr, sizeof); (addr)); if (connectResult = = 0) {NSLog (@ "conn OK");}} / / send data and return data for - (NSString *) sentAndRecv: (NSString * MSG) {const char *str = msg.UTF8String; / / ssize_t sendLen message = send (_clientSocket, STR, strlen (STR), 0); / / char *buf[1024] ssize_t received the message; recvLen = recv (_clientSocket, buf, Sizeof (buf), 0 alloc]; NSString = [[NSString initWithBytes:buf length:recvLen encoding:NSUTF8StringEncoding]; return recvStr;}

Case effect diagram:

IOS bottom Socket programming understanding
image

: a case of multiple threads to achieve interactive client and server simple, my demo address:
server: https://github.com/zhonghphuan/ServerSocket.git
client: https://github.com/zhonghphuan/ClientSocket.git

Case two: the use of Socket to send HTTP format request and browser monitoring:
https://github.com/FieldsOfHope/Socket_Interactive.git

If you have other questions, please contact me: [email protected]