Java Network Programming Basics teaching threads socket Getting started instance _java

Source: Internet
Author: User
Tags flush

When we want to use TCP/IP in Java to connect to the server over the network, we need to create the Java.net.Socket object and connect to the server. If you want to use Java NIO, you can also create a Socketchannel object in Java NiO.

Create a socket

The following example code is connected to the 80 port on the IP address 78.64.84.171 server, which is our Web server (www.jb51.net), and the 80 port is the Web service port.

Copy Code code as follows:

Socket socket = new Socket ("78.46.84.171", 80);

We can also use domain names in place of IP addresses, as in the following example:
Copy Code code as follows:

Socket socket = new Socket ("Jb51.net", 80);

Socket send data

To send the data through the socket, we need to obtain the socket output stream (OutputStream), the sample code is as follows:

Copy Code code as follows:

Socket socket = new Socket ("Jb51.net", 80);
OutputStream out = Socket.getoutputstream ();

Out.write ("Some data". GetBytes ());
Out.flush ();
Out.close ();

Socket.close ();


The code is very simple, but if you want to send the data over the network to the server side, don't forget to call the Flush () method. The TCP/IP implementation at the bottom of the operating system puts the data into a larger data cache block, and the size of the cache block is compatible with the TCP/IP packet size. (Translator Note: Invoking the Flush () method simply writes the data to the operating system cache and does not guarantee that the data will be sent immediately)

Socket Read Data

Read the data from the socket, we need to get the socket input stream (InputStream), the code is as follows:

Copy Code code as follows:

Socket socket = new Socket ("Jb51.net", 80);
InputStream in = Socket.getinputstream ();

int data = In.read ();
... read more data ...

In.close ();
Socket.close ();


The code is also not complicated, but it should be noted that reading data from the input stream of the socket does not read the file, until the read () method is called until 1, because for the socket, the input stream of the socket is returned-1 only when the server closes the connection. Instead, the server does not keep shutting down the connection. If we want to send multiple requests over a single connection, it would be foolish to close the connection in this case.

Therefore, when reading data from the socket's input stream, we must know the number of bytes to read, which can be achieved by telling the server how many bytes were sent in the data, or by setting a special character tag at the end of the data.

Close socket

After using the socket we must close the socket and disconnect from the server. Closing the socket requires only calling the Socket.close () method, and the code is as follows:

Copy Code code as follows:

Socket socket = new Socket ("Jb51.net", 80);

Socket.close ();


(End of full text)

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.