網(wǎng)絡(luò)編程中的兩個主要問題:
1.如何準(zhǔn)確地定位網(wǎng)絡(luò)上一臺或多臺主機俊鱼;定位主機上的特定的應(yīng)用
2.找到主機后如何可靠高效地進行數(shù)據(jù)傳輸
網(wǎng)絡(luò)編程中的兩個要素
1.對應(yīng)問題一:IP和端口號
2.對應(yīng)問題二:網(wǎng)絡(luò)通信協(xié)議(TCP/IP參考模型)
3.通信要素一:IP和端口號
(1)IP:唯一的表示Intenet上的計算機
(2)在java中使用InetAddress類代表IP
(3)IP地址分為類方式一:IPV4和IPV6
IP地址分為類方式二:公網(wǎng)地址(萬維網(wǎng)使用)和私有地址(局域網(wǎng)使用嵌赠,即192.168開頭,范圍192.168.0.0~192.168.255.255)
4.域名: www.baidu.com
5.本地回路地址:127.0.0.1
6.如何實例化InetAddress:getByName(String adress)党瓮、getLocalHost()
兩個常用方法:getHostName() /getHostAddress()
InetAddress name = InetAddress.getByName("192.168.1.1");
System.out.println(name);///192.168.1.1
InetAddress name1 = InetAddress.getByName("www.baidu.com");
System.out.println(name1);//www.baidu.com/39.156.66.18
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);//DESKTOP-UCI6935/192.168.1.107
String hostName = localHost.getHostName();
String hostAddress = localHost.getHostAddress();
System.out.println(hostName + "....." + hostAddress);//DESKTOP-UCI6935.....192.168.1.107
7.端口號:標(biāo)識計算機上運行的進程(程序)
8.端口號和IP地址組合得出一個網(wǎng)絡(luò)套接字:Socket
三次握手和四次揮手(都針對客戶端)
TCP例題:
1.客戶端發(fā)送內(nèi)容到服務(wù)器,服務(wù)器將內(nèi)容打印到控制臺
/*
服務(wù)器端要處于啟動狀態(tài)
* 客戶端發(fā)送內(nèi)容到服務(wù)器盐类,服務(wù)器將內(nèi)容打印到控制臺
* */
@Test
public void client() throws IOException {
//1.創(chuàng)建客戶端連接寞奸,輸入相應(yīng)的ip地址和端口號(即服務(wù)器)
Socket server = new Socket("127.0.0.1",8899);
//2.將內(nèi)容寫入客戶端
OutputStream outputStream = server.getOutputStream();
outputStream.write("你好".getBytes());
outputStream.close();
server.close();
}
@Test
public void server() throws IOException {
//1.創(chuàng)建服務(wù)器端連接呛谜,客戶端通過服務(wù)器端的端口號進行連接
ServerSocket ss = new ServerSocket(8899);
//2.服務(wù)器端進行連接
Socket socket = ss.accept();
InputStream inputStream = socket.getInputStream();
// //方式一:
// int i;
// byte[] c = new byte[10];
// while ((i = inputStream.read(c)) != -1){
// String str = new String(c,0,i);
// System.out.println(str);
// }
//方式二:輸出流的數(shù)據(jù)被寫入一個byte數(shù)組,可通過toByteArray()和toString()獲取數(shù)據(jù)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i;
byte[] c = new byte[10];
while ((i = inputStream.read(c)) != -1){
baos.write(c,0,i);
System.out.println(baos.toString());
}
inputStream.close();
socket.close();
ss.close();
}
2.客戶端發(fā)送文件到服務(wù)器枪萄,服務(wù)器將文件保存到本地
/*
* 客戶端發(fā)送文件到服務(wù)器隐岛,服務(wù)器將文件保存到本地
* */
@Test
public void client() throws IOException {
String hostAddress = InetAddress.getLocalHost().getHostAddress();
Socket socket = new Socket(hostAddress,1234);
//客戶端發(fā)送文件到服務(wù)器
OutputStream outputStream = socket.getOutputStream();
//發(fā)送的文件
FileInputStream fis = new FileInputStream(new File("1.txt"));
int i;
byte[] c = new byte[10];
while ((i = fis.read(c)) != -1){
outputStream.write(c,0,i);
}
fis.close();
outputStream.close();
socket.close();
}
@Test
public void server() throws IOException {
ServerSocket ss = new ServerSocket(1234);
Socket socket = ss.accept();
//服務(wù)器端獲取文件
InputStream inputStream = socket.getInputStream();
//將文件保存到本地
FileOutputStream fos = new FileOutputStream(new File("socket.txt"));
int i;
byte[] c = new byte[10];
while ((i = inputStream.read(c)) != -1){
fos.write(c,0,i);
}
inputStream.close();
fos.close();
socket.close();
}
3.客戶端發(fā)送文件到服務(wù)器,服務(wù)器保存到本地呻引,并返回“發(fā)送成功”給客戶端礼仗,并關(guān)閉相應(yīng)連接
/*
* 客戶端發(fā)送文件到服務(wù)器,服務(wù)器保存到本地逻悠,并返回“發(fā)送成功”給客戶端元践,并關(guān)閉相應(yīng)連接
* */
@Test
public void client() throws IOException {
Socket socket = new Socket("127.0.0.1",1245);
//將文件發(fā)送到服務(wù)器端
//客戶端發(fā)送文件到服務(wù)器
OutputStream outputStream = socket.getOutputStream();
//讀取出發(fā)送的文件
FileInputStream fis = new FileInputStream(new File("1.txt"));
int i;
byte[] c = new byte[10];
while ((i = fis.read(c)) != -1){
outputStream.write(c,0,i);
}
//關(guān)閉數(shù)據(jù)輸出//
socket.shutdownOutput();
//3.獲取到服務(wù)器發(fā)送的“發(fā)送成功”
InputStream inputStream = socket.getInputStream();
int x;
byte[] b = new byte[10];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((x = inputStream.read(b)) != -1){
baos.write(b,0,x);
}
System.out.println(baos.toString());
inputStream.close();
fis.close();
outputStream.close();
socket.close();
}
@Test
public void server() throws IOException {
ServerSocket ss = new ServerSocket(1245);
Socket socket = ss.accept();
//服務(wù)器端獲取文件
InputStream inputStream = socket.getInputStream();
//將文件保存到本地
FileOutputStream fos = new FileOutputStream(new File("socket.txt"));
int i;
byte[] c = new byte[10];
while ((i = inputStream.read(c)) != -1){
fos.write(c,0,i);
}
//3.給客戶端發(fā)送接收成功
OutputStream outputStream = socket.getOutputStream();
outputStream.write("消息接收成功".getBytes());
outputStream.close();
inputStream.close();
fos.close();
socket.close();
}
UDP舉例
/*
* UDP發(fā)送數(shù)據(jù)
* */
@Test
public void send() throws IOException {
//建立UDP連接
DatagramSocket ds = new DatagramSocket();
String str = "真開心";
byte[] bytes = str.getBytes();
//打包傳送的數(shù)據(jù)
DatagramPacket dp = new DatagramPacket(bytes,0,bytes.length,InetAddress.getLocalHost(),2222);
//發(fā)送數(shù)據(jù)
ds.send(dp);
ds.close();
}
@Test
public void receive() throws IOException {
//建立UDP連接
DatagramSocket ds = new DatagramSocket(2222);
byte[] bytes = new byte[10];
//打包接收的數(shù)據(jù)
DatagramPacket dp = new DatagramPacket(bytes,0,bytes.length);
//接收數(shù)據(jù)
ds.receive(dp);
System.out.println(new String(dp.getData(),0,dp.getLength()));
ds.close();
}
URL編程
URL:統(tǒng)一資源定位符,表示Internet上某一資源的地址
結(jié)構(gòu)組成:http://localhost:8080/example/beaut.jpg?username=xx
協(xié)議 主機名 端口號 資源地址 參數(shù)列表