1. Network Basics
- The conditions to be met between the two machines that need to be communicated:
- Each machine has a unique identifier (IP address );
- Communication between them needs to be in the same language ( protocol );
- Each host has more than one application, such as QQ, Weibo, Thunderbolt, etc., how to distinguish which application issued the request, you need to use a different port number:
- The port number range is: 0-65535, of which 0-1023 is reserved by the system;
- The IP address and port number make up the so-called socket, Sockets are the endpoints of two-way communication links between programs running on the network, and are the basis of TCP and UDP;
- http:80; ftp:21; Telnet:23
- TCP/IP is the most widely used protocol in the world, and it is a collection of multiple protocols on different levels based on TCP and IP, as well as the TCP/IP protocol family or the protocol stack
- Tcp:tranmission Control Protocol Transmission Protocol
- Ip:internet Protocol Internet Protocol
- TCP/IP five layer model:
- One. Physical layer: Twisted pair, network card, network cable,
- Two. Data Link Layer
- Three. Network layer:
- Four. Transport layer: TCP/IP protocol
- Five. Application layer: HTTP protocol, FTP protocol, SMTP protocol, Telnet remote login service, etc.
- Java Support for network programming:
2. InetAddress class: Used to identify hardware resources on the network, representing the Internet (IP address).
- View the Java API to get method information for the InetAddress class: No constructors, but you can return inetadress objects by Getbyname ("hostname/IP address");
3. URL class: A Uniform Resource locator (that is, a URL) that can be directly read or written to data on the network through a URL.
- Consists of two parts: the protocol name and the resource name, separated by a colon, such as http://www.baidu.com, representing the HTTP protocol and the resource name, respectively
- Use: See more APIs
Public Static voidMain (string[] args) {//create an instance of a URL Try{URL Imooc=NewURL ("http://www.imooc.com"); //? The following represents the parameter, #后面表示锚点URL url =NewURL (IMOOC, "/index.html?username=tom#test"); System.out.println ("Protocol:" +Url.getprotocol ()); System.out.println ("Host:" +url.gethost ()); //If no port number is specified, the default port number is used, at which time the return value of the Getport () method is-1System.out.println ("Port:" +Url.getport ()); System.out.println ("Path:" +Url.getpath ()); System.out.println ("File:" +url.getfile ()); System.out.println ("Ref:" +url.getref ()); System.out.println ("Defaultport:" +Url.getdefaultport ()); System.out.println ("Query:" +url.getquery ()); Try{System.out.println ("Content:" +url.getcontent ()); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } Catch(malformedurlexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }View Code
- Read Web page content using a URL: The OpenStream () method of the URL object allows you to get the input stream for the specified resource
4. Socket communication
- TCP protocol is a connection-oriented, reliable, sequential, byte-stream sending of data
- A class for network communication based on TCP protocol
- Socket class for Client
- Server-Side ServerSocket class
- Implementation steps:
- Creating ServerSocket and sockets
- Open the input/output stream connected to the socket
- Read/write to socket according to protocol
- Turn off the input and output stream and close the socket
4. TCP Programming
- Description: Implements the user's login. The essence is that the client wants the server to send the information (username and password), the server side responds.
- Server-side:
- Create ServerSocket object, bind listening port;
- Listen for client requests via the Accept () method;
- After the connection is established, the request information sent by the client is read through the input stream;
- Send the response information through the output flow to the client;
- Close the appropriate resource.
- Client:
- Create a socket object that indicates the address and port number of the server you want to connect to;
- After the connection is established, the request information is sent through the output to the server side;
- Get the server response information through the input stream;
- Close the appropriate resource.
- Multi-Threaded Server
- The basic steps for using multithreading to communicate between a server and a multi-client are as follows:
- Server-side Create ServerSocket, loop call accept () Wait for client connection
- Client creates a socket and requests and server connections
- Server-side receive client request, create socket to establish a leased line connection with the customer
- Connect two sockets dialog on a separate thread
- Server side continues to wait for new connections
5. UDP programming
- Description: The UDP connection is a non-connected, unreliable connection.
- Datagrampacket: Represents a datagram packet; Datagramsocket: End-to-end communication
- Server-Side steps:
-
- Create Datagramsocket, specify port number
- Create Datagrampacket
- Receiving data sent by the client
- Reading data
- Client steps:
- Defining sending information
- Create a datagrampacket that contains the information that will be sent
- Create Datagramsocket
- Send data
6. Note
- Set the priority of multiple threads
- Whether to close the output and input stream: for the same socket, if the output stream is turned off, the socket associated with the output stream will also be closed, so it is generally not necessary to close the stream and close the socket directly.
- The use of TCP communication to transport objects, through the form of strings to interact, more should be the way the object is used, it can be encapsulated as a user object: ObjectOutputStream stream.
- The socket program passes the file.
Java--socket Programming (I.)