Network application models mainly include:
www (World Wide Web) is an information browsing system based on the client/server model, based on HTML language and HTTP protocol, and can provide various Internet services. Network information is placed in different locations on the host, and the www server uses hypertext links to link various information. The www client (browser) is responsible for establishing contact with the server, sending requests to the server, processing HTML hypermedia, providing a graphical user interface (GUI), displaying information, etc.
In the client/server working mode, on the server side, be prepared to accept communications from multiple client computers. To this end, in addition to using IP addresses to identify computers on the Internet, port numbers are also introduced to identify threads that are serving background services on the server side. The combination of port number and IP address is called a network socket.
In the implementation of C/S mode in Java language, sockets are divided into two categories:
The Server machine provides services for the Client machine through ports (bus I/O addresses); the Server machine provides several different services at the same time on its several different ports. The Client accesses a certain port of the Server and requests the Server to serve it through this port. Regulations: Port numbers 0~1023 are for system exclusive use. For example, the HTTP protocol is on port 80 and the telnet protocol is on port 23. Ports 1024~65535 are used by applications.
When the Client program and Server program need to communicate, the Socket class can be used to establish a socket connection. Think of a socket connection as a phone call: initially the Client program establishes the call, and the Server program listens; after the call is completed, either party can speak at any time.
There are two optional ways to achieve communication between the two parties: streaming socket and datagram socket:
Streaming socket establishes a communication channel between the Client program and the Server program. Each socket can perform both read and write operations. For either end, the communication session process with the other party is:
Establish a socket connection, obtain input/output streams, read/write data, and close the socket (tear down the connection) after the communication is completed.
Using the socket constructor, a socket object can be established on the client side to the server:
Socket (String host, int port): host is the IP address of the server, port is the port number, these are pre-agreed.
For example, code:
try{ Socket mySocket = new Socket(“http://www.weixueyuan.net” ,1860); }catch(IOException e){}
Then, use the getInputStream() method to obtain the input stream, use this input stream to read the information put by the server into the "line"; use the getOutputStream() method to obtain the output stream, and use this output stream to write information to the "line".
Using the ServerSocket constructor, you can create a server socket object on the server that accepts client sockets:
ServerSocket(int port): Specify the port number and create a ServerSocket object. The port number port must be the same as the port number called by the customer. To do this, use the following form of code:
try{ ServerSocket serverSocket = new ServerSocket(1860); }catch(IOException e){}
The server-side program listens on the specified port. When receiving a service request from the Client program, it creates a socket object to communicate with the Client program corresponding to the port. For example, after executing the above code to create a server socket object and establishing the object serverSocket, it may use the accept() method to obtain the Socket object and receive the information from the client program from the socket mySocket. As shown in the following code:
try{ Socket sc = serverSocket.accept();//ac is a Socket object}catch(IOException e){}
To revoke the service, you can close the Socket object sc:
sc.close();
[Example] Client application in C/S mode. This is a simple example of client-side streaming Socket communication. The code illustrates how to write the client-side program. In the example, the Client program makes a request to port 4441 of the server host. After the connection is established, it completes reading and writing to the server.
import java.io.*;import java.net.*;public class Client{ public static void main(String args[]){ String s = null;Socket mySocket; DataInputStream in = null;DataOutputStream out = null; try{ mySocket = new Socket("localhost",4441); in = new DataInputStream(mySocket.getInputStream()); out = new DataOutputStream(mySocket.getOutputStream()); out.writeUTF("good server!"); while(true){ s = in.readUTF(); if(s==null) break; else System.out.println(s ); } mySocket.close(); }catch(IOException e){ System.out.println(“can't connect”); } }}
[Example] Server-side application corresponding to Client-side application. The program listens on port 4441. When a client request is detected, a string containing "Customer, hello, I am the server" is generated and output to the client.
import java.io.*;import java.net.*;public class Server{ public static void main(String args[]){ ServerSocket server = null; Socket you = null;String s = null; DataOutputStream out = null; DataInputStream in = null; try{ server = new ServerSocket(4441); }catch(IOException e1){ system.out.println("ERROR:" +e1); } try{ you = server.accept(); in = new DataInputStream(you.getInputStream()); out = new DataOutputStream(you. getOutputStream()); while(true){ s = in.readUTF( ); if(s!=null) break; } out.writeUTF("Hello customer, I am the server"); out.close(); } catch(IOException e){System.out.println(“ERROR:”+e);} }}
In order to give full play to the parallel working ability of the computer, the socket connection work can be completed by one thread. When the client requests the server to provide services, or when the server receives a customer's service request, it starts a thread dedicated to completing information communication, creates an input and output stream in this thread, and completes the information exchange between the client and the server.
[Example] A client applet that places socket connection work on a thread. The interface has a send message button, a text box and a text area. The client application first establishes a socket connection with the server. Use the data input stream to repeatedly read the information put into the line by the server, and display the received information in the text area. If the retrieved information is "end", the socket connection is closed and the program ends. The user can also enter information in the text box and press the send information button, and the client program uses the data output stream out to send the content in the text box to the server.
import java.net.*;import java.io.*;import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.applet.*;public class Aclient extends Applet implements Runnable,ActionListener{ JButton button; JTextField textF; JTextArea textA; Socket socket; Thread thread; DataInputStream in; DataOutputStream out; public void init(){ setBackground(new Color(120,153,137)); setLayout(new BorderLayout()); Button = new JButton("Send information"); textF = new JTextField(20); textA = new JTextArea( 20,30); setSize(450,350); JPanel p = new JPanel(); p.add(textF); p.add(button); add(textA,"Center"); add(p,"South"); button.addActionListener(this); } public void start(){ try{ socket = new Socket(this.getCodeBase().getHost(),4441); in = new DataInputStream(socket.getInputStream()); out = new DataOutputStream(socket.getOutputStream()); }catch(IOException e){} if(thread==null){ thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.start(); } } public void run(){ String s = null; while(true){ try{ s = in.readUTF(); }catch(IOException e){} if(s.equals("End")){ try{ socket.close();break; }catch(IOException e){} }else texA.append(s + "/n"); } } public void actionPerformed( ActionEvent e){ if(e.getSource()==button){ String s = textF.getText(); if(s! = null){ try{ out.writeUTF(s); }catch(IOException e1){} } else{ try{ out.writeUTF("Please speak"); } catch(IOException e1){} } } }}
[Example] The program uses terminal 4441 to establish a socket connection with the client. After the server receives the client's request, it creates a thread with the client's socket and starts it. If there is no customer application, continue to monitor customer applications. The thread establishes the input data stream in and the output data stream out according to the client's socket. The thread uses in to read the information that the customer puts into the line. If the received information is "End", the server replies "End" and then closes the socket connection; otherwise it replies: "I am the server you said to me", and the information received by the server.
import java.net.*;import java.io.*;import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.applet.*;public class Aclient extends Applet implements Runnable,ActionListener{ JButton button; JTextField textF; JTextArea textA; Socket socket; Thread thread; DataInputStream in; DataOutputStream out; public void init(){ setBackground(new Color(120,153,137)); setLayout(new BorderLayout()); Button = new JButton("Send information"); textF = new JTextField(20); textA = new JTextArea( 20,30); setSize(450,350); JPanel p = new JPanel(); p.add(textF); p.add(button); add(textA,"Center"); add(p,"South"); button.addActionListener(this); } public void start(){ try{ socket = new Socket(this.getCodeBase().getHost(),4441); in = new DataInputStream(socket.getInputStream()); out = new DataOutputStream(socket.getOutputStream()); }catch(IOException e){} if(thread==null){ thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.start(); } } public void run(){ String s = null; while(true){ try{ s = in.readUTF(); }catch(IOException e){} if(s.equals("End")){ try{ socket.close();break; }catch(IOException e){} }else texA.append(s + "/n"); } } public void actionPerformed( ActionEvent e){ if(e.getSource()==button){ String s = textF.getText(); if(s! = null){ try{ out.writeUTF(s); }catch(IOException e1){} } else{ try{ out.writeUTF("Please speak"); }catch(IOException e1){} } } }}