In client/server operating mode, on the server side, you are ready to accept communication from multiple client computers. To do this, in addition to using IP addresses to identify computers on the Internet, a port number is also introduced to identify the thread that is in the server-side background service with the port number. The combination of port number and IP address is called a network socket (socket).
Java language in the implementation of the C/S mode, sockets are divided into two categories:
On the server side, the ServerSocket class supports the underlying network communication;
On the client side, the socket class supports the underlying communication of the network.
Server machines provide services to client machines via ports (bus I/O addresses), and server machines provide several different services at the same time on several different ports. The client accesses a port on the server, which is brought to the server for its service. Provisions: Port number 0~1023 for the system-specific. For example, the HTTP protocol is on port 80,telnet protocol on port 23. Port 1024~65535 is intended for use by applications.
You can use the socket class to establish a socket connection when the client program and the server program need to communicate. A socket connection can be imagined as a phone call: Initially the client program establishes the call, the server program listens, and when the call is completed, either party can speak at any time.
Two sides to achieve communication there are streaming sockets and datagram socket two options:
A streaming socket is a connected communication, TCP (Transmission Control Protocol): A connection is established before each communication, and the connection is disconnected after the end of the communication. The characteristic is that it can guarantee the correctness and reliability of transmission.
A datagram socket is a non-connected communication, UDP (User Datagram Protocol): The data to be transferred is divided into small packets and sent directly to the Internet. No need to establish connections and remove connections, fast, but no reliable guarantee.
A streaming socket establishes a communication channel between the client program and the server program. Each socket can be read and write in two different operations. For either end, the communication session process with the other party is:
Establish socket connection, get input/output stream, read data/write data, close socket when communication is complete (remove connection).
Using the socket construction method, you can establish a socket object to the server on the client:
Socket (String host,int Port): Host is the IP address of the server, port is the port number, and these are pre-agreed. The input stream is obtained by using the getInputStream () method, the input stream is used to read the information of the server into the "line", and the output stream is obtained with the Getoutputstream () method, and the information is written to "line" with this output stream.
Using the ServerSocket construction method, you can establish a server socket object that accepts client sockets on the server:
ServerSocket (int port): Specify the port number to create a ServerSocket object. Port number port to be the same as the port number that the customer is calling.
The server-side program listens on the specified port and, when it receives a service request from the client program, creates a socket object that communicates with the client program that corresponds to that port. For example, after executing the above code to establish a server socket object, after establishing the object ServerSocket, it is possible that it uses the Accept () method to get the socket object.
Package cn.url.day24;
Import Java.io.BufferedReader;
Import Java.io.DataOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import Java.net.Socket;
Import java.net.UnknownHostException;
/** * Client */public class Clientsocketdemo {public static void main (string[] args) {DataOutputStream dos = null;
try {//Initiate socket request open socket SOCKET = new Socket ("192.168.5.15", 9527);
OutputStream OS = Socket.getoutputstream ();
The data processing stream is responsible for sending to the client and getting data from the keyboard. Dos = new DataOutputStream (OS);
System.out.println ("Please enter any content to start transmission:");
String S=getkeyboard ();//Gets the keyboard data while (!s.equals ("*")) {System.out.println ("Please enter what needs to be transferred:");
S=getkeyboard (); Dos.writeutf ("Hello server:" +s);//Send data to server side} socket.close ();//Close Socket link//dos.close ();
Wnhostexception e) {e.printstacktrace ();
} catch (IOException e) {e.printstacktrace (); }finally{if(dos!=null) try {dos.close ();
} catch (IOException e) {e.printstacktrace (); }}}/** * get keyboard input */public static String Getkeyboard () {InputStreamReader is = new InputStreamReader (System.
in);
BufferedReader br = new BufferedReader (IS);
String S=null;
try {s = br.readline ();
return s;
} catch (IOException e) {e.printstacktrace ();
} return s; }
}
Package cn.url.day24;
Import Java.io.DataInputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.net.ServerSocket;
Import Java.net.Socket;
/**
* Service side
*
/public class Serversocketdemo {public
static void Main (string[] args) {
SYSTEM.OUT.PRINTLN ("Server startup. ");
DataInputStream dis = null;
try {
ServerSocket serversocket = new ServerSocket (9527);
Socket socket = serversocket.accept ();
InputStream is = Socket.getinputstream ();
InputStreamReader ISR = new InputStreamReader (is);
dis = new DataInputStream (is);
while (true) {
String line = Dis.readutf ();
SYSTEM.OUT.PRINTLN ("The message sent by the client is:" +line);}
} catch (IOException e) {
e.printstacktrace ();
} finally{
try {
if (dis! = null)
dis.close ();
} catch (IOException e) {
e.printstacktrace ();
}
}
}
}