TCP/IP model and TCP/UDP protocol

Source: Internet
Author: User
Protocol: rules, standards, or conventions established for data exchange (communication) in the network. (= Semantics + syntax + rules) TCP/IP has become an industrial standard for Internet communication. TCP/IP model:



TCP protocolIs a connection-oriented reliability transmission protocol. Before sending data, you must first send a connection request. when the other party listens to a request, the connection is established, and then both parties can send and receive messages, after the message is sent, the two parties are disconnected (namely, the three-way handshake protocol ).


Three-way handshake protocol:

First handshake:

When a connection is established, the client sends a SYN Packet (SYN = J) to the server and enters the syn_sent status, waiting for confirmation from the server; SYN: Synchronize sequence numbers ).
The second handshake:

When the server receives the SYN packet, it must confirm the customer's Syn (ACK = J + 1) and send a SYN Packet (SYN = K), that is, the SYN + ACK packet, the server enters the syn_recv status;
The third handshake:

The client receives the server's SYN + ACK packet and sends the ACK (ACK = k + 1) Confirmation packet to the server. After the packet is sent, the client and server enter the established status and complete three handshakes.


Socket:

To facilitate the development of network application software, Berkeley University launched an application to access the communication protocol's operating system call-socket (socket) on UNIX ). The emergence of socket allows programmers to easily access TCP/IP and develop various network applications. With the popularization of UNIX applications, sockets have been widely used in programming network software. Later, sockets were introduced into windows and other operating systems. The Java language also introduces the socket programming model.


TCP-based socket programming :(Socket and serversocket)

Client programming:① Call socket () to create a stream socket and connect it to the server; ② call getoutputstream () and getinputstream of the socket class to obtain the output stream and input stream, and start sending and receiving network data. ③ Close the communication socket. Server programming:① Call serversocket (INT port) to create a server socket and bind it to the specified port; ② call accept () to listen for connection requests. If the client requests a connection, the connection is accepted, return communication socket. ③ Call the getoutputstream () and getinputstream of the socket class to obtain the output stream and input stream and start sending and receiving network data. ④ Close the communication socket.


UDP is a non-connection-free and unreliable protocol, that is, before data communication, the sender and the receiver do not need to be connected. The sender needs to send data and only need to construct a datagram, add the acceptor address to the datagram, and then send it out. this protocol is an unreliable protocol. Because there is no connection between the sending and receiving devices, the data sent out may be lost midway through, and the sending end has not received any feedback. therefore, the most important thing in implementing UDP protocol programs is how to Implement UDP datagram
Java provides two key classes for implementing the UDP protocol. Datagram (Datagrampacket ):Construct a datagram that can contain the data to be sent and the acceptor address. UDP (Datagramsocket ):Build the sender and receiver that implements the UDP function to send and receive data packets.
UDP network programming method: Write the receiver program:① Call mongoramsocket (INT port) to create a datagram socket and bind it to the specified port; ② call mongorampacket (byte [] Buf, int length ), create a byte array to receive UDP packets. ③ Call receive () of the datagramsocket class to receive UDP packets. ④ Close the datagram socket. Write the sender program:① Call mongoramsocket () to create a datagram socket; ② call mongorampacket (byte [] Buf, int offset, int length, inetaddress address, int port) to create a UDP packet to be sent. ③ Call the send () method of the datagramsocket class to send a UDP packet. ④ Close the datagram socket.

TCP/IP model and TCP/UDP protocol

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.