無網不歡
1、什麼是網絡編程?
書上是這麼描述的,網絡編程是指物理上位於兩臺計算機的兩個進程之間實現網絡通信的編程。其實就是計算機通過Java程序實現資源共享和信息傳遞。
網絡通信主要由三個要素組成:IP地址、端口號、協議。
/TCP/IP協議/
2、IP地址
IP地址用於標示某一天計算機,類似於計算機的身份證。
IP地址的組成:網絡號段+主機號段
A類:第一號段爲網絡號段+後三段的主機號段
B類:前二號段爲網絡號段+後二段的主機號段
C類:前三號段爲網絡號段+後一段的主機號段
IP地址的分類:
A類 1.0.0.1---127.255.255.254
(1)10.X.X.X是私有地址(私有地址就是在互聯網上不使用,而被用在局域網絡中的地址)
(2)127.X.X.X是保留地址,用做循環測試用的。
B類 128.0.0.1---191.255.255.254 172.16.0.0---172.31.255.255是私有地址。169.254.X.X是保留地址。
C類 192.0.0.1---223.255.255.254 192.168.X.X是私有地址
D類 224.0.0.1---239.255.255.254
E類 240.0.0.1---247.255.255.254
Java提供了InetAddress類來獲取IP相關內容,該類沒有構造方法。
常用方法:
// 在給定主機名的情況下確定主機的 IP地址
static InetAddress getByName(String host)
// 返回 IP地址字符串(以文本表現形式)
String getHostAddress()
// 獲取此 IP地址的主機名
String getHostName()
3、端口
端口號是標示計算機上某個應用程序的,也就是用於標識進程的邏輯地址。
有效端口:0-65535,0-1024系統使用或保留端口。
4、協議
1.UDP協議
無連接的傳輸協議,利用UDP協議進行數據傳輸時,先要將傳輸的數據定義成數據報,在數據報中指明數據所要到達的端點,然後再將數據報發送出去。無連接、速度快、不可靠。
2.TCP協議
端點之間建立連接,建立連接後發送端將發送的數據以字節流的方式發送出去;接收端則對數據按序列順序將數據整理好,數據在需要時可以重新發送。有連接、速度慢、可靠,三次握手協議。
##5、Socket編程
Socket套接字:網絡上具有唯一標識的IP地址和端口號組合在一起才能構成唯一能識別的標識符套接字。
1.UDP協議下的Socket編程
//發送端
public class UdpSend {
public static void main(String[] args) throws IOException {
// 生成發送端socket對象
DatagramSocket ds = new DatagramSocket();
// 設置socket包裹
// 設置發送端數據
String str = "hello world java";
byte[] c = str.getBytes();
DatagramPacket packet = new DatagramPacket(c, c.length,
InetAddress.getByName("127.0.0.1"), 10087);
//發送數據包
ds.send(packet);
ds.close();
}
}
//接收端
public class UdpReceive {
public static void main(String[] args) throws IOException{
// 創建接收端的Socket對象
DatagramSocket ds=new DatagramSocket(10087);
//設置接收的包裹
byte[] b=new byte[1024];
DatagramPacket packet=new DatagramPacket(b, b.length);
//接收數據
ds.receive(packet);
//解析數據
String line =new String (packet.getData());
//獲得發送端的IP
String ip=packet.getAddress().getHostAddress();
System.out.println(ip+"--->"+line);
//127.0.0.1--->hello world java
//釋放資源
ds.close();
}
}
2.TCP協議下的Socket編程
//發送端
public class TcpClient {
public static void main(String[] args) throws IOException{
//生成socket對象
Socket s=new Socket("127.0.0.1", 10086);
//封裝通道對象
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//設置發送的數據
String line="hello world";
bw.write(line);
bw.newLine();
bw.flush();
//禁用此套接字的輸出流
s.shutdownOutput();
//獲取服務端的反饋信息
InputStream stream=s.getInputStream();
byte[] b=new byte[1024];
stream.read(b);
String str=new String(b);
System.out.println(str);//上傳成功了!!!
//釋放資源
//bw.close();
s.close();
}
}
//接收端
public class TcpServer {
public static void main(String[] args) throws IOException{
//生成服務端socket對象
ServerSocket ss=new ServerSocket(10086);
//設置監聽事件
Socket s=ss.accept();//阻塞式
//封裝通道流
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String line=null;
while((line=br.readLine())!=null){
System.out.println(line);
//hello world
}
//向客戶端給出反饋
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bw.write("上傳成功了!!!");
bw.newLine();
bw.flush();
//釋放資源
s.close();
//ss.close();
}
}
6、URL編程
URL網絡資源定位符。
URL的基本表示格式:
protocol://hostnameport/resourcename#anchor
protocol:協議,http、ftp等
hostname:主機名
port:端口號
resourcename:資源名,主機上能訪問到的目錄或文件
anchor:錨點標記,指定文件中的特定標記位置
常用類:
URL:類URL代表一個統一資源定位符,它是指向互聯網“資源”的指針。
常用方法:
// 獲取此 URL的協議名稱
String getProtocol()
// 獲取此 URL的主機名
String getHost()
// 獲取此 URL的路徑部分
String getPath()
// 獲取此 URL的端口號
int getPort()
// 獲取此 URL的文件名
String getFile()
// 獲取此 URL的錨點
String getRef()
// 返回一個URLConnection對象,它表示到 URL所引用的遠程對象的連接
URLConnection openConnection()
// 打開到此URL的連接並返回一個用於從該連接讀入的InputStream
InputStream openStream()
URLConnection:抽象類,它代表應用程序和URL之間的通信鏈接。此類的實例可用於讀取和寫入此URL引用的資源。
常用方法:
// 將讀超時設置爲指定的超時值,以毫秒爲單位
void setReadTimeout(int timeout)
// 返回連接超時設置
int getConnectTimeout()
// 添加由鍵值對指定的一般請求屬性
void addRequestProperty(String key, String value)
// 返回寫入到此連接的輸出流。
OutputStream getOutputStream()
// 返回從此打開的連接讀取的輸入流
InputStream getInputStream()
try {
//網絡資源定位對象
URL url = new URL("http://www.baidu.com");
//打開連接 生成連接對象
URLConnection connection = url.openConnection();
//設置超時時間
connection.setConnectTimeout(5000);
//獲取輸入流
InputStream inputStream = connection.getInputStream();
//字節流轉成字符流 並設置編碼方式
InputStreamReader reader = new InputStreamReader(inputStream, "utf-8");
//高效流
BufferedReader bufferedReader = new BufferedReader(reader);
String str;
while ((str = bufferedReader.readLine()) != null) {
System.out.println(str);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
注:更多方法查看API