Day08_網路程式設計(上)
阿新 • • 發佈:2020-07-21
Day08_網路程式設計(上)
javaWeb:網頁程式設計,B/S
網路程式設計:TCP/IP,C/S
網路程式設計中兩個主要的問題
- 如何準確的定位到網路上一臺或者多臺主機
- 找到主機後如何進行通訊
網路程式設計中的要素
- IP和埠號
- 網路通訊協議 UDP,TCP
IP
IP地址:InetAddress
- 唯一定位一臺網路上計算機
- 127.0.0.1 :本機localhost
- IP地址分類:
- IPv4,IPv6
- 公網,私網
InetAddress
import java.net.InetAddress; import java.net.UnknownHostException; public class testInetAddress { public static void main(String[] args) { try { //查詢本地地址 InetAddress inetAddress1= InetAddress.getByName("127.0.0.1"); System.out.println(inetAddress1); InetAddress inetAddress2= InetAddress.getByName("localhost"); System.out.println(inetAddress2); InetAddress inetAddress3= InetAddress.getLocalHost(); System.out.println(inetAddress3); System.out.println("===================="); //查詢網站IP地址 InetAddress inetAddress4= InetAddress.getByName("www.baidu.com"); System.out.println(inetAddress4); //常用方法 System.out.println(inetAddress4.getCanonicalHostName());//規範的名字 System.out.println(inetAddress4.getHostAddress());//ip System.out.println(inetAddress4.getHostName());//域名,或者自己電腦的名字 } catch (UnknownHostException e) { e.printStackTrace(); } } }
輸出:
/127.0.0.1
localhost/127.0.0.1
bogon/192.168.1.51
====================
www.baidu.com/111.206.223.173
111.206.223.173
111.206.223.173
www.baidu.com
埠
埠表示計算機上的一個程式的程序;
-
不同的程序有不同的埠號,用來區分軟體
-
被規定0-65535
-
TCP,UDP每個協議都有65535個埠可以同時用,故同時可以呼叫65535*2個埠。 例如:tcp和udp可以同時用80埠。但是單個協議下埠不能衝突。
-
埠分類
- 公有埠1-1023,一般會被內建程序或者伺服器使用。
- http:80
- https:43
- FTP:21
- Telnet:23
- 程式註冊埠:1024-49151,分配給使用者或者程式
- tomcat:8080
- MySQL:3306
- Oracle:1521
- 動態、私有:49152-65535
InetSocketAddress類
import java.net.InetSocketAddress; public class testInetSocketAddress { public static void main(String[] args) { InetSocketAddress socketAddress= new InetSocketAddress("127.0.0.1",8080); InetSocketAddress socketAddress2= new InetSocketAddress("localhost",8080); System.out.println(socketAddress); System.out.println(socketAddress2); System.out.println(socketAddress.getAddress()); System.out.println(socketAddress.getHostName()); System.out.println(socketAddress.getPort()); } }
輸出:
/127.0.0.1:8080 localhost/127.0.0.1:8080 /127.0.0.1 localhost 8080
- 公有埠1-1023,一般會被內建程序或者伺服器使用。
通訊協議
TCP:使用者傳輸協議
UDP:使用者資料報協議
IP:網路互聯協議
TCP和IP協議實際是一組協議
TCP:
- 面向連線,穩定
三次握手
,四次揮手
- 客戶端,服務端
- 傳輸完成,釋放連線,效率低
UDP
- 無連線,不穩定
- 客戶端,服務端:沒有明顯的界限
- 不管有沒有準備好,都可以發給你
- DDos:洪水攻擊!(飽和攻擊)
TCP
伺服器
- 建立服務的埠ServerSocket
- 等待使用者的連線 accept
- 接受用的訊息
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class tcpServerDemo01 {
public static void main(String[] args) {
ServerSocket serverSocket=null;
Socket socket=null;
InputStream is=null;
ByteArrayOutputStream dataFrame=null;
try {
//1.服務端提供IP地址和埠
serverSocket=new ServerSocket(9999);
while(true){
//2.等待客戶端連線
socket=serverSocket.accept();
//3.讀取客戶端的訊息
is=socket.getInputStream();
//管道流
dataFrame=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len;
while((len=is.read(buffer))!=-1){
dataFrame.write(buffer,0,len);
}
System.out.println(dataFrame.toString());
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if (dataFrame!=null){
try {
dataFrame.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(serverSocket!=null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
客戶端
- 連線伺服器Socket
- 傳送訊息
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class tcpClientDemo01 {
public static void main(String[] args) throws IOException {
Socket socket=null;
OutputStream os=null;
try {
//1.要知道服務端的IP地址和埠號
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port=9999;
//2.建立一個Socket連線
socket=new Socket(serverIP,port);
//3.傳送訊息IO流
os=socket.getOutputStream();
os.write("你好,我是客戶端!".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服務端輸出:
你好,我是客戶端!
之前沒看IO流的使用,現在沒搞懂IO流,明天去補IO流的課!