Java的面向?qū)ο缶幊?/a>">Java的面向?qū)ο缶幊?/a>
652
2022-05-30
@[toc]
一,網(wǎng)絡(luò)編程
網(wǎng)絡(luò)編程在很大程度上是發(fā)送和接收信息的功能,中間傳輸是物理線路。
1.2,目的:
計算機網(wǎng)絡(luò):
計算機網(wǎng)絡(luò)是指在網(wǎng)絡(luò)操作系統(tǒng)、網(wǎng)絡(luò)管理軟件和網(wǎng)絡(luò)通信協(xié)議的管理和協(xié)調(diào)下,通過通信線路將不同地理位置上具有獨立功能的多臺計算機及其外部設(shè)備連接起來,實現(xiàn)資源共享和信息傳輸?shù)挠嬎銠C系統(tǒng)。
網(wǎng)絡(luò)編程目的:
數(shù)據(jù)交流,數(shù)據(jù)交互。就是通信。
過程:
1,確定一臺主機。192.168.16.124:端口,定位到
這個主機上的某個位置資源。
網(wǎng)頁編程和網(wǎng)絡(luò)編程:
JavaWeb是網(wǎng)頁編程 B/S
網(wǎng)絡(luò)編程:C/S
我這里主要針對于傳輸層,TCP,UDP。
倆個問題:
1,準確定位到網(wǎng)絡(luò)上的一臺或者多臺終端
2,通信
要素:
1,ip和端口號
2,網(wǎng)絡(luò)通信協(xié)議
這篇文章先記錄一下ip和端口的理解與運用。
二,Java萬物皆對象
ip地址:inetaddress
2.1,InetAddress類
這個類,沒有構(gòu)造方法,不能直接new對象。只能通過靜態(tài)方法獲得InetAddress的對象。
例子:
目的就是:查詢網(wǎng)站ip地址,這里用ctf網(wǎng)站為例。
public class inet { public static void main(String[] args) throws UnknownHostException, UnknownHostException { InetAddress InetAddress1=InetAddress.getByName("imustctf.top"); System.out.println(InetAddress1.getAddress()); System.out.println(InetAddress1.getCanonicalHostName()); System.out.println(InetAddress1.getHostAddress()); System.out.println(InetAddress1.getHostName()); } }
上述代碼解釋:
getCanonicalHostName方法獲取規(guī)范名。
getHostAddress方法獲取ip
getHostName方法獲取域名
輸出樣式:
2.2,端口
端口就是計算機上一個程序的進程。
不同的進程使用不同的端口號是為了區(qū)分軟件。端口號不能沖突。
我們用命令看一下電腦的一些目前使用的端口。
命令是:
查看所有端口命令:
netstat -ano
如下圖:
查看指定的端口和查看端口的進程
netstat -ano|finder "" tasklist|findstr ""
2.3 InetSocketAddress類
1,netSocketAddress類是用來封裝端口的。
2,可以理解為在InetAddress基礎(chǔ)上加端口。
3,有構(gòu)造器的。
這個可以new,沒問題哈。
依然用imustctf來舉例子。
public static void main(String[] args) { InetSocketAddress SocketAddress= new InetSocketAddress("imustctf.top",8080); System.out.println(SocketAddress); System.out.println(SocketAddress.getPort()); InetAddress SocketAddress1=SocketAddress.getAddress(); System.out.println(SocketAddress1.getHostAddress()); System.out.println(SocketAddress.getHostName()); }
上述代碼塊解釋:
getPort方法:獲得端口號。
用InetAddress類獲取到端口ip。
getHostAddress方法:上面說過他的作用
getHostName方法:輸出端口名
輸出如下:
imustctf.top/39.103.239.16:8080
8080
39.103.239.16
imustctf.top
三,最后
這篇文章先寫一下網(wǎng)絡(luò)編程的基礎(chǔ)學習,ip和端口的知識與運用。后續(xù)繼續(xù)深入研究學習。
Java TCP/IP 網(wǎng)絡(luò)
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔相應法律責任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。