Socket
The two programs on the network realize the exchange of data through a two-way communication connection, one end of this connection is called a socket.
Sockets are the abstraction layer of TCP/IP Protocol communication, so we also need to understand the TCP protocol
Transport Layer Protocol
TCP: Connection-oriented, experienced three handshake, the advantage is reliable transmission (to ensure data correctness, ensure the order of data), for the transmission of large amounts of data (stream mode), the disadvantage is that slow, the establishment of a connection requires more overhead time and system resources.
UDP: For non-connected, transport unreliable drops (data loss), for transmitting small amounts of data (packet mode), fast.
Add: The so-called three-time handshake (Three-way handshake), which is when a TCP connection is established, requires a total of 3 packets sent by the client and the server.
The purpose of the three-time handshake is to connect the server to the specified port, establish a TCP connection, and synchronize the serial number and confirmation number of both parties and Exchange TCP window size information. In socket programming, the client executes connect (). Will trigger a three-time handshake.
Let's implement the following server-to-client communication:
Server: Server.class
Package sc_test;
Import Java.io.BufferedWriter;
Import Java.io.DataOutputStream;
Import java.io.IOException;
Import Java.io.OutputStreamWriter;
Import Java.net.ServerSocket;
Import Java.net.Socket;
public class Server {
public static void Main (string[] args) throws IOException {
ServerSocket Server =new serversocket (8888); Create a server-specific port
System.out.println ("The server has been created waiting for the client ... ");
Socket socket =server.accept (); receiving connections from clients
System.out.println (" a client establishes a connection ");
Send Data
String msg = " Welcome connection Server ";
output Stream
Output Method One
BufferedWriter bwriter = new BufferedWriter (New OutputStreamWriter (Socket.getoutputstream ()));
//
Bwriter.write (msg);
Bwriter.newline ();
Bwriter.flush ();
Output Method Two
DataOutputStream dos = new DataOutputStream (Socket.getoutputstream ());
Dos.writeutf (msg);
Dos.flush ();
}
}
Client: Client.class
Package sc_test;
Import Java.io.BufferedReader;
import Java.io.DataInputStream;
import java.io.IOException;
Import Java.io.InputStreamReader;
import Java.net.Socket;
import java.net.UnknownHostException;
Public class Client {
Public Static void Main (string[] args) throws Unknownhostexception, IOException {
Socket Client = new socket ("localhost", 8888); To create a client, you must specify a server + port to establish a connection
Receive Data
BufferedReader br= new BufferedReader (New InputStreamReader (Client.getinputstream ()));
String echo = Br.readline (); Blocking Method
the corresponding method two
DataInputStream dis = new datainputstream (Client.getinputstream ());
String echo = Dis.readutf ();
System. out. println (ECHO);
}
}
Experimental results:
Open the server thread, the following occurs, into the blocking state, waiting for the client to connect as follows:
The server has been created waiting for the client ...
Then run the client program and receive the following information sent to the client by the server:
Welcome to connect to server
The server receives the printing information as follows:
The server has been created waiting for the client ...
A client establishes a connection
Java--scoket Communications