Socket, what is the east, interview, the written test has him, interview also has him, if do smart hardware, will definitely use the socket, so, today take you to review the socket;
First, Introduction: Socket: Is the intermediate Software abstraction layer that the layer communicates with the TCP/IP protocol family, which is a set of interfaces. In the design mode, the socket is actually a façade mode, which hides the complex TCP/IP protocol family behind the socket, for example:
- You want to call a friend, dial first, a friend hears a phone call, and then you and your friend set up a connection, you can talk. When the communication is over, hang up the phone and end the conversation.
To establish a connection step: Start with the server side.
1> server-side first initialize the Socket , then with the end ? binding (BIND) , -to-end ? into ? Monitor (Listen) , Tune with Accept blocking, waiting for the client to connect.
2> at this point if there is a client initialization ? a Socket , then connect to the server (connect);
3> If the connection is successful, then the client-server connection is built . up. The client sends A data request, the server receives the request and processes the request, and then the connection succeeds, and the client-server connection is built. The client sends a data request, the server receives the request and processes the request, then arc=send (fd,sztext,cnt,0), sends the response data to the client, the client reads the data, and finally closes the connection, and the second interaction ends. In short, the client loses the server-side IP address and sends the data, then presses the Send button, the server receives the data, and then responds to the client. The client reads the data of the response, which is displayed on the interface. On the server side, the primary is to start the socket and the listener thread. Server side? has been listening for a client connection, if there is a connection, processing the client's request, giving a response, and then continue listening.
Client: Apple ISO development: Socket TCP/IP communication, using the following methods:
- Create a project.
- Add the Asyncsocket to the project.
- Add cfnetwork.framework to the project;
- To implement a test class:
#import <UIKit/UIKit.h>
#import "AsyncSocket.h"
@interfaceiphone_socketViewController: Uiviewcontroller {
Asyncsocket *asyncsocket; }
@end
second, the application: IOS socket based on TCP/UDP of Communication
iOS Communication socket apple
already have a well-written open source class library on the networkGcdasyncsocketand theGcdasyncudpsocket,this isGCDversion of thanAsyncsocketand theAsyncudpsocketIt's supposed to be easy.the usage is also very simple, withhttpvery similar ,as long as you specify the serverIPand Portand then implement a variety of callbacks on the line, the original ecological implementation is groping ..... SocketBy default, it is the use ofTCPprotocol, created after the communication between the twoSocketwill remain connected, unless manuallyCloseor because of network reasonsClose,as a result, this is the case for servers andresources, this model only adapts to small-scale access to servers, especially for applications with high real-time, such as live video, call Systems , andhttpare generally short-connected, onceafter the request is completed, the client will open the connection on the service side. httpis to overrideSocketabove the high-level protocol, andSocketis a relatively low-level communication, just set up a connection channel, the specific transmission of what kind of data, according to what format to transfer, you need to define yourself, so this needs to rewrite the definition of the server and the client should follow the rules, andhttphave been used by previous definitions.
go to GitHub first.Web site to download the latest package, and then look at the introduction first. Write a more detailed click Download Socketdemo from Gitut,Many of the online posts are old versions. The authorities have introduced Gcdasyncsocket to replace the old Asyncsocket .。my project is No-arc., this frame has only the arc version. So after introducing the gcdasyncsocket. h and. m files, modify the properties of the project in Xcode,
The steps are as follows:
- Find the compiler for C/C++/OBJECTIVE-C option in "Build Settings" in targets . Change to Apple LLVM compiler 3 only if 3.0 or above is available;
- Find GCDASYNCSOCKET.M in " Build Phases " in "Compile sources", add parameter-fobj-arc;
- The framework required for the introduction of Gcdasyncsocket , the cfnetwork and the security framework;
Third, the difference between communication http,tcp/ip and sockets:
1. TCP/IP connection
mobile phones are able to use networking because the bottom of the phone implements TCP/IP protocol that allows a mobile terminal to establish a TCP connection over a wireless network. TCP The protocol can provide an interface to the upper network, so that the transmission of the upper network data is established " No difference " Network . Setting up a TCP connection requires a "three-time handshake":
- First handshake: The client sends a SYN packet (SYN=J) to the server and enters the Syn_send state, waiting for the server to confirm;
- Second handshake: The server receives the SYN packet, it must confirm the customer's SYN (ACK=J+1), and also send itself a SYN packet (syn=k), that is, the Syn+ack packet, when the server enters the SYN_RECV state;
- Third handshake: The client receives the server's Syn+ack packet, sends the acknowledgment packet ack (ACK=K+1) to the server, the packet is sent, the client and the server enter the established state, and the handshake is completed three times.
The data is not included in the packets that are delivered during the handshake, and the client and server formally begin transmitting the data after the three handshake is complete . under ideal conditions, TCP Once the connection is established, either side of the communication is actively shutting down the connection before TCP the connection will be maintained forever. When disconnected, both the server and the client can initiate a request to disconnect the TCP connection, and the disconnection process requires a " four handshake "(the process is not fine-written, is the server and client interaction, which ultimately determines the disconnection).
2. HTTP connection
HTTP protocol is Hypertext Transfer Protocol (hypertext Transfer Protocol), which is the foundation of Web networking and one of the most commonly used protocols for mobile networking, is an application built on the TCP protocol.
HTTP The most significant feature of the connection is that each request sent by the client requires a server loopback response, and after the request is completed, the connection is actively released . the process from establishing a connection to closing a connection is called a "one-time connection."
3.1 Socket (socket) concept
Sockets ( Socket ) is the cornerstone of communication and is to support TCP/IP The basic operating unit of the Protocol's network communication. It is an abstract representation of the endpoint in the network communication process, including the five kinds of information necessary for network communication: The protocol used by the connection, the IP address of the local host, the protocol port of the local process, the IP of the remote host address, the protocol port of the remote process.
3.2 Establishing a socket connection
Establish Socket a connection requires at least one pair of sockets, one of which runs on the client, called Clientsocket , another running on the server side, called ServerSocket .
The connection between sockets is divided into three steps: Server listening, client request, connection acknowledgement.
Server monitoring: Server-side sockets do not locate specific client sockets, but are waiting for the status of the connection, real-time monitoring network status, waiting for the client connection request.
Client request: refers to the client's socket to make a connection request, to connect to the target is the server-side socket. To do this, the client's socket must first describe the socket of the server it is connecting to, indicate the address and port number of the server-side socket, and then make a connection request to the server-side socket.
Connection confirmation: When a server-side socket hears or receives a connection request from a client socket, it responds to a client socket request, establishes a new thread, sends a description of the server-side socket to the client, and once the client confirms the description, the two sides formally establish the connection. While the server-side socket continues to be in the listening state, it continues to receive connection requests from other client sockets .
4. Socket connection and TCP/IP connection
Create Socket when connecting, you can specify the Transport layer protocol to use, Socket can support different transport layer protocols ( TCP or UDP ), when using TCP when the protocol is connected, the Socket connection is a TCP connection .
? 5, socket connection and HTTP connection
In many cases, the server side is required to proactively push data to the client, keeping the client and server data in real time and in sync. At this point, if the two sides established a Socket Connection, the server can directly transfer data to the client, if the two sides established an HTTP Connection, The server needs to wait until the client sends a request to send the data back to the client, so the client periodically sends a connection request to the server, not only to remain online, but also to " ask " Whether the server has new data, and if so, transmits the data to the client.
6, the HTTP protocol is the application layer of the association:
One is the engine (Socket), provides the ability of network communication, one is sedan (Http), provides the specific way , the communication between two computers is nothing more than the data communication between two ports, the specific data will show in what form ' is a different application layer protocol to define the ' like http ' FTP ' ...?? The socket is a tool for developing port communication, and it needs to be lower.
7, network request mode:Get and post this is the HTTP protocol two methods, plus head, delete, and so the two methods have an essential difference, get only one stream, parameters appended to the URL, the size of the number has a strict limit and can only be a string. Post parameters are passed through another stream, not through the URL, so can be very large, can also pass binary data, such as file upload. In servlet development,
Supplemental Description: TCP/IP , Http , Socket the Difference
through the preliminary understanding, the IP protocol corresponds to the network layer, the TCP protocol corresponds to the transport layer, and the HTTP protocol corresponds to the application layer, and we usually say the most socket is what, in fact, the socket is the TCP/IP protocol encapsulation, The socket itself is not a protocol, but rather a calling interface (API). With the socket, we can use the TCP/IP protocol. In fact, the socket is not necessarily associated with the TCP/IP protocol. The socket programming interface is designed to adapt to other network protocols as well. So, the advent of sockets just makes it easier for programmers to use the TCP/IP protocol stack, which is an abstraction of the TCP/IP protocol, thus forming some of the most basic function interfaces we know, such as Create, listen, connect, accept, send, Read and write, and so on. The network has a paragraph about the socket and TCP/IP protocol relationship is easier to understand: "TCP/IP is a protocol stack, like operating system operation mechanism, must be specific, but also to provide external operating interface." In fact, the transport layer of TCP is based on the network layer of IP protocol, and the application layer of the HTTP protocol is based on the transport layer of the TCP protocol, and the socket itself is not a protocol, as mentioned above, it just provides a connection between TCP or sockets process is divided into three steps: Server monitoring, Client request, Connection acknowledgement.
- Server monitoring: Server-side sockets do not locate specific client sockets, but are waiting for the status of the connection, real-time monitoring network status, waiting for the client connection request.
- Client request: Refers to the client's socket to make a connection request, to connect to the target is the server-side socket. To do this, the client's socket must first describe the socket of the server it is connecting to, indicate the address and port number of the server-side socket, and then make a connection request to the server-side socket.
- Connection confirmation: When a server-side socket hears or receives a connection request from a client socket, it responds to a client socket request, establishes a new thread, sends a description of the server-side socket to the client, and once the client confirms the description, the two sides formally establish the connection.
While the server-side socket continues to be in the listening state, it continues to receive connection requests from other client sockets.
Third, the characteristics of HTTP links
The HTTP protocol is the Hypertext Transfer Protocol (Hypertexttransfer Protocol), which is web-linked
Four, the difference betweenTCP and UDP (the most test). It's getting rotten. I think--\\)
1, TCP is a link-oriented, although the security of the network instability characteristics determine how many times the handshake can not guarantee the reliability of the connection, but the TCP three handshake at a minimum ( in fact, to a large extent guaranteed) to ensure the reliability of the connection;
And UDP is not connection-oriented, UDP transmission data before the connection with the other, the docking received data does not send a confirmation signal, the sender does not know whether the data will be properly received, of course, there is no need to resend, so that UDP is a non-connected, unreliable data transmission protocol .
2, also due to 1 of the characteristics of the said, so that the cost of UDP smaller data transmission rate is higher, because there is no need to send and receive data confirmation, so UDP real-time better. know the difference between TCP and UDP, it is not difficult to understand why the use of TCP transmission protocol MSN than UDP transmission file slow, but can not say that QQ communication is not safe, because the programmer can manually verify the data sent and received by UDP, For example, the sender of each packet number and then by the receiver to verify AH what, even so, UDP because the package on the underlying protocol does not adopt a TCP-like "three-time handshake" to achieve the TCP can not achieve the transmission efficiency.
if reproduced please specify the transfer to: Airzilong's Blog
Introduction to IOS sockets and its simple application