The socket is the endpoint of the interprocess communication link under a specific programming model. Because of the popularity of this particular programming model, the socket name has been reused in other fields, including Java called technology.
If a connection is to be made, a machine must run a process to wait for the connection, while another machine must attempt to reach the first machine. This telephone system is similar: a party must initiate a call, and the other party must wait for a phone call at this time.
Java Network Model Diagram
The following is an introduction to applying the java.net package to write a network application through a server and client with a "echo" feature.
The main feature of this example is that the server-side program waits for input from the client and then echoes the read to the client and outputs it on the server-side console. When the client receives information from the console, it sends input to the client and receives the ECHO data from the server, which is then displayed in the console.
The client program code is as follows:
Copy Code code as follows:
Package com.javapp.ch11;
Import java.io.*;
Import java.net.*;
/**
* Description: Server-side and client programs with ECHO feature
*/
public class Echoclientdemo {
Server-side service ports.
public static final int serverport = 990;
public static void Main (string[] args) {
try {
Establishes a connection socket.
Socket s = new socket ("localhost", serverport);
SYSTEM.OUT.PRINTLN ("socket =" + s);
The input stream for the new network connection.
BufferedReader in = new BufferedReader (new InputStreamReader (s)
. getInputStream ());
The output stream for the automatic refresh of the new network connection.
PrintWriter out = new PrintWriter (New BufferedWriter (
New OutputStreamWriter (S.getoutputstream ()), true);
First, we use system.in to construct InputStreamReader and reconstruct BufferedReader.
BufferedReader stdin = new BufferedReader (
New InputStreamReader (system.in));
System.out.println ("Enter a string, enter BYE to exit!");
while (true) {
Reads the string entered from the console and outputs it to the network connection, sending data to the server side.
Out.println (Stdin.readline ());
Reads a row from a network connection that receives server-side data.
String str = in.readline ();
If the received data is empty (if you press ENTER directly, not blank data), exit the loop and close the connection.
if (str = null) {
Break
}
System.out.println (str);
}
S.close ();
catch (IOException e) {
System.err.println ("IOException" + e.getmessage ());
}
}
}
In the client program above. First, the socket class in the java.net packet is used to establish a connection socket, and the getInputStream method of the socket object that is applied subsequently receives the data from the server and applies the socket object's Getouputstream method to send the data to the server. When you create an input/output stream, you can read and write data like a file.
The "echo" Server-side program code that supports multiple clients is as follows:
Copy Code code as follows:
Package com.javapp.ch11;
Import java.io.*;
Import java.net.*;
/**
* Description: "echo" server-side programs that support multiple clients
*/
public class Echoserverthreaddemo {
Server-side service ports.
public static final int serverport = 990;
public static void Main (string[] args) {
try {
The ordinal number of the client that is already connected.
int number = 1;
Establish a server-side listening socket.
ServerSocket s = new ServerSocket (serverport);
System.out.println ("started:" + s);
while (true) {
Wait and receive a request to establish a connection socket.
Socket incoming = S.accept ();
System.out.println ("Connection" + number + "accepted:");
SYSTEM.OUT.PRINTLN (incoming);
Start a thread to transfer data between the server and the client.
The main program continues to listen for requests to come in.
Thread t = new Echothread (incoming,number);
T.start ();
number++;
}
catch (IOException e) {
System.err.println ("IOException");
}
}
}
Class Echothread extends Thread {
Private Socket S;
int n;
Public Echothread (Socket incoming,int number) {
s = incoming;
n = number;
}
public void Run () {
try {
The input stream for the new network connection.
BufferedReader in = new BufferedReader (new InputStreamReader (s)
. getInputStream ());
The output stream for the automatic refresh of the new network connection.
PrintWriter out = new PrintWriter (New BufferedWriter (
New OutputStreamWriter (S.getoutputstream ()), true);
System.out.println ("hello! Enter BYE to exit. ");
echo the input from the client.
while (true) {
Reads a row from a network connection, which receives data from the client.
String line = In.readline ();
If the received data is empty (if you press ENTER directly, not blank data), exit the loop and close the connection.
if (line = = null) {
Break
} else {
if (Line.trim (). Equals ("BYE")) {
SYSTEM.OUT.PRINTLN ("the client" + N + "entered bye!");
System.out.println ("Connection" + N + "would be closed!");
Break
}
System.out.println ("Echo" + N + ":" + line);
Output a row to a network connection that sends data to the client.
Out.println ("Echo" + N + ":" + line);
}
}
Closes the socket.
S.close ();
catch (IOException e) {
System.err.println ("IOException");
}
}
}
In a server-side program, first create a server-side listening socket with the ServerSocket class in the java.net package. The Accept method of the ServerSocket class is then applied to wait and receive the user request. When the server receives a connection request, it starts a thread to handle the data transfer of the server and the client separately. The receiving and sending of server-side data is the same as the sending and presentation of the client data described above.