Java programming Those things 103--network programming technology 2

Source: Internet
Author: User
Tags socket port number

13.2.3 TCP Programming

According to the previous introduction, the network communication mode has TCP and UDP two kinds, in which the TCP mode of network communication is to maintain the connection in the process of communication, a bit similar to the telephone, only need to dial a number (establish a network connection), you can call (multiple transmission data). In this way in the actual network programming, because the transmission is reliable, similar to the phone, if a to B call, B said did not hear clearly let a repeat, until B listen to clearly, the actual network transmission is the case, if the sender sent the data receiver feel a problem, then the network will automatically request the sender to resend, Until the receiving party receives it.

In the Java language, it provides good support for network programming in TCP mode, in the actual implementation, the Java.net.Socket class on behalf of the client connection, the Java.net.ServerSocket class on behalf of the server-side connection. In the network programming, the details of the underlying network communication has been achieved a relatively high encapsulation, so when the programmer actually programming, only to specify the IP address and the port number can be established connection. It is precisely because of this high degree of encapsulation, on the one hand simplifies the Java language Network programming difficulty, but also makes the use of the Java language for network programming can not penetrate to the bottom of the network, so it is very difficult to use the Java language to programming the low-level system of the network, in particular, The Java language does not implement low-level network sniffing and access to information such as IP packet structure. However, because the Java language network programming is relatively simple, it has been widely used.

When you use TCP for network programming, you need to follow the steps of the network programming described earlier, and here are a few steps to implement on the client and server side of the Java language.

In Client network programming, you first need to establish a connection, which in the Java API represents a network connection as an object of the Java.net.Socket class, so establish a client network connection, which is to create the object of the socket type that represents the network connection, as shown in the following example:

Socket socket1 = new Socket(“192.168.1.103”,10000);
Socket socket2 = new Socket(“www.sohu.com”,80);

In the code above, SOCKET1 implementation is connected to the IP address is 192.168.1.103 computer port 10,000th, and Socket2 is connected to the domain name is www.sohu.com computer 80th port, as to how the underlying network to achieve connectivity, for programmers is completely transparent. If a connection is established, the native network is disconnected, or the server-side program is not turned on, an exception is thrown.

Once the connection is established, it completes the first step of client programming, followed by a "request-response" model for network data interchange, in the Java language, the data transfer function is implemented by Java IO, which means that only the input and output streams need to be obtained from the connection. The data to be sent is then written to the output stream of the connection object, and the data is read from the input stream after the send completes. The sample code is as follows:

OutputStream os = socket1.getOutputStream();  //获得输出流
InputStream is = socket1.getInputStream();  //获得输入流

In the code above, from Socket1 this connection object obtains the output stream and the input stream object, in the entire network programming, the subsequent data exchange becomes the IO operation, namely follows "The request-response" model stipulation, first writes the data to the output stream, this data will be sent out by the system, It then reads the server-side feedback from the input stream, which completes a data exchange process, which can be done many times.

Here is only the most basic output stream and input stream objects, but also based on the previous learning IO knowledge, using the flow of the nesting of these obtained basic flow objects to the desired decorative flow objects, so as to facilitate the operation of the data.

Finally, when the data exchange is complete, turn off the network connection, release the network connection occupies the system port and the memory and so on resources, completes the network operation, the example code is as follows:

socket1.close();

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.