JAVA notes: Java Network Programming (3) TCP programming, network programming tcp
In Java, Socket is used to develop TCP programs. Point-to-point communication connections can be established.
In Socket program development, the server uses ServerSocket to wait for the client connection. For Java Network programs, each client uses a Socket object.
As long as the client complies with the connection communication protocol, the server can receive it.
The main method of SeverSocket class:
Each time the Socket class is run on the server, the accept () method needs to be used to wait for the client to connect. After this method is executed, the server will enter the blocking status until the program is executed after the client is connected, the return type of this method is Socket, and each Socket represents a client object. Main Methods of Socket classes:
During communication, the server can perform input and output, and the client can also perform input and output.
A simple C/S architecture design: the server sends data to the client through a specified port through which the client receives data. Server:
Package IP_demo; import java. awt. print. printable; import java. io. IOException; import java. io. printStream; import java.net. serverSocket; import java.net. socket; public class HelloServer {public static void main (String [] args) throws IOException {ServerSocket server = null; Socket client = null; PrintStream out = null; server = new ServerSocket (8080); System. out. println ("server running, waiting for client connection ·"); client = server. accept (); String str = "Helloworld"; out = new PrintStream (client. getOutputStream (); out. println (str); client. close (); server. close ();}}
Client:
Package IP_demo; import java. io. bufferedReader; import java. io. inputStreamReader; import java.net. socket; public class HelloClient {public static void main (String args []) throws Exception {// All exceptions throw Socket client = null; client = new Socket ("localhost ", 8080); BufferedReader buf = null; buf = new BufferedReader (new InputStreamReader (client. getInputStream (); String str = buf. readLine (); System. out. println ("server output content:" + str); buf. close (); client. close ();}};
The C/S architecture requires both the server and client code. The above code can only complete data transmission once. The following example is a classic case of network programming communication interaction. It is called a response program. What is input by the client, the server will respond to the received content. Server Design: To enable the client to be used by multiple users at the same time, multi-threaded operations must be added:
Import java.net. *; import java. io. *; public class EchoThreadServer {public static void main (String args []) throws Exception {// All exceptions throw ServerSocket server = null; // defines ServerSocket class Socket client = null; // indicates that the client server = new ServerSocket (8888); // The server listens to boolean f = true on port 8888; // defines a flag while (f) {System. out. println ("the server runs and waits for the client to connect. "); Client = server. accept (); // get the connection, and the program enters the blocking state new Thread (new EchoThread (client )). start (); // each client represents a thread} server. close ();}};
Import java.net. *; import java. io. *; public class EchoThread implements Runnable {private Socket client = null; public EchoThread (Socket client) {this. client = client;} public void run () {BufferedReader buf = null; // receives the input stream PrintStream out = null; // print the stream output. try {out = new PrintStream (client. getOutputStream (); // prepare to receive the input information of the client buf = new BufferedReader (new InputStreamReader (client. getInputStream (); boolean flag = true; // flag, indicating that you can always receive and respond to the information while (flag) {String str = buf. readLine (); // receives the content sent by the client if (str = null | "". equals (str) {// indicates no content flag = false; // exit loop} else {if ("bye ". equals (str) {// If the input content is bye, the flag is terminated;} else {out. println ("ECHO:" + str); // Response Message }}} client. close () ;}catch (Exception e ){}}};
Client:
Import java.net. *; import java. io. *; public class EchoClient {public static void main (String args []) throws Exception {// All exceptions throw Socket client = null; // client = new Socket ("localhost", 8888); BufferedReader buf = null; // PrintStream out = null is received at a time; // send data BufferedReader input = null; // receives keyboard data input = new BufferedReader (new InputStreamReader (System. in); buf = new BufferedReader (new InputStreamReader (client. getInputStream (); out = new PrintStream (client. getOutputStream (); boolean flag = true; // defines the flag while (flag) {System. out. print ("input information:"); String str = input. readLine (); // receives the input information of the keyboard out. println (str); if ("bye ". equals (str) {flag = false;} else {String echo = buf. readLine (); // receives the returned result System. out. println (echo); // Output Response Information} buf. close (); client. close ();}};