IOS: Instant Messaging < Learn Socket>

Source: Internet
Author: User
Tags file transfer protocol

What is a socket?

The terminology of computer science is:

The two programs on the network realize the exchange of data through a two-way communication connection, one end of this connection is called a socket. The English literal of the socket is "hole" or "socket". As the BSD UNIX process communication mechanism, take the latter one meaning. Often also referred to as a "socket," which describes IP addresses and ports, is a handle to a communication chain that can be used to communicate between different virtual machines or different computers. Hosts on the internet typically run multiple service software, while providing several services. Each service opens a socket and binds to a port, and the different ports correspond to different services. The socket is like a porous socket, as its English intended. A host is like a room full of various sockets, each outlet has a number, some sockets provide 220 vac, some provide 110 volts AC, some provide cable TV programs. Customer software plug into different numbered sockets, you can get different services

General saying:

Socket called "socket"

The network of two programs through a two-way communication connection to achieve the exchange of data, the end of this connection is called a socket , usually we send a network request to the server, and then the server responds to the request, the return server data application usually through the "socket" Make a request to the network or answer a network request


Details:

1.Socket is equivalent to a "pipe" or "tunnel" on either side of the connection, one enters, one comes out

2. User name and password are stored in the server's database

3. Do we want to ensure that the client program accesses the Web application or the database application? is through the port number

Network communication elements:

1. The request on the network is to establish a connection through the socket and then communicate with each other

2.IP Address (The unique identity of the host device on the network) (the host looking for the server)

3. Port number (Locator)

4. A logical address for marking a process (a program is a process), marking different processes

5. Valid port: 0~65535, where 0~1024 is used by the system or reserved port, it is recommended to use more than 1024 ports in development

6. Transport protocol (how to interact) (the way TCP and UDP need or do not need to establish a connection.) Just like Express, like Shun Fung or tact is a way)

7. Rules of Communication (Common protocol: TCP, UDP)

Tcp:
Three-time handshake for data security and reliability such as calling:

First handshake: dial the past first

second handshake: the other side "feed" a sound, respond to a bit,

third handshake: after I heard each other, I also said "Hello" and responded.

Only these three steps, someone feeds, you also feed, it can hear my response, I can hear its response is successful. If you borrow money, it will ensure safety and reliability.

1. Must establish a connection, the efficiency will be slightly lower (every time to establish a connection)

2. How many HTTP requests, how many times three handshake (HTTP is inefficient, but the impact is not very small, because the next time the user Send network request is not fixed, so there is no significant impact) (call if you keep, the telephone fee is not much?)

3.HTTP is to make a TCP connection, three handshake per request

Udp:

The data and the source and the destination are encapsulated into the packet, do not need to establish a connection (just like the teacher (source) screen broadcast, the timely synchronization of data sent to your computer (purpose), directly sent. If the network is broken, see the screen again, you can only see the latest content. No, I lost it, missed it.

The size of each datagram is limited to 64KB (first is real-time, if the data is bigger, if there is a problem, there is not much damage)

Because there is no need to connect, it is an unreliable protocol (no more than three handshake, high performance)

No need to establish connection, fast speed

Application:
Socket Communication Flowchart:


1.bind () is the bound port (The server should have ports to identify itself)

2.listen () is the listening port to see if there is Lianjie come over, I agree to accept the connection (if the client wants to connect to the Web application port, listen to port 80.) If it is a malicious attack, multiple concurrent requests, will cause network congestion, then I can disconnect your connection)

3.accept ()

4.write () client sends a request

5.read () service-side Accept Request


If the client sends a login request, my server is to make the request, the server to do the response data, send data to the client, do a write () the operation of an HTTP request is finished, then there is an operation, end the connection. Because the time of the next request is not fixed. Just do a close () operation

HTTP to the bottom of the socket to establish a connection to the communication pipeline, to achieve data transmission

HTTP is a TCP Transport protocol (mode), it is a reliable and secure protocol

To implement the socket service-side monitoring method:

1: The implementation of the socket listening method is implemented using the C language (the system has the default, but the trouble)

2: Using the Cocoaasyncsocket third box (is OC), the internal is the C package, time-consuming operation is certainly time-consuming, development, we also use this framework

3: Telnet command telnet host port/telnet 192.168.10.10 5288 telnet command is a service that corresponds to a port on the connection server

Protocol on the socket Layer:

The protocol on the 1.Socket layer refers to the format of the data transfer:


2:http protocol (similar to a dictionary protocol)


3:XMPP Protocol ( is an Instant Messaging protocol (for the purpose of defining the format of data transfer)


4: Custom Instant Messenger protocol, JSON format


Add: The difference between TCP, UDP, HTTP, socket

IP: Network layer protocol;

TCP and UDP: Transport layer protocol;

HTTP: Application layer protocol;

SOCKET:TCP/IP the network's API.

TCP/IP stands for Transmission Control Protocol/Internet Protocol, which refers to a series of protocols.

TCP and UDP use the IP protocol to transfer packets from one network to another network. Think of IP as a highway that allows other protocols to travel and find the exits of other computers. TCP and UDP are "trucks" on highways, and the goods they carry are protocols such as HTTP, File Transfer Protocol FTP, and so on.

TCP and UDP are transport layer protocols that are used, such as Ftp,http and SMTP. Although both TCP and UDP are used to transmit other protocols, they have a significant difference: TCP provides guaranteed data transfer, and UDP does not. This means that TCP has a special mechanism to ensure that data is safe from one endpoint to another without error, and UDP does not provide any such assurance.

HTTP (Hypertext Transfer Protocol) is a protocol that leverages TCP to transfer information between two computers, typically Web servers and clients. The client initiates an HTTP request to the Web server using a Web browser, and the Web server sends the requested information to the client.

Remember, the IP protocol is required to connect to the network; TCP is a mechanism that allows us to securely transmit data, and HTTP, which uses the TCP protocol to transmit data, is a special protocol used by Web servers and clients.

The Socket interface is a TCP/IP network Api,socket interface that defines many functions or routines for developing applications on TCP/IP networks.



Text/Programmer in Fog (Jane book author)
Original link: http://www.jianshu.com/p/3167802eb1e5
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

IOS: Instant Messaging < Learn Socket>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.