java 中ip對象:InetAddress.
import java.net.*;
class IPDemo{
public static void main(String[] args) throws UnknownHostException{
//通過名稱(ip字符串or主機名)來獲取一個ip對象。
InetAddress ip = InetAddress.getByName(“www.baidu.com”);//java.net.UnknownHostException
System.out.println(“addr:”+ip.getHostAddress());
System.out.println(“name:”+ip.getHostName());
}
}
UDP傳輸
:
java語言應該將udp封裝成對象,易於我們的使用,這個對象就是DatagramSocket. 封裝了udp傳輸協議的socket對象。
因爲數據包中包含的信息較多,爲了操作這些信息方便,也一樣會將其封裝成對象。這個數據包對象就是:DatagramPacket.通過這個對象中的方法,就可以獲取到數據包中的各種信息。
DatagramSocket具備發送和接受功能,在進行udp傳輸時,需要明確一個是發送端,一個是接收端。
udp的發送端:
1,建立udp的socket服務,創建對象時如果沒有明確端口,系統會自動分配一個未被使用的端口。
2,明確要發送的具體數據。
3,將數據封裝成了數據包。
4,用socket服務的send方法將數據包發送出去。
5,關閉資源
import java.net.*;
class UdpSend{
public static void main(String[] args)throws Exception {
// 1,建立udp的socket服務。
DatagramSocket ds = new DatagramSocket(8888);//指定發送端口,不指定系統會隨機分配。
// 2,明確要發送的具體數據。
String text = "udp傳輸演示 哥們來了";
byte[] buf = text.getBytes();
// 3,將數據封裝成了數據包。
DatagramPacket dp = new DatagramPacket(buf,
buf.length,InetAddress.getByName("10.1.31.127"),10000);
// 4,用socket服務的send方法將數據包發送出去。
ds.send(dp);
// 5,關閉資源。
ds.close();
}
}
udp的接收端:
1,創建udp的socket服務,必須要明確一個端口,作用在於,只有發送到這個端口的數據纔是這個接收端可以處理的數據。
2,定義數據包,用於存儲接收到數據。
3,通過socket服務的接收方法將收到的數據存儲到數據包中。
4,通過數據包的方法獲取數據包中的具體數據內容,比如ip、端口、數據等等。
5,關閉資源。
class UdpRece {
public static void main(String[] args) throws Exception{
// 1,創建udp的socket服務。
DatagramSocket ds = new DatagramSocket(10000);
// 2,定義數據包,用於存儲接收到數據。先定義字節數組,數據包會把數據存儲到字節數組中。
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
// 3,通過socket服務的接收方法將收到的數據存儲到數據包中。
ds.receive(dp);//該方法是阻塞式方法。
// 4,通過數據包的方法獲取數據包中的具體數據內容,比如ip,端口,數據等等。
String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();
String text = new String(dp.getData(),0,dp.getLength());//將字節數組中的有效部分轉成字符串。
System.out.println(ip+":"+port+"--"+text);
// 5,關閉資源。
ds.close();
}
}
TCP傳輸
兩個端點的建立連接後會有一個傳輸數據的通道,這通道稱爲流,而且是建立在網絡基礎上的流,稱之爲socket流。該流中既有讀取,也有寫入。
tcp的兩個端點:一個是客戶端,一個是服務端。
客戶端:對應的對象,Socket
服務端:對應的對象,ServerSocket
TCP客戶端:
1,建立tcp的socket服務,最好明確具體的地址和端口。這個對象在創建時,就已經可以對指定ip和端口進行連接(三次握手)。
2,如果連接成功,就意味着通道建立了,socket流就已經產生了。只要獲取到socket流中的讀取流和寫入流即可,只要通過getInputStream和getOutputStream就可以獲取兩個流對象。
3,關閉資源。
import java.net.*;
import java.io.*;
//需求:客戶端給服務器端發送一個數據。
class TcpClient{
public static void main(String[] args) throws Exception{
Socket s = new Socket("10.1.31.69",10002);
OutputStream out = s.getOutputStream();//獲取了socket流中的輸出流對象。
out.write("tcp演示,哥們又來了!".getBytes());
s.close();
}
}
TCP服務端:
1,創建服務端socket服務,並監聽一個端口。
2,服務端爲了給客戶端提供服務,獲取客戶端的內容,可以通過accept方法獲取連接過來的客戶端對象。
3,可以通過獲取到的socket對象中的socket流和具體的客戶端進行通訊。
4,如果通訊結束,關閉資源。注意:要先關客戶端,再關服務端。
class TcpServer{
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(10002);//建立服務端的socket服務
Socket s = ss.accept();//獲取客戶端對象
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+".....connected");
// 可以通過獲取到的socket對象中的socket流和具體的客戶端進行通訊。
InputStream in = s.getInputStream();//讀取客戶端的數據,使用客戶端對象的socket讀取流
byte[] buf = new byte[1024];
int len = in.read(buf);
String text = new String(buf,0,len);
System.out.println(text);
// 如果通訊結束,關閉資源。注意:要先關客戶端,在關服務端。
s.close();
ss.close();
}
}
實例
將一個文件上傳到服務器
客戶端:
File file = new File("c:\\client.txt");
System.out.println(file.exists());
Socket s = new Socket("192.168.1.100",10005);
BufferedReader bufr =new BufferedReader(new FileReader(file));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
String line = null;
while((line=bufr.readLine())!=null){
out.println(line);
}
s.shutdownOutput();//告訴服務器文件傳輸完畢,否則服務器一直等待接受數據
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = bufIn.readLine();
System.out.println(str);
bufr.close();
s.close();
服務器端:
ServerSocket ss = new ServerSocket(10005);
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress()+".....connected");
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bufw = new BufferedWriter(new FileWriter("c:\\server.txt"));
String line = null;
while((line=bufIn.readLine())!=null){
bufw.write(line);
bufw.newLine();
bufw.flush();
}
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
out.println("上傳成功");
bufw.close();
s.close();
ss.close();