TCP and UDP

Source: Internet
Author: User
Tags file transfer protocol

My most annoying way of sharing isCodeThe title is indeed full at the beginning, but the content of the result is poor, and the goal of summing up the memory cannot be achieved.

1. What is TCP and UDP?
TCP (Transmission Control Protocol) and UDP (User datasync protocol) Protocols belong to the transport layer protocol. Among them, TCP provides reliable data transmission in the IP environment,
Its services include data stream transmission, reliability, effective traffic control, full-duplex operations, and multiplexing. Send data packets through connection-oriented, end-to-end, and reliable data packets. In layman's terms,
It first opens a connection channel for the sent data and then sends the data. UDP does not provide a Network File System for the IP address), SNMP (Simple Network Management Protocol)
, DNS (Primary Domain Name System), TFTP (Common File Transfer Protocol), etc. Both protocols are transport layer protocols that provide information carriers for the application layer. The TCP protocol is connection-based
By protocol, there are traffic control and error control, and because of the Reliability guarantee and control means, the transmission efficiency is lower than that of UDP. UDP is based on the non-connection unreliable protocol,
Without control means, data is only sent to the other party, so the efficiency is higher than that of TCP.

2. Applicable scenarios of TCP and UDP

Based on the features described in 1, it is difficult to draw a conclusion that the TCP protocol is applicable to scenarios with relatively low efficiency requirements but relatively high accuracy requirements, or scenarios with a connection concept;
The UDP Protocol is applicable to scenarios with relatively high efficiency requirements and low accuracy requirements.

TCP is generally used for file transmission (ftp http requires high data accuracy, and the speed can be relatively slow), sending or receiving emails (pop imap smtp requires high data accuracy, non-urgent applications ),
Remote Logon (Telnet SSH has certain requirements on data accuracy and connection concepts). UDP is generally used for Instant Messaging (QQ chat has low requirements on data accuracy and packet loss, but the speed must be fast ),
Online Video (the RTSP speed must be fast to ensure video continuity, but sometimes an image frame is spent, which is acceptable), network voice calls (VOIP voice packets are generally relatively small,
High-speed transmission is required, and occasional disconnection or crosstalk is not a problem.

3. TCP and UDP instances
3.1. TCP instance

Tcp clientProgram:

Package COM. boonya. TCP. client; import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstream; import Java. io. inputstreamreader; import java.net. socket; public class tcpclient {public static final string IP = "192.168.0.157"; public static final int Port = 8962; private socket s; Public tcpclient () throws ioexception {try {S = new socket (IP, Port);} catch (ioexception e) {e. printstacktrace () ;}} public void setconnection () throws ioexception {inputstream is; try {is = S. getinputstream (); bufferedreader BR = new bufferedreader (New inputstreamreader (is); system. out. println (BR. readline ();} catch (ioexception e) {e. printstacktrace () ;}} public static void main (string ARGs []) throws ioexception {tcpclient MC = new tcpclient (); MC. setconnection ();}}

TCP server program:

  package COM. boonya. TCP. server; import Java. io. ioexception; import Java. io. outputstream; import java.net. serversocket; import java.net. socketpublic class tcpserver {// create a serversocket and set its port number to private serversocket SS; public static final int Port = 8962; Public tcpserver () {try {Ss = new serversocket (port);} catch (ioexception e) {e. printstacktrace () ;}} public void setconnection () throws ioexception {// create a Socket socket s on the server side; outputstream OS; try {// serversocke. accept () t returns a socket object S = ss. accept (); OS = S. getoutputstream (); OS. write ("hello ". getbytes (); OS. close (); S. close ();} catch (ioexception e) {e. printstacktrace () ;}} public static void main (string [] ARGs) throws ioexception {tcpserver MS = new tcpserver (); Ms. setconnection () ;} 

3.2. UDP instance
UDP server program:

 
Package COM. boonya. UDP. server; import Java. io. ioexception; import java.net. datagrampacket; import java.net. datagramsocket; public class udpserver {public static void main (string [] rrgs) throws ioexception {datagramsocket Socket socket = new datagramsocket (14567); While (true) {byte data [] = new byte [1024]; // create an empty mongorampacket object mongorampacket packet = new mongorampacket (data, data. length); // use the receive method to receive data sent by the client. // if the client does not send data, the process will be stuck in the socket. receive (packet); string result = new string (packet. getdata (), packet. getoffset (), packet. getlength (); system. out. println ("result --->" + result );}}}

UDP client program:

Package COM. boonya. UDP. client; import java.net. datagrampacket; import java.net. datagramsocket; import java.net. inetaddress; public class udpclient {public static void main (string [] ARGs) {try {// first create a initramsocket object initramsocket socket = new initramsocket (); // create an inetaddree inetaddress serveraddress = inetaddress. getbyname ("192.168.0.157"); string STR = "hello"; // This is the data to be transmitted byte data [] = Str. getbytes (); // splits the transmitted content into bytes. // creates a datagrampacket object and specifies the address to which the packet is sent to the network, and the port number datagrampacket packet = new datagrampacket (data, data. length, serveraddress, 14567); // call the send method of the socket object to send data socket. send (packet);} catch (exception e) {// todo auto-generated Catch Block E. printstacktrace ();}}}


 

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.