Using java to achieve a functional server / client conversations.
You must first run the server is running, then run the client.
Each time a client sends a message, the server receives a message, and then send a message to the server, the client then receives a message.
Program to achieve the following:
Client:
. 1 public class Client { 2 static the Socket Client; . 3 . 4 public Client () { . 5 the try { . 6 // link port 9999 of the machine . 7 Client = new new the Socket ( "127.0.0.1", 9999 ); . 8 } the catch (Exception E ) { . 9 System.out.println ( "O Client error!" + E); 10 } . 11 } 12 is 13 is public static void Send (String MSG) { 14 the try { 15 // real-time refresh output stream 16 the PrintWriter OUT = new new the PrintWriter (client.getOutputStream (), to true ); . 17 Out.println ( "Client:" + MSG); 18 is // the out.close (); . 19 } the catch (IOException E) { 20 is System.out.println ( "error Send O!" + E); 21 is } 22 is } 23 is 24 public static void the listen () { 25 the try { 26 is System.out.println ( "Listening !. ..... " ); 27 // get the input stream 28 the InputStreamReader in = new new the InputStreamReader (client.getInputStream ()); 29 the BufferedReader br = new new the BufferedReader (in); 30 // read and print out a line 31 is System.out.println (br.readLine ( )); 32 } the catch (IOException E) { 33 is System.out.println ( "the Listen ERRO!" + E); 34 is } 35 } 36 37 [ public static void main (String [] args) { 38 is String MSG = ""; 39 Client cl = new Client(); 40 Scanner cin = new Scanner(System.in); 41 while (!msg.equals("#")) { 42 //cl=new Client(); 43 System.out.print("输入信息:"); 44 msg = cin.nextLine(); 45 cl.send(msg); 46 cl.listen(); 47 } 48 } 49 }
Server:
. 1 public class Server { 2 static the ServerSocket Server; . 3 static the Socket S; . 4 public Server () { . 5 the try { . 6 System.out.println ( "boot server!" ); 7 // listening on a local port 9999 . 8 Server = new new the ServerSocket (9999 ); 9 // wait for the client to create link objects Socket 10 S = server.accept (); . 11 } the catch (IOException E) { 12 is System.out.println ( "error O!" +E); 13 is } 14 } 15 public void the listen () { 16 the try { . 17 System.out.println ( "Listening ......!" ); 18 is // obtain input from the Socket stream in . 19 the InputStreamReader in = new new the InputStreamReader (s.getInputStream ()); 20 is the BufferedReader br = new new the BufferedReader (in); 21 is // read the input stream and the output line 22 is System.out.println (br.readLine ()); 23 is } the catch (IOException E) { 24 System.out.println ( "! O error" + E); 25 } 26 is } 27 public static void Send (String MSG) 28 { 29 the try { 30 // get <span style = "color from the Socket: # FF0000 ; "> updated in real time </ span> output stream, 31 is the PrintWriter OUT = new new the PrintWriter (s.getOutputStream (), to true ); 32 // writes a line data to the output stream 33 is Out.println (" Server: "+ MSG ); 34 is } the catch (IOException E) { 35 System.out.println ( "send input output error!" + E); 36 } 37 [ } 38 is public static void main (String [] args) { 39 Server SE = new new Server (); 40 String MSG = "" ; 41 is CIN = Scanner new new Scanner (the System.in); 42 is // input "#" represents a stop 43 is the while (msg.equals ( "#"! )) { 44 is // receiving information 45 se.listen (); 46 is the System.out .print ( "input information:" ); 47 = MSG cin.nextLine (); 48 // send 49 se.send (MSG); 50 } 51 is } 52 is }
Program execution as shown below:
The basic steps of the socket communication:
(1) specify a port number to create ServerSocket, the server call accept () method waits to receive a connection from a client.
(2) client creates a socket connection server.
(3) Open the socket input and output streams.
(4) using the input and output streams of the read and write socket
(5) and close the stream socket
Java socket API shown in the following figure:
java create a socket in fact corresponds to the creation of the linux socket API in the socket, bing () and listen ().
java accept method in the socket corresponding to the socket API accept (), readLine java input stream of a socket () of the corresponding recv () method of the socket API.