Summary of socket usage for Java network programming __java

Source: Internet
Author: User

PS: This article is my own Learning Java Network Programming notes recorded in the process, is a summary of what has been learned. Basic knowledge of network programming

The URL (uniform Resource locater) Uniform Resource Locator that represents the location of resources in the network. (Can be a file or directory, or it can be a database)

Common formats for URLs are as follows: Transport protocol://Host Name: Port number: filename (for example,--http://192.168.1.110:80/index.html), where the parameters are described below:

Transmission protocol refers to the way of acquiring resources, such as HTTP, FTP, TFTP and telent, etc.

The host name can be an IP address, or it can be a host domain name;

The port number corresponds to a different network service (the default port number for the HTTP protocol is 80);

The file name is the full path to the resource file (relative to the default root directory of the access specified by the server)

A description of the file path: Suppose that the absolute path of a file is E:\Demo\Eclipse\NET\URL\URLdemo1\output.html, expressed as "e:\\demo\\eclipse\\net\\url\\urldemo1\\ Output.html ", (escape character) Note the separator is" \ "(Windows). Note The platform that Java is running, different systems (such as Windows and Linux) have different delimiters, under Windows \, under Linux for/. Socket Programming

Java supports streaming based communication (Stream-basedcommunication) and packet based communication (packet-basedcommunication). Stream based communication uses TCP, and packet based communication uses Udp,java to create a socket (socket) for network communication. server sockets (servers socket)

The following statement creates a server socket and attaches it to a port:

<span style= "Font-family:courier new;font-size:14px;" >serversocketserversocket = new ServerSocket (port);</span>

If you attempt to create a server socket on a port that you already use, you can cause a java.net.BindException exception.

After creating a server socket, the server listens for the connection using the following statement:

<span style= "Font-family:courier new;font-size:14px;" >socket Socket =serversocket.accept ();</span>
client socket (socket)

The client executes the following statement, requesting a connection to the server:

<span style= "Font-family:courier new;font-size:14px;" >socketsocket = new Socket (serverName, Port);</span>

The above statement opens a socket that enables the client program to communicate with the server, where servername is the hostname or IP address of the server. If the specified host is not found, the socket construction method throws an exception java.net.UnknownHostException. data transfer through sockets

Gets the input/output stream by calling the getInputStream () and Getoutputstream () methods of the socket object. InputStream and OutputStream streams are used to read or write bytes, and can be packaged using DataInputStream, DataOutputStream, BufferedReader, and PrintWriter InputStream and OutputStream to read basic data types and strings. InetAddress class

InetAddress represents an IP address, which has two subclasses: Inet4address, Inet6address, and IPV4 and IPV6 addresses respectively. The class does not provide a constructor and can only obtain the inetaddress instance through the two static methods it provides.

<span style= "Font-family:courier new;font-size:14px;" >getbyname (String host);//According to the host name provided, obtain the corresponding inetaddress instance

getbyaddress (byte[] addr);//According to the IP address provided, Get the corresponding inetaddress instance </span>

Use the InetAddress class in the server program to get the host name and IP address of the client. A inetaddress instance of a socket connected to a client can be obtained by using the following statement in the server program:

<span style= "Font-family:courier new;font-size:14px;" >inetaddressinetaddress = socket.getinetaddress ();</span>

You can also use the static method of the InetAddress class Getbyname () to create a inetaddress instance through a host name or IP address.

<span style= "Font-family:courier new;font-size:14px;" >inetaddressinetaddress = Inetaddress.getbyname ("Jack");</span>
using Urldecoder and Urlencoder

When a string containing non-Western European characters (such as Chinese characters) is included in the URL address, the system converts these non-Western European strings to application/x-www-form-urlencoded MIME strings.

The Urldecoder class contains a decode (Strings, String Enc) static method, complete from ... A MIME to a normal string conversion. Decoding

The Urlencoder class contains a encode (Strings, String enc) static method that completes from the normal string to ... A MIME conversion. (encoded) MIME (multipurposeinternet mail Extensions) Multipurpose Internet Mail Extension type, which is the type of application that sets the file name for an extension, and when the file is accessed, The browser will automatically open using the specified application.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.