网络编程 - 概述、网络编程要素、IP和端口号、网络协议、TCP、UDP、URL
网络编程1 网络编程概述2 网络通信要素概述3 通信要素1:IP和端口号3.1 内容3.2 InetAddress类3.2.1 概述3.2.2 方法3.2.3 代码实例
4 通信要素2:网络协议4.1 内容4.2 TCP/IP协议簇4.3 TCP和UDP4.4 TCP三次握手和四次挥手4.5 Socket
5 TCP网络编程5.1 基于Socket的TCP编程5.2 代码实例5.3 例题5.4 练习
6 UDP网络编程6.1 内容6.2 常用方法6.3 代码实例
7 URL编程7.1 URL类内容7.2 URL类构造器7.3 URL类常用方法7.6 代码实例7.5 针对HTTP协议的URLConnection类7.6 URI、URL和URN的区别
8 小结
网络编程
1 网络编程概述
网络编程中有两个主要的问题:
如何准确地定位网络上一台或多台主机;定位主机上的特定的应用找到主机后如何可靠高效地进行数据传输
网络编程中的两个要素:
对应问题一:IP和端口号对应问题二:网络通信协议:TCP/IP参考模型(应用层、传输层、网络层、物理+数据链路层)
2 网络通信要素概述
3 通信要素1:IP和端口号
IP:Internet Protocol
3.1 内容
通信要素一:IP和端口号
IP:唯一的标识 Internet 上的计算机(通信实体)
在Java中使用InetAddress类代表IP
IP分类:IPv4 和 IPv6;万维网 和 局域网
域名:www.baidu.com
本地回路地址:127.0.0.1
对应着:localhost
如何实例化InetAddress(无构造器):
两个获取InetAddress实例方法:
public static InetAddress getByName(String host)public static InetAddress getLocalHost() 两个常用方法:
public String getHostName():获取此 IP 地址的主机名【域名】public String getHostAddress():返回 IP 地址字符串(以文本表现形式)【IP地址】
端口号:正在计算机上运行的进程
要求:不同进程有不同的端口号
范围:被规定维一个16位的整数0~65535
端口号与IP地址的组合得出一个网络套接字:Socket
3.2 InetAddress类
3.2.1 概述
3.2.2 方法
3.2.3 代码实例
public class InetAddressTest {
public static void main(String
[] args
) {
try {
InetAddress inet1
= InetAddress
.getByName("192.168.10.14");
System
.out
.println(inet1
);
InetAddress inet2
= InetAddress
.getByName("www.wumiaojie.com");
System
.out
.println(inet2
);
InetAddress inet3
= InetAddress
.getByName("127.0.0.1");
System
.out
.println(inet3
);
InetAddress inet4
= InetAddress
.getLocalHost();
System
.out
.println(inet4
);
System
.out
.println(inet2
.getHostName());
System
.out
.println(inet2
.getHostAddress());
} catch (UnknownHostException e
) {
e
.printStackTrace();
}
}
}
4 通信要素2:网络协议
4.1 内容
4.2 TCP/IP协议簇
4.3 TCP和UDP
4.4 TCP三次握手和四次挥手
三次握手
客户端:你好,我是吴
服务端:你好,我知道你是吴,我是王
客户端:你好,我知道你知道我是吴,我是吴
UDP:(可用于视频直播)
导弹全部一次性多量发送,被拦截率低,不管有没有打中
一般是客户端主动断开连接
客户端:我现在想断开连接了
服务端:我接受到你想断开连接的信息了
服务端:我现在已经断开连接了
客户端:发个信息确认是否断开【有没有后话,无则断开了】
4.5 Socket
端口号与IP地址的组合得出一个网络套接字:Socket
5 TCP网络编程
5.1 基于Socket的TCP编程
5.2 代码实例
客户端岛
服务器岛
客户端岛生产了一些产品想卖给服务器岛
于是客户端岛整了一小船(Socket),船里装东西
船有运送的目的,即服务器岛(IP)
服务器岛有多个港口(ServerSocket),船要停在哪里,就需要只能具体的港口号码(端口号)
package com
.wmiao
.java
;
import org
.junit
.Test
;
import java
.io
.ByteArrayOutputStream
;
import java
.io
.IOException
;
import java
.io
.InputStream
;
import java
.io
.OutputStream
;
import java
.net
.InetAddress
;
import java
.net
.ServerSocket
;
import java
.net
.Socket
;
public class TCPTest1 {
@Test
public void client() {
Socket socket
= null
;
OutputStream os
= null
;
try {
InetAddress inet
= InetAddress
.getByName("127.0.0.1");
socket
= new Socket(inet
, 8899);
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();
}
}
}
}
@Test
public void server(){
ServerSocket ss
= null
;
Socket socket
= null
;
InputStream is
= null
;
ByteArrayOutputStream baos
= null
;
try {
ss
= new ServerSocket(8899);
socket
= ss
.accept();
is
= socket
.getInputStream();
baos
= new ByteArrayOutputStream();
byte[] buffer
= new byte[5];
int len
;
while ((len
= is
.read(buffer
)) != -1){
baos
.write(buffer
,0,len
);
}
System
.out
.println(baos
.toString());
} catch (IOException e
) {
e
.printStackTrace();
} finally {
if (baos
!= null
) {
try {
baos
.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 (ss
!= null
) {
try {
ss
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
}
}
服务端获取客户端发送者信息
System
.out
.println("收到了来自于:" + socket
.getInetAddress().getHostAddress() + "的数据");
5.3 例题
package com
.wmiao
.java
;
import org
.junit
.Test
;
import java
.io
.*
;
import java
.net
.InetAddress
;
import java
.net
.ServerSocket
;
import java
.net
.Socket
;
public class TCPTest2 {
@Test
public void client() {
Socket socket
= null
;
OutputStream os
= null
;
FileInputStream fis
= null
;
try {
socket
= new Socket(InetAddress
.getByName("127.0.0.1"),8899);
os
= socket
.getOutputStream();
fis
= new FileInputStream(new File("爱情与友情.jpg"));
byte[] buffer
= new byte[20];
int len
;
while ((len
= fis
.read(buffer
)) != -1){
os
.write(buffer
,0,len
);
}
} catch (IOException e
) {
e
.printStackTrace();
} finally {
if (fis
!= null
) {
try {
fis
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
if (os
!= null
) {
try {
os
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
if (socket
!= null
) {
try {
socket
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
}
@Test
public void server(){
ServerSocket ss
= null
;
Socket socket
= null
;
InputStream is
= null
;
FileOutputStream fos
= null
;
try {
ss
= new ServerSocket(8899);
socket
= ss
.accept();
is
= socket
.getInputStream();
fos
= new FileOutputStream(new File("爱情与友情1.jpg"));
byte[] buffer
= new byte[20];
int len
;
while ((len
= is
.read(buffer
)) != -1){
fos
.write(buffer
,0,len
);
}
} catch (IOException e
) {
e
.printStackTrace();
} finally {
if (fos
!= null
) {
try {
fos
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
if (is
!= null
) {
try {
is
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
if (ss
!= null
) {
try {
ss
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
if (socket
!= null
) {
try {
socket
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
}
}
package com
.wmiao
.java
;
import org
.junit
.Test
;
import java
.io
.*
;
import java
.net
.InetAddress
;
import java
.net
.ServerSocket
;
import java
.net
.Socket
;
public class TCPTest3 {
@Test
public void client() {
Socket socket
= null
;
OutputStream os
= null
;
FileInputStream fis
= null
;
InputStream is
= null
;
ByteArrayOutputStream baos
= null
;
try {
socket
= new Socket(InetAddress
.getByName("127.0.0.1"),8899);
os
= socket
.getOutputStream();
fis
= new FileInputStream(new File("爱情与友情.jpg"));
byte[] buffer
= new byte[20];
int len
;
while ((len
= fis
.read(buffer
)) != -1){
os
.write(buffer
,0,len
);
}
socket
.shutdownOutput();
is
= socket
.getInputStream();
baos
= new ByteArrayOutputStream();
byte[] buffer2
= new byte[20];
int len2
;
while ((len2
= is
.read(buffer2
)) != -1){
baos
.write(buffer2
,0,len2
);
}
System
.out
.println(baos
.toString());
} catch (IOException e
) {
e
.printStackTrace();
} finally {
if (fis
!= null
) {
try {
fis
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
if (os
!= null
) {
try {
os
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
if (socket
!= null
) {
try {
socket
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
if (is
!= null
) {
try {
is
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
if (baos
!= null
) {
try {
baos
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
}
@Test
public void server(){
ServerSocket ss
= null
;
Socket socket
= null
;
InputStream is
= null
;
FileOutputStream fos
= null
;
OutputStream os
= null
;
try {
ss
= new ServerSocket(8899);
socket
= ss
.accept();
is
= socket
.getInputStream();
fos
= new FileOutputStream(new File("爱情与友情3.jpg"));
byte[] buffer
= new byte[20];
int len
;
while ((len
= is
.read(buffer
)) != -1){
fos
.write(buffer
,0,len
);
}
os
= socket
.getOutputStream();
os
.write("你好,图片我已经收到,发送成功".getBytes());
} catch (IOException e
) {
e
.printStackTrace();
} finally {
if (fos
!= null
) {
try {
fos
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
if (is
!= null
) {
try {
is
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
if (ss
!= null
) {
try {
ss
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
if (socket
!= null
) {
try {
socket
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
if (os
!= null
) {
try {
os
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
}
}
5.4 练习
6 UDP网络编程
6.1 内容
6.2 常用方法
6.3 代码实例
package com
.wmiao
.java
;
import org
.junit
.Test
;
import java
.io
.IOException
;
import java
.net
.DatagramPacket
;
import java
.net
.DatagramSocket
;
import java
.net
.InetAddress
;
public class UDPTest {
@Test
public void sender(){
DatagramSocket socket
= null
;
try {
socket
= new DatagramSocket();
String str
= "我是UDP方式发送的导弹";
byte[] data
= str
.getBytes();
InetAddress inet
= InetAddress
.getLocalHost();
DatagramPacket packet
= new DatagramPacket(data
,0,data
.length
,inet
,8899);
socket
.send(packet
);
} catch (IOException e
) {
e
.printStackTrace();
} finally {
if (socket
!= null
) {
socket
.close();
}
}
}
@Test
public void receiver(){
DatagramSocket socket
= null
;
try {
socket
= new DatagramSocket(8899);
byte[] buffer
= new byte[100];
DatagramPacket packet
= new DatagramPacket(buffer
,0,buffer
.length
);
socket
.receive(packet
);
System
.out
.println(new String(packet
.getData(), 0, packet
.getLength()));
} catch (IOException e
) {
e
.printStackTrace();
} finally {
if (socket
!= null
) {
socket
.close();
}
}
}
}
7 URL编程
7.1 URL类内容
7.2 URL类构造器
7.3 URL类常用方法
7.6 代码实例
package com
.wmiao
.java
;
import java
.net
.MalformedURLException
;
import java
.net
.URL
;
public class URLTest {
public static void main(String
[] args
) {
try {
URL url
= new URL("http://192.168.1.100:8080/helloworld/index.jsp#a?username=shkstart&password=123");
System
.out
.println("getProtocol() :"+url
.getProtocol());
System
.out
.println("getHost() :"+url
.getHost());
System
.out
.println("getPort() :"+url
.getPort());
System
.out
.println("getPath() :"+url
.getPath());
System
.out
.println("getFile() :"+url
.getFile());
System
.out
.println("getQuery() :"+url
.getQuery());
} catch (MalformedURLException e
) {
e
.printStackTrace();
}
}
}
7.5 针对HTTP协议的URLConnection类
package com
.wmiao
.java
;
import java
.io
.FileOutputStream
;
import java
.io
.IOException
;
import java
.io
.InputStream
;
import java
.net
.HttpURLConnection
;
import java
.net
.URL
;
public class URLTest1 {
public static void main(String
[] args
) {
HttpURLConnection urlConnection
= null
;
InputStream is
= null
;
FileOutputStream fos
= null
;
try {
URL url
= new URL("http://localhost:8080/examples/beauty.jpg");
urlConnection
= (HttpURLConnection
) url
.openConnection();
urlConnection
.connect();
is
= urlConnection
.getInputStream();
fos
= new FileOutputStream("day10\\beaty.jpg");
byte[] buffer
= new byte[1024];
int len
;
while ((len
= is
.read(buffer
)) != -1){
fos
.write(buffer
,0,len
);
}
} catch (IOException e
) {
e
.printStackTrace();
} finally {
if (is
!= null
) {
try {
is
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
if (fos
!= null
) {
try {
fos
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
if (urlConnection
!= null
) {
urlConnection
.disconnect();
}
}
}
}
7.6 URI、URL和URN的区别
8 小结