Dark Horse programmer-Java Network Programming

Source: Internet
Author: User

-------------------- Android training, Java training, and hope to communicate with you! ----------------------
I learned Java Network Programming today. I think it is not difficult, but it is mainly about the IO stream. Java Network Programming is actually socket programming and communication through two protocols, one is UDP and the other is TCP.
First introduce UDP
UDP encapsulates data and source and target data into data packets and sends them in the form of delivery. No connection is required. The size of each data packet is limited to 64 KB. Because there is no connection, It is not reliable, but the speed is fast. It can be understood that when we use UDP for data transmission, it is like sending a mail. We do not need to know whether the recipient of the mail actually exists. We only need to use a packet to ipvrampacket, in this packet, You need to specify the address of the recipient, that is, the IP address, and the recipient's name, that is, the port number, and make sure that you have the mail service mongoramsocket, then let it take the package away. below isCode

 
Datagramsocket DS = new datagramsocket (); byte [] DATA = "this is the data to be sent ". getbytes (); datagrampacket dp = new datagrampacket (data, data. length, inetaddress. getbyname ("192.168.0.6"), 10086); DS. send (DP); DS. close ();

In this way, it is sent out, and the receiving end only needs to receive the package and open it to see what it is. It also needs to have the mail service and set the only person to receive the package, listen to the specific port number, and prepare a package to install the package you received. The following is the code, omitting the method to view the content, which is the knowledge of the IO stream:

Datagramsocket DS = new datagramsocket (10086); byte [] DATA = new byte [1024]; datagrampacket dp = new datagrampacket (data, data. length); DS. receive (DP); // DS is omitted when data is retrieved from the package is enabled. close ();

The following describes TCP
TCP needs to establish a connection to form a transmission channel, which transmits a large amount of data during the connection process and forms a connection through a three-way handshake. This is a reliable protocol. Because a connection must be established, the efficiency will be slightly lower. TCP is like a call. When you make a call, the other party needs to press the access key to establish the call. Then, when you say something to me, the communication is complete, and when you call the call, you need to know the phone number of the other party, that is, the IP address, then the name of the person to be called, that is, the port number, and the topic to be picked up during communication, that is
Getoutputstream and getinputstream. The code below demonstrates

 
Socket S = new socket ("192.168.0.6", 10086); outputstream out = S. getoutputstream (); out. write ("Wei, nihao ". getbytes (); S. close ();

The acceptor receives the data before replying to you. The Code

 
Serversocket Ss = new serversocket (10086); socket S = ss. accept (); inputstream in = S. getinputstream (); byte [] Buf = new byte [1024]; int Len = in. read (BUF); // data processing SS is omitted here. close ();

In order to simply write only one process of sending and receiving, there is no back-and-forth communication, in fact, the truth is the same. The content is simple. In fact, it mainly provides learning methods to understand the principles and processes. In order to simply write only one process of sending and receiving, there is no back-and-forth communication, in fact, the truth is the same. The content is simple. In fact, it mainly provides learning methods to understand the principles and processes. In order to simply write only one process of sending and receiving, there is no back-and-forth communication, in fact, the truth is the same. The content is simple. In fact, it mainly provides learning methods to understand the principles and processes.

-------------------- Android training, Java training, and hope to communicate with you! ----------------------
For details, see edu.csdn.net/heima.

 

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.