1、Java 實(shí)例 – 獲取指定主機(jī)的IP地址:使用 InetAddress 類的 InetAddress.getByName() 方法來(lái)獲取指定主機(jī)(網(wǎng)址)的IP地址办绝。
public class Net_GetIP {
public Net_GetIP() {
InetAddress address =null;
try {
address = InetAddress.getByName("www.baidu.com");
}
catch (UnknownHostException e) {
System.exit(2);
}
System.out.println(address.getHostName() +"的IP地址wei:" + address.getHostAddress());
System.exit(0);
}
}
執(zhí)行結(jié)果:
2、Java 實(shí)例 – 查看端口是否已使用:
public class Net_PortCheck {
public static void main(String[] args) {
Socket Skt;
String host ="localhost";
if (args.length >0) {
host = args[0];
}
for (int i =0; i <6; i++) {
try {
System.out.println("查看 "+ i +"未使用");
Skt =new Socket(host, i);
System.out.println("端口 " + i +" 已被使用");
}
catch (UnknownHostException e) {
System.out.println("Exception occured"+ e);
break;
}
catch (IOException e) {
}
}
}
}
執(zhí)行結(jié)果:
3艘希、Java 實(shí)例 – 獲取本機(jī)ip地址及主機(jī)名:使用 InetAddress 類的 getLocalAddress() 方法獲取本機(jī)ip地址及主機(jī)名贝攒。
public class Net_GetLocal {
public Net_GetLocal()throws Exception {
InetAddress addr = InetAddress.getLocalHost();
System.out.println("Local HostAddress: "+addr.getHostAddress());
String hostname = addr.getHostName();
System.out.println("Local host name: "+hostname);
}
}
執(zhí)行結(jié)果:
4锌杀、Java 實(shí)例 – 獲取遠(yuǎn)程文件大小:
public class Net_GetFileSize {
public Net_GetFileSize()throws Exception {
int size;
URL url =new URL("http://photocdn.sohu.com/20111207/Img328215620.jpg");
URLConnection conn = url.openConnection();
size = conn.getContentLength();
if (size <0)
System.out.println("無(wú)法獲取文件大小逝钥。");
else
? ? ? ? ? ? System.out.println("文件大小為:" + size +" bytes");
conn.getInputStream().close();
}
}
執(zhí)行結(jié)果:
5屑那、Java 實(shí)例 – Socket 實(shí)現(xiàn)多線程服務(wù)器程序:使用 Socket 類的 accept() 方法和 ServerSocket 類的 MultiThreadServer(socketname) 方法來(lái)實(shí)現(xiàn)多線程服務(wù)器程序。
public class Net_MultiThreadServerextends Thread{
Socketcsocket;
Net_MultiThreadServer(Socket csocket) {
this.csocket = csocket;
}
public static void main(String args[])
throws Exception {
ServerSocket ssock =new ServerSocket(1234);
System.out.println("Listening");
while (true) {
Socket sock = ssock.accept();
System.out.println("Connected");
new Thread(new Net_MultiThreadServer(sock)).start();
}
}
public void run() {
try {
PrintStream pstream =new PrintStream
(csocket.getOutputStream());
for (int i =100; i >=0; i--) {
pstream.println(i +
" bottles of beer on the wall");
}
pstream.close();
csocket.close();
}
catch (IOException e) {
System.out.println(e);
}
}
}
執(zhí)行結(jié)果:
6艘款、Java 實(shí)例 – 查看主機(jī)指定文件的最后修改時(shí)間:
public class Net_FileTime {
public Net_FileTime()throws Exception {
URL u =new URL("http://127.0.0.1/test/test.html");
URLConnection uc = u.openConnection();
SimpleDateFormat ft =new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
uc.setUseCaches(false);
long timestamp = uc.getLastModified();
System.out.println("test.html 文件最后修改時(shí)間 :" + ft.format(new Date(timestamp)));
}
}
執(zhí)行結(jié)果:
7持际、Java 實(shí)例 – 使用 Socket 連接到指定主機(jī):使用 net.Socket 類的 getInetAddress() 方法來(lái)連接到指定主機(jī)。
public class Net_Ping {
public static void main(String[] args) {
try {
InetAddress addr;
Socket sock =new Socket("www.baidu.com",80);
addr = sock.getInetAddress();
System.out.println("連接到 " + addr);
sock.close();
}catch (java.io.IOException e) {
System.out.println("無(wú)法連接 " + args[0]);
System.out.println(e);
}
}
}
執(zhí)行結(jié)果:
8哗咆、Java 實(shí)例 – 網(wǎng)頁(yè)抓戎┯:使用 net.URL 類的 URL() 構(gòu)造函數(shù)來(lái)抓取網(wǎng)頁(yè)。
public class Net_GetWeb {
public Net_GetWeb()throws Exception {
URL url =new URL("http://www.baidu.com");
BufferedReader reader =new BufferedReader
(new InputStreamReader(url.openStream()));
BufferedWriter writer =new BufferedWriter
(new FileWriter("data.html"));
String line;
while ((line = reader.readLine()) !=null) {
System.out.println(line);
writer.write(line);
writer.newLine();
}
reader.close();
writer.close();
}
}
執(zhí)行結(jié)果:
9晌柬、Java 實(shí)例 – 獲取 URL響應(yīng)頭的日期信息:使用 HttpURLConnection 的 httpCon.getDate() 方法來(lái)獲取 URL響應(yīng)頭的日期信息姥份。
public class Net_URLGetDate {
public Net_URLGetDate()throws Exception {
URL url =new URL("http://www.runoob.com");
HttpURLConnection httpCon =
(HttpURLConnection) url.openConnection();
long date = httpCon.getDate();
if (date ==0)
System.out.println("無(wú)法獲取信息郭脂。");
else
? ? ? ? ? ? System.out.println("Date: " +new Date(date));
}
}
執(zhí)行結(jié)果:
10、Java 實(shí)例 – 獲取 URL 響應(yīng)頭信息:
public class Net_URLCon {
public Net_URLCon()throws IOException {
URL url =new URL("http://www.baidu.com");
URLConnection conn = url.openConnection();
Map headers = conn.getHeaderFields();
Set keys = headers.keySet();
for( String key : keys ){
String val = conn.getHeaderField(key);
System.out.println(key+"? ? "+val);
}
System.out.println( conn.getLastModified() );
}
}
執(zhí)行結(jié)果:
11澈歉、Java 實(shí)例 – 解析 URL:使用 net.URL 類的 url.getProtocol() ,url.getFile() 等方法來(lái)解析 URL 地址展鸡。
public class Net_URLAnalyse {
public Net_URLAnalyse()throws Exception {
URL url =new URL("http://www.baidu.com");
System.out.println("URL 是 " + url.toString());
System.out.println("協(xié)議是 " + url.getProtocol());
System.out.println("文件名是 " + url.getFile());
System.out.println("主機(jī)是 " + url.getHost());
System.out.println("路徑是 " + url.getPath());
System.out.println("端口號(hào)是 " + url.getPort());
System.out.println("默認(rèn)端口號(hào)是 "
? ? ? ? ? ? ? ? + url.getDefaultPort());
}
}
執(zhí)行結(jié)果:
12、Java 實(shí)例 – ServerSocket 和 Socket 通信實(shí)例:
public class Server {
public static void main(String[] args) {
try {
ServerSocket ss =new ServerSocket(8888);
System.out.println("啟動(dòng)服務(wù)器....");
Socket s = ss.accept();
System.out.println("客戶端:"+s.getInetAddress().getLocalHost()+"已連接到服務(wù)器");
BufferedReader br =new BufferedReader(new InputStreamReader(s.getInputStream()));
//讀取客戶端發(fā)送來(lái)的消息
? ? ? ? ? ? String mess = br.readLine();
System.out.println("客戶端:"+mess);
BufferedWriter bw =new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bw.write(mess+"\n");
bw.flush();
}catch (IOException e) {
e.printStackTrace();
}
}
}
······················································································
public class Client {
public static void main(String[] args) {
try {
Socket s =new Socket("127.0.0.1",8888);
//構(gòu)建IO
? ? ? ? ? ? InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
BufferedWriter bw =new BufferedWriter(new OutputStreamWriter(os));
//向服務(wù)器端發(fā)送一條消息
? ? ? ? ? ? bw.write("測(cè)試客戶端和服務(wù)器通信埃难,服務(wù)器接收到消息返回到客戶端\n");
bw.flush();
//讀取服務(wù)器返回的消息
? ? ? ? ? ? BufferedReader br =new BufferedReader(new InputStreamReader(is));
String mess = br.readLine();
System.out.println("服務(wù)器:"+mess);
}catch (UnknownHostException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}
執(zhí)行結(jié)果: