Socket programming all, TCP/IP
One, TCP/IP communication, must have an IP address, must have the same socket (socket)
Steps:
1, addressing positioning:
Locate the host by IP address
Find the program by port (port number) (0-65535)
To view the port number of the host: cmd Netstat-ano
SQL Port number: 3306
Our own: after 8080
Native IP 127.0.0.1
2. Communication (Protocol)
TCP/IP contains:
TCP Reliable (three-time handshake) B can receive, B is received, B returns information to a
TUP unreliable, stateless, fast, video is generally used in this
Socket sockets
The socket application accesses TCP/IP calls
Socket Programming:
①, server-side:
1, set up a port to monitor the server
ServerSocket Server = new ServerSocket (8080);
2. Ready to explain customer service request: wait-Prepare
Socket socket = server.accept ();
3. Create an input stream:
BufferedReader br = new Buff. (New InputS. R.. (Socket.getinputstream));
PrintWriter pw = new PrintWriter (Socket.getoutputstream (), true);
4. Close the stream, close the socket, close the server
Br.close (); Socket.close ();p w.colse ();
②, customer service side: Less the first step ServerSocket
1. Send a request to the server
Socket socket = new Socket ("127.0.0.1", 8080);
2. Create an input/output stream:
BufferedReader br = new Bu. R.. (New InputS. R (Socket.getinputstream))
3, close the flow, socket,server
3.19 complement