1.1概述
網絡編程的目的:
無限電台。。。。傳播交流信息,數據交換。通信
想要達到這個效果需要什么:
1.如何准確的定位網絡上的一台珠璣 ip地址 端口,定位到這個計算機上的某個資源
2.找到了這個珠璣,如何傳輸數據
Javaweb: 網頁編程 B/s
網絡編程:TCP/IP C/S
1.2網絡通信的要素
如何實現網絡的通信?
通信雙方地址:
-
IP
-
端口號
-
192.168.16.124::5900
-
規則:網絡通信的協議
TCP/IP
小結:
-
網絡編程中有兩個主要的問題
-
如何准確的定位到網絡上的一台或多台珠璣
-
找到主機之后如何進行通信
-
-
-
網絡編程中高端要素
-
IP和端口號 IP
-
網絡通信協議 udp tcp
-
-
萬物皆對象
1.3ip
ip地址:inteAddress 查看本地cmd ipconfig
-
唯一定位一台網絡上計算機
-
127.0.0.1:本機localhost
-
ip地址的分類
-
ipv4/Ipv6
-
ipv4 127.0.0.1 4個字節組成。 0~255 42億個
-
ipv6 fe80::60f8:dddd:6bea:17a7%5 ,128位。8個無符號整數!
-
-
-
try {
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);
InetAddress[] inetAddress1 = InetAddress.getAllByName("127.0.0.1");
System.out.println(inetAddress1);
System.out.println(InetAddress.getByName("localhost"));
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
1.4端口
netstat -ano #查看所有的窗口
netstat -ano|findstr"5900" #查看指定的端口
taskList|findstr "8696" #查看指定端口的進程
ctrl+shift+Esc
public static void main(String[] args) {
InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8080);
InetSocketAddress localhost = new InetSocketAddress("localhost", 8080);
System.out.println(inetSocketAddress);
System.out.println(localhost);
}
}
1.5通信協議
網絡通信協議:速率,傳輸碼率,代碼結構,傳輸控制....
TCP/IP協議簇:實際上是一組協議
重要:
-
TCP:用戶傳輸協議
-
UDP:用戶數據報協議
出名的協議:
-
TCP:
-
IP:網絡互連協議
-
1.6TCP
服務端
public static void main(String[] args) throws Exception {
//1.創建服務
ServerSocket serverSocket = new ServerSocket(9000);
//2.監聽客戶端的連接
Socket socket = serverSocket.accept();//阻塞式監聽,會一直等待客戶端連接
//3.獲取輸入流
InputStream is = socket.getInputStream();
//4.文件輸出
FileOutputStream fos = new FileOutputStream(new File("receive"));
byte[] buffer=new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
//關閉資源
fos.close();
is.close();
socket.close();
serverSocket.close();
}
客戶端
public static void main(String[] args) throws Exception {
//1.創建服務
ServerSocket serverSocket = new ServerSocket(9000);
//2.監聽客戶端的連接
Socket socket = serverSocket.accept();//阻塞式監聽,會一直等待客戶端連接
//3.獲取輸入流
InputStream is = socket.getInputStream();
//4.文件輸出
FileOutputStream fos = new FileOutputStream(new File("receive"));
byte[] buffer=new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
//同知服務端已經傳輸完了
socket.shutdownOutput();//傳輸完畢
//確定服務端接收完畢,斷開連接
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();//管道流
byte[] buffer2=new byte[1024];
int len2;
while ((len2=inputStream.read(buffer2))!=-1){
baos.write(buffer2,0,len2);
}
//關閉資源
baos.close();
inputStream.close();
fos.close();
is.close();
socket.close();
serverSocket.close();
}
服務端
blic static void main(String[] args) throws Exception {
//1.創建一個Socket連接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
//2.創建一個輸出流
OutputStream os = socket.getOutputStream();
//3.讀取文件
FileInputStream fis = new FileInputStream(new File(""));
//4.寫出文件
byte[] buffer=new byte[1024];
int len;
while ((len=fis.read(buffer))!=-1){
os.write(buffer,0,len);
}
//通客戶端已經接受完了
OutputStream outputStream = socket.getOutputStream();
outputStream.write("我已經接收完了,可以斷開了".getBytes());
//5.關閉資源
fis.close();
os.close();
socket.close();
}
URL下載資源
public static void main(String[] args) throws Exception {
//1.下載地址
URL url=new URL("https://baikevideo.cdn.bcebos.com/media/mda-Of9hv9gVColin1F7/492af5f8e199a6a79e31b500cfd862f9.mp4");
//2.連接到這個資源 HTTP
HttpURLConnection urlConnection= (HttpURLConnection) url.openConnection();
InputStream inputStream=urlConnection.getInputStream();
FileOutputStream fos=new FileOutputStream("以父之名.mp4");
byte[] buffer=new byte[1024];
int len;
while ((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);//寫出這個數據
}
fos.close();
inputStream.close();
urlConnection.disconnect();//斷開連接
}
}