TCP Programming
Using the socket (socket) in Java to complete the development of TCP programs, using this class can easily establish a reliable, bidirectional, continuous, point-to-point communication connection.
In the socket program development, the server side uses ServerSocket to wait for the client connection, for the Java Network program, each client uses a socket object representation.
In the Java Network program, the client can receive the server as long as it conforms to the connected communication protocol.
The ServerSocket class is primarily used in the development of server-side programs to receive connection requests from clients.
The socket will use the Aceept () method to wait for a client connection each time the server side runs, and after this method executes, the server side goes into a blocking state until the client connects until the program can continue down, and the return value type of this method is socket, Each socket represents a client object.
Accept () Returns a socket instance object each time
TCP Programs
PackageCom.oumyye. Network programming;Importjava.net.* ;ImportJava.io.* ; Public classhelloclient{ Public Static voidMain (String args[])throwsException {//all exceptions ThrownSocket client =NULL;//represents the clientClient =NewSocket ("localhost", 8080) ; BufferedReader buf=NULL;//One-time reception completeBUF =NewBufferedReader (NewInputStreamReader (Client.getinputstream ())); String Str=Buf.readline (); System.out.println ("Server-side output content:" +str); Buf.close (); Client.close (); }};
After you have finished writing the server, you can test it directly with the Telnet command.
Open localhost 8080
Java Basics---Network programming (TCP programming)