1. Network : Different areas of the calculator connected to the network, including LAN metropolitan area network Internet, etc.
Address: IP address, determine an absolute location on the network, port number: Used to differentiate the software on the computer.
Resource needs to come in so there is a URI, url where the URL represents a Uniform resource locator, and the URI represents a uniform resource
URL is an upgraded version of URI with one more location
2. TCP protocol and UDP protocol
TCP: Connect and turn on the phone like a call, say one response at a time, face the connection, inefficient
UDP: Protocol, SMS, non-oriented connection
3. ports:between 1~65535, it is generally not recommended to use the port below 1024, because the port is occupied more
4. Caching; The cost of DNS lookups is very large, so the InetAddress class caches the corresponding results, so once the results are obtained then it will not be looked up again, in some cases the first lookup fails, and the second is found to be successful because the first time the information is in transit, the second request is available , because the Supplemental DNS query is only cached for 10 seconds, a change in the IP address may take several hours, during this time there will be a variety of exceptions, the hostname is generally more stable than the IP address, so generally choose the host name, the host name can not be found when the IP address is selected.
5. inetaddress : There is no public constructor, but there are static factory methods to connect to the DNS server to resolve host names, which are commonly used with inetaddress.getbyname () and Inetaddress.getlocalhost ();
Common methods of InetAddress:
Gethostaddress (): Get IP Address
GetHostName (): Get host Name
URI: A string that identifies a resource
URL: Identifies a resource plus location
Common methods
OpenConnection()
OpenStream()
例如https://hao.360.cn/?src=lm&ls=n31c42a959f
GetFile () : gets this URL directory path plus query string hostname after the first/and identifier #, not just after the host name from the first/to the last?src=lm&ls=n31c42a959f
Getport () : Gets the URL port number of this, no return-1;
GetPath () : Gets the URL directory path for this. /
Getprotocol () : Gets the URL protocol name for this.https
GetHost () : Gets the URL host name for this.hao.360.cn
GetRef():url的片段标识符#后面的,没有返回null;
Getquery():返回查询字符串src=lm&ls=n31c42a959f
The Equals method should be avoided on the URL because it may be blocking I/O operations
Get communication: Name value pair, unsafe to display
Get/index.html? Account=dasdsa&password=dsadsadsad http/1.1: For a request line, get is a representation of the returned resource,/index.html the path of the requested resource, http// 1.1 is the version of the agreement? followed by a key-value pair,
host:127.0.0.1:8888: port number;
accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8: Types of text that can be processed
Connection:keep-alive: Keep connected
6. URLConnection
Getcontentype (): Gets the content type of the response body
Getconnectlength (): Gets the number of bytes of content, no return-1;
getOutputStream();返回输出流
7. Socket
Socket(InetAddress address, int port):开启socket;
setSoTimeout(int timeout):设置超时时间;
getOutputStream():请求输出流
close():关闭输入输出
shutdownInput():关闭输入,不释放socket占用的资源
shutdownOutput():关闭输出,不释放socket占用的资源
getPort():获取远程端口
getKeepAlive():是否开启连接
8. Seversocket
ServerSocket(int port):创建serversocket对象
accept():监听连接和处理连接是可能抛出的异常,返回一个socket对象
DatagramSocket
DatagramSocket():用于client端
DatagramSocket(int port):用于server端
send(DatagramPacket p):用于client端
receive(DatagramPacket p):用于server端
setSoTimeout(int timeout):连接超时时间
9. DatagramPacket
DatagramPacket(byte[] buf,int length,InetAddress address,int port):用于client端,打包数据
getData():获得缓冲区数据,返回一个字节数组,后面一般会利用getLength()
getLength():getdata()中有效长度
See: O ' Reilly network programming
Java Basics of Network programming