Java Network - Programming Examples
Learn how to modify networks in Java programming. Here are most commonly used examples:
1. How to change the host name to its specific IP address?
2. How to get connected with web server?
3. How to check a file is modified at a server or not?
4. How to create a multithreaded server?
5. How to get the file size from the server?
6. How to make a socket displaying message to a single client?
7. How to make a srever to allow the connection to the socket 6123?
8. How to get the parts of an URL?
9. How to get the date of URL connection?
10. How to read and download a webpage?
11. How to find hostname from IP Address?
12. How to determine IP Address & hostname of Local Computer?
13. How to check whether a port is being used or not?
14. How to find proxy settings of a System?
15. How to create a socket at a specific port?
How to split a string into a number of substrings ?
Solution:
Following example shows how to change the host name to its specific IP address with the help of
[Link]() method of [Link] class.
import [Link];
import [Link];
public class GetIP {
public static void main(String[] args) {
InetAddress address = null;
try {
address = [Link]
("[Link]");
}
catch (UnknownHostException e) {
[Link](2);
}
[Link]([Link]() + "="
+ [Link]());
[Link](0);
}
}
Result:
The above code sample will produce the following result.
Page 88
[Link] = [Link]
How to get connected with web server?
Solution:
Following example demonstrates how to get connected with web server by using [Link]()
method of [Link] class.
import [Link];
import [Link];
public class WebPing {
public static void main(String[] args) {
try {
InetAddress addr;
Socket sock = new Socket("[Link]",
80); addr = [Link]();
[Link]("Connected to " + addr);
[Link]();
} catch ([Link] e) {
[Link]("Can't connect to " +
args[0]); [Link](e);
}
}
}
Result:
The above code sample will produce the following result.
Connected to [Link]
How to check a file is modified at a server or not?
Solution:
Following example shows How to check a file is modified at a server or not.
import [Link];
import [Link];
public class Main {
public static void main(String[] argv)
throws Exception {
URL u = new URL("[Link]
URLConnection uc = [Link]();
[Link](false);
long timestamp = [Link]();
Page 89
[Link]("The last modification
time of [Link] is :"+timestamp);
}
}
Result:
The above code sample will produce the following result.
The last modification time of [Link] is :24 May 2009 12:14:50
How to create a multithreaded server?
Solution:
Following example demonstrates how to create a multithreaded server by using [Link]() method
of Socket class and MultiThreadServer(socketname) method of ServerSocket class.
import [Link];
import [Link];
import [Link];
import [Link];
public class MultiThreadServer implements Runnable
{ Socket csocket;
MultiThreadServer(Socket csocket) {
[Link] = csocket;
}
public static void main(String args[])
throws Exception {
ServerSocket ssock = new ServerSocket(1234);
[Link]("Listening");
while (true) {
Socket sock = [Link]();
[Link]("Connected");
new Thread(new MultiThreadServer(sock)).start();
}
}
public void run() {
try {
PrintStream pstream = new PrintStream
([Link]());
for (int i = 100; i >= 0; i--) {
[Link](i +
" bottles of beer on the wall");
}
[Link]();
[Link]();
}
catch (IOException e) {
[Link](e);
}
}
}
Page 90
Result:
The above code sample will produce the following result.
Listening
Connected
How to get the file size from the server?
Solution:
Following example demonstrates How to get the file size from the server.
import [Link];
import [Link];
public class Main {
public static void main(String[] argv)
throws Exception {
int size;
URL url = new URL("[Link]
URLConnection conn = [Link]();
size = [Link]();
if (size < 0)
[Link]("Could not determine file
size."); else
[Link]("The size of the file is = "
+size + "bytes");
[Link]().close();
}
}
Result:
The above code sample will produce the following result.
The size of The file is = 16384 bytes
How to make a socket displaying message to a single client?
Solution:
Following example demonstrates how to make a socket displaying message to a single client with the help
of [Link]() method of Socket class.
import [Link];
import [Link];
import [Link];
Page 91
public class BeerServer {
public static void main(String args[])
throws Exception {
ServerSocket ssock = new ServerSocket(1234);
[Link]("Listening");
Socket sock = [Link]();
[Link]();
PrintStream ps = new PrintStream
([Link]());
for (int i = 10; i >= 0; i--) {
[Link](i +
" from Java Source and Support.");
}
[Link]();
[Link]();
}
}
Result:
The above code sample will produce the following result.
Listening
10 from Java Source and Support
9 from Java Source and Support
8 from Java Source and Support
7 from Java Source and Support
6 from Java Source and Support
5 from Java Source and Support
4 from Java Source and Support
3 from Java Source and Support
2 from Java Source and Support
1 from Java Source and Support
0 from Java Source and Support
How to make a srever to allow the connection to the socket 6123?
Solution:
Following example shows how to make a srever to allow the connection to the socket 6123 by using
[Link]() method of ServerSocket class andb [Link]() method of Socket class.
import [Link];
import [Link];
import [Link];
import [Link];
public class SocketDemo {
public static void main(String[] args) {
try {
ServerSocket server = new
ServerSocket(6123); while (true) {
[Link]("Listening");
Socket sock = [Link]();
InetAddress addr = [Link]();
[Link]("Connection made to "
Page 92
+ [Link]()
+ " (" + [Link]() +
")"); pause(5000);
[Link]();
}
}
catch (IOException e) {
[Link]("Exception detected: " + e);
}
}
private static void pause(int ms) {
try {
[Link](ms);
}
catch (InterruptedException e) {
}
}
}
Result:
The above code sample will produce the following result.
Listening
Terminate batch job (Y/N)? n
Connection made to [Link]
How to get the parts of an URL?
Solution:
Following example shows how to get the parts of an URL with the help of [Link]() ,[Link]()
method etc. of [Link] class.
import [Link];
public class Main {
public static void main(String[] args)
throws Exception {
URL url = new URL(args[0]);
[Link]("URL is " +
[Link]()); [Link]("protocol
is " + [Link]());
[Link]("file name is " + [Link]());
[Link]("host is " + [Link]());
[Link]("path is " + [Link]());
[Link]("port is " + [Link]());
[Link]("default port is "
+ [Link]());
}
}
Result:
The above code sample will produce the following result.
Page 93
URL is [Link]
protocol is TCP/IP
file name is java_program.txt
host is [Link]
path is
port is 2
default port is 1
How to get the date of URL connection?
Solution:
Following example demonstrates how to get the date of URL connection by using [Link]() method
of HttpURLConnection class.
import [Link];
import [Link];
import [Link];
public class Main{
public static void main(String args[])
throws Exception {
URL url = new URL("[Link]
HttpURLConnection httpCon =
(HttpURLConnection) [Link]();
long date = [Link]();
if (date == 0)
[Link]("No date information.");
else
[Link]("Date: " + new Date(date));
}
}
Result:
The above code sample will produce the following result.
Date:05.26.2009
How to read and download a webpage?
Solution:
Following example shows how to read and download a webpage URL() constructer of [Link] class.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Page 94
public class Main {
public static void main(String[] args)
throws Exception {
URL url = new URL("[Link]
BufferedReader reader = new BufferedReader
(new InputStreamReader([Link]()));
BufferedWriter writer = new BufferedWriter
(new FileWriter("[Link]"));
String line;
while ((line = [Link]()) != null) {
[Link](line);
[Link](line);
[Link]();
}
[Link]();
[Link]();
}
}
Result:
The above code sample will produce the following result.
Welcome to Java Tutorial
Here we have plenty of examples for you!
Come and Explore Java!
How to find hostname from IP Address?
Solution:
Following example shows how to change the host name to its specific IP address with the help
of [Link]() method of [Link] class.
import [Link];
public class Main {
public static void main(String[] argv) throws Exception {
InetAddress addr = [Link]("[Link]");
[Link]("Host name is: "+[Link]());
[Link]("Ip address is: "+ [Link]());
}
}
Result:
The above code sample will produce the following result.
[Link] = [Link]
Page 95
How to determine IP Address & hostname of Local Computer?
Solution:
Following example shows how to find local IP Address & Hostname of the system using getLocalAddress()
method of InetAddress class.
import [Link];
public class Main {
public static void main(String[] args)
throws Exception {
InetAddress addr = [Link]();
[Link]("Local HostAddress:
"+[Link]());
String hostname = [Link]();
[Link]("Local host name: "+hostname);
}
}
Result:
The above code sample will produce the following result.
Local HostAddress: [Link]
Local host name: saikat
How to check whether a port is being used or not?
Solution:
Following example shows how to check whether any port is being used as a server or not by creating a
socket object.
import [Link].*;
import [Link].*;
public class Main {
public static void main(String[] args) {
Socket Skt;
String host = "localhost";
if ([Link] > 0) {
host = args[0];
}
for (int i = 0; i < 1024; i++) {
try {
[Link]("Looking for "+ i);
Skt = new Socket(host, i);
[Link]("There is a server on port "
+ i + " of " + host);
Page 96
}
catch (UnknownHostException e) {
[Link]("Exception occured"+
e); break;
}
catch (IOException e) {
}
}
}
}
Result:
The above code sample will produce the following result.
Looking for 0
Looking for 1
Looking for 2
Looking for 3
Looking for 4. . .
How to find proxy settings of a System?
Solution:
Following example shows how to find proxy settings & create a proxy connection on a system using
put method of systemSetting & getResponse method of HttpURLConnection class.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class Main{
public static void main(String s[])
throws Exception{
try {
Properties systemSettings =
[Link]();
[Link]("proxySet", "true");
[Link]("[Link]",
"[Link]");
[Link]("[Link]", "80");
URL u = new URL("[Link]
HttpURLConnection con = (HttpURLConnection)
[Link]();
[Link]([Link]() +
" : " + [Link]());
[Link]([Link]()
== HttpURLConnection.HTTP_OK);
}
catch (Exception e) {
[Link]();
Page 97
[Link](false);
}
[Link]("[Link]",
"true");
Proxy proxy = (Proxy) [Link]().
select(new
URI("[Link] next();;
[Link]("proxy hostname : " +
[Link]()); InetSocketAddress addr =
(InetSocketAddress) [Link]();
if (addr == null) {
[Link]("No Proxy");
}
else {
[Link]("proxy hostname : "
+ [Link]());
[Link]("proxy port : "
+ [Link]());
}
}
}
Result:
The above code sample will produce the following result.
200 : OK
true
proxy hostname : HTTP
proxy hostname : [Link]
proxy port : 80
How to create a socket at a specific port?
Solution:
Following example shows how to sing Socket constructor of Socket [Link] also get Socket details
using getLocalPort() getLocalAddress , getInetAddress() & getPort() methods.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
try {
InetAddress addr =
[Link]("[Link]");
Socket theSocket = new Socket(addr, 80);
[Link]("Connected to "
+ [Link]()
+ " on port " + [Link]() + " from port "
+ [Link]() + " of "
+ [Link]());
Page 98
}
catch (UnknownHostException e) {
[Link]("I can't find " + e );
}
catch (SocketException e) {
[Link]("Could not connect
to " +e );
}
catch (IOException e) {
[Link](e);
}
}
}
Result:
The above code sample will produce the following result.
Connected to /[Link] on port
80 from port 2857 of /[Link]