網(wǎng)絡(luò)通信需要的東西:IP地址设联、端口號镜雨、協(xié)議
端口號:范圍0-65535,0-1023為系統(tǒng)保留
IP地址+端口號=socket
一当叭、InetAddress
static:
InetAddress.getLocalHost()
InetAddress.getByName()
方法:
address.getHostName()
address.getHostAddress()
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
public class Test1 {
public static void main(String[] arg) throws UnknownHostException {
//獲取InetAddress實例
InetAddress address=InetAddress.getLocalHost();
//獲取計算機名
System.out.println("計算機名:"+address.getHostName());
//獲取IP地址
System.out.println("IP地址:"+address.getHostAddress());
//字節(jié)數(shù)組形式IP地址
byte[] bytes=address.getAddress();
System.out.println("字節(jié)數(shù)組形式IP地址:"+Arrays.toString(bytes));
//直接打印
System.out.println(address);
//根據(jù)機器名獲取InetAddress實例
InetAddress address2=InetAddress.getByName("DESKTOP-A4T5UT1");
System.out.println("計算機名:"+address2.getHostName());
System.out.println("IP地址:"+address2.getHostAddress());
//根據(jù)IP獲取InetAddress實例
InetAddress address3=InetAddress.getByName("192.168.1.108");
System.out.println("計算機名:"+address3.getHostName());
System.out.println("IP地址:"+address3.getHostAddress());
}
}
輸出:
二熙揍、URL
方法:
url.getProtocol()
url.getHost()
url.getPort()
url.getPath()
url.getRef()
url.getQuery()
import java.net.MalformedURLException;
import java.net.URL;
public class Test1 {
public static void main(String[] arg) {
try {
//創(chuàng)建URL實例
URL imooc=new URL("http://www.imooc.com/");
//?后面表示參數(shù)职祷,#后面表示錨點
URL url=new URL(imooc,"/index.html?username=tom#test");
System.out.println("協(xié)議:"+url.getProtocol());
System.out.println("主機:"+url.getHost());
//未指定端口號,使用默認(rèn)端口號80届囚,返回值為-1
System.out.println("端口:"+url.getPort());
System.out.println("文件路徑:"+url.getPath());
System.out.println("文件名稱:"+url.getRef());
System.out.println("查詢字符串:"+url.getQuery());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
輸出:
拉取網(wǎng)頁:
import java.net.MalformedURLException;
import java.net.URL;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class Test1 {
public static void main(String[] arg) {
try {
//創(chuàng)建URL實例
URL url=new URL("http://www.baidu.com/");
//通過URl的openStream方法獲取URL對象表示的資源的字節(jié)輸入流
InputStream is=url.openStream();
//將字節(jié)輸入流轉(zhuǎn)換為字符輸入流
InputStreamReader isr=new InputStreamReader(is,"utf-8");
//為字符輸入流添加緩沖
BufferedReader br=new BufferedReader(isr);
//讀取數(shù)據(jù)
String data =br.readLine();
while(data!=null)
{
System.out.println(data);
data=br.readLine();
}
br.close();
isr.close();
is.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
輸出:
為網(wǎng)頁的html
三有梆、socket
服務(wù)端:
public static void main(String[] arg) {
try{
//1.創(chuàng)建一個服務(wù)器端的Socket,即ServerSocket,指定綁定的端口
ServerSocket ss= new ServerSocket(8880);
//2.調(diào)用accept方法開始監(jiān)聽意系,等待客戶端的連接
System.out.println("服務(wù)器即將啟動泥耀,等待客戶端的連接...");
Socket so=ss.accept();//accept方法返回Socket實例
//3.獲取一個輸入流,并讀取客戶端信息
InputStream is=so.getInputStream();//字節(jié)輸入流
InputStreamReader isr=new InputStreamReader(is);//將字節(jié)輸入流包裝成字符輸入流
BufferedReader br=new BufferedReader(isr);//加上緩沖流蛔添,提高效率
String info=null;
while((info=br.readLine())!=null){//循環(huán)讀取客戶端信息
System.out.println("我是服務(wù)器痰催,客戶端說:"+info);
}
so.shutdownInput();//關(guān)閉輸入流
//4.獲取一個輸出流,向客戶端輸出信息,響應(yīng)客戶端的請求
OutputStream os=(OutputStream) so.getOutputStream();//字節(jié)輸出流
PrintWriter pw=new PrintWriter(os);//字符輸出流
BufferedWriter bw=new BufferedWriter(pw);//緩沖輸出流
bw.write("歡迎您迎瞧!");
bw.newLine();
bw.flush();
//5.關(guān)閉資源
os.close();
pw.close();
bw.close();
br.close();
isr.close();
is.close();
so.close();
ss.close();
}catch(IOException e){
e.printStackTrace();
}
}
客戶端:
public static void main(String arg[]) throws IOException{
//1.創(chuàng)建客戶端Socket夸溶,指定服務(wù)器地址和端口
Socket so=new Socket("localhost", 8880);//端口號要和服務(wù)器端相同
//2.獲取輸出流,向服務(wù)器端發(fā)送登錄的信息
OutputStream os=(OutputStream) so.getOutputStream();//字節(jié)輸出流
PrintWriter pw=new PrintWriter(os);//字符輸出流
BufferedWriter bw=new BufferedWriter(pw);//加上緩沖流
bw.write("用戶名:admin;密碼:123");
bw.flush();
so.shutdownOutput();//關(guān)閉輸出流
//3.獲取輸入流夹攒,得到服務(wù)端的響應(yīng)信息
InputStream is=(InputStream) so.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
String info=null;
while((info=br.readLine())!=null){
System.out.println("我是客戶端蜘醋,服務(wù)器說:"+info);
}
//4.關(guān)閉資源
bw.close();
pw.close();
os.close();
so.close();
}