The example of this article tells the TCP transmission of Java to implement SOCKET. Share it for everyone for your reference. The specific analysis is as follows:
The client sends data to the server
* TCP transmission, the process of client establishment.
* 1, create a TCP client Socket service. The socket object is used.
* It is recommended that the object clear the destination as soon as the creation is created. To connect the host.
* 2, if the connection is successfully established, it means that the data transmission channel has been established.
* This channel is the Socket stream, which is established at the bottom. Since it is a flow, it means that there are both inputs and outputs here.
* If you want to input or output stream objects, you can find a socket to get it.
* You can obtain two byte streams through getoutPutstream () and getInputStream ().
* 3, use the output stream to write the data.
* 4, turn off the resources.
package com.socket.tcp.demo; Import Java.io.ioException; Import Java.io.OutPutstream; Import Java.socket; Import Java.net.unknownhoste xception; Public Class ClientDemo { /** * @param ARGS * @ Throws Ioexception * @Throws UnknownHostexception */Public Static Void Main (String [] ARGS) Throws UNKNOWHOSTEXCETION, IOEXCEPTION {// The client send Data to the server/ * * TCP transmission, the process of client establishment. * 1, create a TCP client Socket service. The socket object is used. * It is recommended that the object clear the destination as soon as the creation is created. To connect the host. * 2, if the connection is successfully established, it means that the data transmission channel has been established. * This channel is the Socket stream, which is established at the bottom. Since it is a flow, it means that there are both inputs and outputs here. * If you want to input or output stream objects, you can find a socket to get it. * You can obtain two byte streams through getoutPutstream () and getInputStream (). * 3, use the output stream to write the data. * 4, turn off the resources. */// Create client Socket service. // Connect the address of the target server, 192.168.1.100 is the address of the target server, and 10002 is the port socket socket = new socket ("192.168.1.100", 10002); // Get the output stream in the socket stream. Output messages to the server, that is, send a message to the server outputStream out = socket.getputstream (); // Use the output stream to write the specified data. out.write ("TCP Demonstration: Buddy is here again!" Getbytes ()); // Close the resource. socket.close ();}}
The server receives the data sent by the client and is printed on the console.
* The idea of establishing the TCP server:
* 1, create a server Socket service. Through the Serversocket object.
* 2, the server must provide a port to the outside world, otherwise the client cannot be connected.
* 3, get the client object that is connected.
* 4, get the data from the client through the client object
* And print on the console.
* 5, turn off the resources. Client client, closing server.
package com.socket.tcp.demo; Import Java.io.ioException; Import Java.Io.InputStream; Import java.net.serveersocket; Import Java.socket; ublic class serverDemo {// UDP: sending end, receiving end (No connection) // TCP: The client, server (connected) start the server first, start the client/** * @param ARGS * @throws IOEXCEPTION */Public Static Void Main (String [] ARGS) Throws IOEXCEPTION {// The server receives the data sent by the client and is printed on the console. / * * The idea of establishing the TCP server: * 1, create a server Socket service. Through the Serversocket object. * 2, the server must provide a port to the outside world, otherwise the client cannot be connected. * 3, get the client object that is connected. * 4, obtain data from the client object to obtain data from the client* and print it on the console. * 5, turn off the resources. Client client, closing server. *//1 Create the server object. Serversocket SS = New Serversocket (10002); // Listen to the 10002 port of the server where the application is located, // See if there are any clients connected, or send messages // 2, and get the client object connected to the connection. Socket s = ss.Accept (); // Obstruct. String ip = s.Getinetaddress (). Gethostaddress (); // Get the IP address of the connected client // 3, obtain the input flow through the socket object, read Data from the client, inputStream in = s.GetinputStream (); byte [] buf = new byte [1024]; int len = in.read (buf); string text = new string (buf, 0, len) System.out.println (IP+":"+Text); s.close (); ss.close (); // Close the server, theoretically it is not necessary}}}}
Run the renderings: (start the server first, then start the client)
It is hoped that this article is helpful to everyone's Java program design.