JAVA Network Programming TCP communication, java Network Programming tcp

Source: Internet
Author: User

JAVA Network Programming TCP communication, java Network Programming tcp

Socket introduction:

Socket is called "Socket", which describes the IP address and port. Hosts on the Internet generally run multiple service software and provide several services at the same time. Each service opens a Socket and is bound to a port. Different ports correspond to different services. The Socket and ServerSocket classes are located in the java.net package. ServerSocket is used on the server. Socket is used to establish a network connection. When the connection is successful, a Socket instance is generated at both ends of the application to complete the required session by operating on the instance.

Common Socket methods:

-Int getLocalPort () to obtain the local port number

-InetAddress getLocalAddress (): Obtain the local address bound to the socket.

-Int getPort (): gets the port number used by the remote end.

-InetAddress. getInetAddress (): Obtain the remote address bound to the socket.

Common InetAddress methods:

-String getCanonicalHostName () obtains the Fully Qualified Domain Name of this IP address.

-String getHostAddress () returns the IP address String

1 // obtain the local address and port number: 2 public void testSocket () throws Exception {3 Socket socket = new Socket ("localhost", 8088); 4 InetAddress add = socket. getLocalAddress (); 5 System. out. println (add. getCanonicalHostName (); 6 System. out. println (add. getHostName (); 7 System. out. println (add. getLocalPort (); 8}
1 // obtain the remote address and port number: 2 public static void testSocket () throws Exception {3 Socket socket = new Socket ("localhost", 8088); 4 InetAddress inetAdd = socket. getInetAddress (); 5 System. out. println (inetAdd. getCanonicalHostName (); 6 System. out. println (inetAdd. getHostName (); 7 System. out. println (socket. getLocalPort (); 8}

Obtain the network input stream and network output stream

The following two methods are used to obtain the input stream and output stream through Socket:

-InputStream getInputStream () is used to return the input stream of this socket.

-OutStream getOutputStream () is used to return the output stream of this socket.

Public static void testSocket () throws Exception {Socket socket = new Socket ("localhost", 8088); OutputStream out = socket. getOutputStream (); OutputStreamWriter osw = new OutputStreamWriter (out, "UTF-8"); PrintWriter pw = new PrintWriter (osw, true); pw. println ("output content"); // InputStream in = socket. getInputStream (); InputStreamReader isr = new InputStreamReader (in, "UTF-8"); BufferedReader br = new BufferedReader (isr); br. readLine (); // read the received content}

Note: After Socket is used for communication, close the Socket to release system resources.

-Void close (): closes the socket. It also means that the obtained input and output streams are closed.

Server ServerSocket listener

1 // create a ServerSocket and apply for the Service port 80882 ServerSocket server = new ServerSocket (8088); 3/* The method will block until a Socket is connected, and return the Socket */4 Socket socket = server. accept (); 5...

Client Socket connection

1 // parameter 1: IP address of the server, parameter 2: Server Port 2 // Note: A connection is initiated when a Socket is created, if the connection fails, an exception will be thrown. 3 Socket socket = new Socket ("localhost", 8088); 4 ....

C-S Communication Model

Difference Between TCP and UDP

TCP (Transport Control Protocol) is a connection-based Protocol. That is to say, a reliable connection must be established with the other party before sending and receiving data. Chat tools use TCP protocol

User Data Protocol (UDP) is the Protocol corresponding to TCP. It is a non-connection-oriented protocol. Instead of establishing a connection with the other Party, it directly sends data packets! Most games use 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.