計算機網(wǎng)絡
就是將地理位置不同的具有獨立功能的多臺計算及外部設備,通過通信線路連接起來,在網(wǎng)絡操作系統(tǒng)、網(wǎng)絡管理軟件以及網(wǎng)絡通信協(xié)議的管理和協(xié)調(diào)下,實現(xiàn)資源共享和信息傳遞的計算機系統(tǒng)
目的
傳播交流信息、數(shù)據(jù)交換、通信
如何做到
1.如何準確定位網(wǎng)絡上的一臺主機 192.xxx.xx.xx:端口,定位到這個計算機上的某個資源
2.找到主機,如何傳輸數(shù)據(jù)
javaweb:網(wǎng)頁編程 b/s
網(wǎng)絡編程:tcp/ip c/s
網(wǎng)絡通信的要素
如何實現(xiàn)網(wǎng)絡的通信
1.ip、端口號
2.協(xié)議 tcp/ip協(xié)議
3.萬物皆對象
IP
ip地址:InetAdress
唯一定位一臺網(wǎng)絡上的計算機
127.0.0.1:本機Locallost
IP地址分類
ipv4/ipv6
ipv4 127.0.0.1 4個字節(jié) 0~255 42億 30億在北美,4億在亞洲,2011年用盡
ipv6:128 8個無符號整數(shù)
公網(wǎng)(互聯(lián)網(wǎng))/私網(wǎng)(局域網(wǎng))
ABCD類地址
192.168.xx.xx 專門給組織內(nèi)部使用的
域名:記憶ip問題
package com.xgglr;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
public class IPDemo {
public static void main(String[] args) {
try {
InetAddress[] inetAddresses = InetAddress.getAllByName("127.0.0.1");
System.out.println(Arrays.toString(inetAddresses));
InetAddress[] localHosts = InetAddress.getAllByName("localhost");
System.out.println(Arrays.toString(localHosts));
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);
InetAddress[] baiduName = InetAddress.getAllByName("www.baidu.com");
System.out.println(Arrays.toString(baiduName));
for (InetAddress address : baiduName) {
String canonicalHostName = address.getCanonicalHostName();//規(guī)范名稱
String hostName = address.getHostName();//域名
String hostAddress = address.getHostAddress();//IP
System.out.println(canonicalHostName);
System.out.println(hostName);
System.out.println(hostAddress);
System.out.println("============");
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
端口
表示計算機上的一個程序的進程,不同的進程端口號不同,區(qū)分如軟件
被規(guī)定0~65535
tcp/udp 6553582 tcp:80 udp:80 單個協(xié)議下,端口號不能沖突
端口分類:共有端口0-1023
http:80 https:443 ftp:21 Telent:23
程序注冊端口:1024-49151,分配用戶或者程序
tomcat:8080 mysql:3306 oracle:1521
動態(tài)、私有:49152-65535
netstat -ano 查看所有端口
netstat -ano|findstr “5900” 查看指定端口
tasklist|findstr “8696” 查看指定端口的進程
通信協(xié)議
網(wǎng)絡通信協(xié)議:速率,傳輸碼率,代碼結(jié)構(gòu),傳輸控制
分層思想
tcp/ip協(xié)議:一組協(xié)議
tcp:用戶傳輸協(xié)議,udp:用戶數(shù)據(jù)報協(xié)議
出名的協(xié)議:tcp,ip:網(wǎng)絡互聯(lián)協(xié)議
tcp和udp對比
tcp:打電話
連接 穩(wěn)定
三次握手 四次揮手
客戶端 服務端
傳輸完程 釋放連接 效率低
udp:發(fā)短信
不連接 不穩(wěn)定
客戶端、服務端 沒有明確的界限
不能有沒有準備好 都可以發(fā)送
TCP
客戶端:連接服務器Socket,發(fā)送消息
package com.xgglr.tcpDemo;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
//客戶端
public class tcpClintDemo {
public static void main(String[] args) {
try {
InetAddress byName = InetAddress.getByName("localhost");
int port = 8888;
Socket socket = new Socket(byName,port);
OutputStream outputStream = socket.getOutputStream();
outputStream.write("您好!!!,這里時客戶端".getBytes());
outputStream.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
服務端:建立服務的接口ServerSocket,等待用戶的連接accept,接受用戶的消息文章來源:http://www.zghlxwxcb.cn/news/detail-837993.html
package com.xgglr.tcpDemo;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
//服務端
public class tcpServerDemo {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
serverSocket = new ServerSocket(8888);
while (true){
socket = serverSocket.accept();
inputStream = socket.getInputStream();
//管道流
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len;
while ((len = inputStream.read(bytes)) != -1){
byteArrayOutputStream.write(bytes,0,len);
}
System.out.println(byteArrayOutputStream.toString());
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (byteArrayOutputStream != null){
try {
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null){
try {
inputStream.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();
}
}
}
}
}
文件上傳
package com.xgglr.tcpDemo;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
//服務端
public class TcpFileDownLoaderServerDemo {
public static void main(String[] args) {
try {
while (true){
ServerSocket socket = new ServerSocket(9999);
Socket accept = socket.accept();
InputStream is = accept.getInputStream();
FileOutputStream fos = new FileOutputStream(
new File("D:\\software\\JavaCode\\javaSE\\JavaSE01\\src\\com\\xgglr\\tcpDemo\\tcp.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1){
fos.write(buffer,0,len);
}
//確認接收到了,可以斷開
accept.getOutputStream().write("已經(jīng)接收,可以關閉!??!".getBytes());
fos.close();
is.close();
accept.close();
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.xgglr.tcpDemo;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
//客戶端
public class TcpFileDownLoaderClintDemo {
public static void main(String[] args) {
try {
//創(chuàng)建連接
InetAddress byName = InetAddress.getByName("localhost");
int port = 9999;
Socket socket = new Socket(byName,port);
//創(chuàng)建輸出流
OutputStream os = socket.getOutputStream();
//讀取文件
FileInputStream fileInputStream = new FileInputStream(new File("2.jpg"));
//寫出文件
byte[] buffer = new byte[1024];
int len;
while ((len = fileInputStream.read(buffer)) != -1){
os.write(buffer,0,len);
}
//通知服務器發(fā)送完畢
socket.shutdownOutput();
//確認是否接收
InputStream is = socket.getInputStream();
//管道流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int lens;
while ((lens = is.read(bytes)) != -1){
baos.write(bytes,0,lens);
}
System.out.println(baos.toString());
fileInputStream.close();
os.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
UDP
不用連接,需要知道地址文章來源地址http://www.zghlxwxcb.cn/news/detail-837993.html
package com.xgglr.udp;
import java.io.IOException;
import java.net.*;
//UDP客戶端
public class UdpClint {
public static void main(String[] args) {
//建立連接Socket
try {
DatagramSocket socket = new DatagramSocket();
//建包
String msg = "你好?。?! Server";
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
/**
* byte buf[], 數(shù)據(jù)
* int offset, 數(shù)據(jù)的起始長度
* int length, 結(jié)束長度
* netAddress address, 接受ip
* int port 端口號
* */
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);
//發(fā)送包
socket.send(packet);
socket.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.xgglr.udp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.Arrays;
//UDP服務端
public class UdpServer {
public static void main(String[] args) {
//開放端口
try {
DatagramSocket socket = new DatagramSocket(9090);
//接受數(shù)據(jù)
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);// 阻塞接收
System.out.println(packet.getAddress().getHostName());
System.out.println(packet.getPort());
System.out.println(packet.getLength());
// System.out.println(Arrays.toString(packet.getData()));
socket.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
到了這里,關于網(wǎng)絡編程、UDP、TCP的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!