Network Collection
Simple Tutorial
Use HttpURLConnection to download file from an HTTP URL
Create a URL object for a given URL. The URL can be either:
A direct link which contains the real file name at the end, for example:http://jdbc.postgresql.org/download/postgresql-9.2-1002.jdbc4.jar
An indirect link which does not contain the real file name, for example:http://myserver.com/download?id=1234
Open connection on the URL object – which would return an
HttpURLConnection
object if the URL is an HTTP URL.Open the input stream of the opened connection.
Create an output stream to save file to disk.
Repeatedly read array of bytes from the input stream and write them to the output stream, until the input stream is empty.
Close the input stream, the output stream and the connection.
An HTTP utility class to send GET/POST request
sendGetRequest(requestURL)
: accepts only the URL string.sendPostRequest(requestURL, params)
: accepts a URL string and aHashMap
containing POST parameters.readSingleLineRespone()
: returns a String.readMultipleLinesRespone()
: returns an array of Strings.disconnect()
Upload files by sending multipart request programmatically
MultipartUtility(String requestURL, String charset)
: creates a new instance of this class for a given request URL and charset.void addFormField(String name, String value)
: adds a regular text field to the request.void addHeaderField(String name, String value)
: adds an HTTP header field to the request.void addFilePart(String fieldName, File uploadFile)
: attach a file to be uploaded to the request.List<String> finish()
: this method must be invoked lastly to complete the request and receive response from server as a list of String.
Java InetAddress Examples
getByName(String host)
: creates an InetAddress object based on the provided hostname.getByAddress(byte[] addr)
: returns an InetAddress object from a byte array of the raw IP address.getAllByName(String host)
: returns an array of InetAddress objects from the specified hostname, as a hostname can be associated with several IP addresses.getLocalHost()
: returns the address of the localhost.getHostAddress()
: returns the IP address in text.getHostname()
: gets the hostname.
Java Socket Client Examples (TCP/IP)
The client initiates connection to a server specified by hostname/IP address and port number.
Send data to the server using an
OutputStream
.Read data from the server using an
InputStream
.Close the connection.
Java Socket Server Examples (TCP/IP)
Create a server socket and bind it to a specific port number
Listen for a connection from the client and accept it. This results in a client socket is created for the connection.
Read data from the client via an
InputStream
obtained from the client socket.Send data to the client via the client socket’s
OutputStream
.Close the connection with the client.
Java UDP Client Server Program Example
send(DatagramPacket p)
: sends a datagram packet.receive(DatagramPacket p)
: receives a datagram packet.setSoTimeout(int timeout)
: sets timeout in milliseconds, limiting the waiting time when receiving data. If the timeout expires, aSocketTimeoutException
is raised.close()
: closes the socket.