Java TCP Getting Started exampleTCP Sockets
Java provides two classes for the TCP protocol: Socket class and ServerSocketclass. A socket instance represents a TCP connectionend. A TCP connection is an abstract two-way channel with the IP address and port numberThe .
TCP Client
A typical TCP client is going through the following three steps:
1. Create a Scoket instance: the constructor establishes a TCP connection to the specified remote host and port.
2. Communication via the socket's input/output stream (I/O streams): A Scoket Connection instance consists of a inputstream and a outputstream, and their usage is the same as other Java input and output streams.
3. Using the CLO of the Scoket classThe SE () method closes the connection.
TCP server-side
1. Create a SERVERSOCKet Instances, and specify the local port. The function of this escaped word is to listen for the specified port receivedthe connection.
2. Repeated execution:
A. Calling SerVersocketthe Accept () method to get the nextclient connections, based on newly established customersEnd Connection,creates a socket instance and is created by AcceThe PT () method returnsback.
B. Using the InputStream and outputstream of the returned socket instance and the client-side inputline traffic.
c. After the communication is complete, use the close () method of the socket class to close the client socketconnection.
schematic diagram:
The following program example:Tcpserver.java
Package Com.iboxpay.tcp;import Java.io.bufferedreader;import Java.io.dataoutputstream;import java.io.IOException; Import Java.io.inputstream;import Java.io.inputstreamreader;import Java.io.outputstream;import Java.net.serversocket;import Java.net.socket;public class TCPServer {public static void main (string[] args) throws IOException {BufferedReader buffer = null; ServerSocket server= null; Socket s = null;inputstream is = null;outputstream OS = Null;dataoutputstream dos = null;try {server = new ServerSocket (8 080); s = server.accept (); is = S.getinputstream (); OS = S.getoutputstream (); SYSTEM.OUT.PRINTLN ("-----------Welcome to TCP socket-------"), buffer = new BufferedReader (new InputStreamReader (IS)); DOS = new DataOutputStream (OS); System.out.println (Buffer.readline ());//output} catch (IOException e) {e.printstacktrace ();} Finally{buffer.close (); Os.close (); Is.close (); S.close (); Server.close ();}}}
Tcpclient.java
Package Com.iboxpay.tcp;import Java.io.bufferedreader;import Java.io.dataoutputstream;import java.io.IOException; Import Java.io.inputstream;import Java.io.inputstreamreader;import Java.io.outputstream;import Java.net.inetaddress;import Java.net.socket;public class Tcpclient{public static void Main (string[] args) {string[] str = {"172.30.5.58", "8080"}; try {client (str);} catch (IOException e) {e.printstacktrace ()}} public static void Client (string[] str) throws Ioexception{socket s = null;inputstream ips = Null;outputstream ops = null; BufferedReader Brkey = Null;dataoutputstream dos = null; BufferedReader brnet = null;try {//socket s=new Socket (Inetaddress.getbyname ("192.168.0.213"), 8001); if (Str.length < 2) {System.out.println ("Usage:java TcpClient serverip serverport"); Return }//Build socket S =new socket (Inetaddress.getbyname (str[0]), Integer.parseint (str[1])); IPs =s.getinputstream (); OPS =s.getoutputstream (); DOS = new DataOutputStream (OPS); Brnet = new BufferedrEader (new InputStreamReader (IPS)); while (true) {Brkey = new BufferedReader (new InputStreamReader (system.in));//keyboard input String Strword = Brkey.readline (); Dos.writebytes (Strword + system.getproperty ("Line.separator")); if (Strword.equalsignorecase ("Quit")) break; }}catch (Exception e) {e.printstacktrace ();} finally{dos.close (); Brnet.close (); Brkey.close (); S.close (); }}}
Java TCP Getting Started example