1 InetAddress类的使用
1.1 实现网络通信需要解决的两个问题
如何准确定位网络上一台或多台主机;定位主机上的特定应用找到主机后如何可靠高效的进行数据传输
1.2 网络通信的两个要素
对应问题1:IP和端口号
IP的理解:
IP是网络上唯一标识的计算机实体
在Java中使用InetAddress类代表IP
IP分为:IPv4 和 IPv6;万维网 和 局域网
域名: www.baidu.com www.mi.com www.sina.com www.jd.com
域名解析:域名容易记忆,当在连接网络时输入一个主机的域名后,域名服务器(DNS)负责将域名转化成IP地址,这样才能和主机建立连接。 -------域名解析
本地回路地址:127.0.0.1 对应着:localhost
对应问题2:提供网络通信协议
TCP/IP参考模型(应用层、传输层、网络层、物理+数据链路层)
TCP和UDP的区别
TCP的三次握手和四次挥手
1.3 InetAddress类
此类的对象代表着一个具体的IP地址
实例化InetAddress
getByName(String host)getLocalHost()
常用方法
getHostName()getHostAddress()
代码
public class InetAddressTest {
@Test
public void test1() throws UnknownHostException
{
InetAddress net1
= InetAddress
.getByName("192.168.1.167");
InetAddress net2
= InetAddress
.getByName("www.baidu.com");
InetAddress net3
= InetAddress
.getByName("127.0.0.1");
System
.out
.println(net1
);
System
.out
.println(net2
);
System
.out
.println(net3
);
InetAddress net4
= InetAddress
.getLocalHost();
System
.out
.println(net4
);
System
.out
.println(net4
.getHostName());
System
.out
.println(net4
.getHostAddress());
}
}
端口号
正在计算机上运行的进程。
要求:不同的进程不同的端口号范围:被规定为一个 16 位的整数 0~65535。
端口号与IP地址的组合得出一个网络套接字:Socket
2 TCP网络编程
2.1 例子1:客户端发送信息给服务端,服务端将数据显示在控制台上
服务器端:
首先服务器端得有一个ServerSocket指明自己的端口号;
ServerSocket ss
= new ServerSocket(8989);
然后服务器端调用ServerSocket的accept(),创建Socket对象
Socket socket
= ss
.accept();
然后利用socket创建一个输入流,用于将接收socket传来的数据。
InputStream is
= socket
.getInputStream();
因为要输出到控制台上,所以需要使用一个ByteArrayOutputStream进行接收
ByteArrayOutputStream baos
= new ByteArrayOutputStream();
进行数据的读写
byte[] buffer
= new byte[1024];
int len
;
whilw((len
= is
.read(buffer
)) != -1){
baos
.write(buffer
, 0, len
);
}
System
.out
.println(baos
);
客户端:
首先的有一个ip地址
InetAddress net
= InetAddress
.getByName("192.168.56.1");
然后创建一个socket对象,指明要连接的端口号
Socket socket
= new Socket(net
, 8989);
利用socket对象获得一个输出流、
OutputStream os
= socket
.getOutputStream();
写输出数据
os
.write("I am a student".getBytes());
客户端
@Test
public void client(){
Socket socket
= null
;
OutputStream os
= null
;
try {
InetAddress net1
= InetAddress
.getByName("192.168.56.1");
socket
= new Socket(net1
, 8989);
os
= socket
.getOutputStream();
os
.write("你好,我是客户端mm".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 client(){
Socket socket
= null
;
OutputStream os
= null
;
try {
InetAddress net1
= InetAddress
.getByName("192.168.56.1");
socket
= new Socket(net1
, 8989);
os
= socket
.getOutputStream();
os
.write("你好,我是客户端mm".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();
}
}
}
}
2.2 例题2:客户端发送文件给服务端,服务端将文件保存在本地。
服务器端:
首先服务器端得有一个ServerSocket来指明自己的端口号
ServerSocket ss
= new ServerSocket(8989);
然后调用ServerSocket的accept()方法,创建一个socket对象
Socket socket
= ss
.accept();
利用socket对象去创建一个输入流,接收来自socket的数据
InputStream is
= socket
.getInputStream();
创建一个文件输出流,用于写出到本地文件系统中
FileOutputStream fos
= new FileOutputStream("本地的一个路径下的一个文件名");
接收和写出的操作
byte[] buffer
= new byte[1024];
int len
;
while((len
= is
.read(buffer
)) != -1){
fos
.write(buffer
, 0, len
);
}
客户端
首先客户端得有一个用于标识自己的ip
InetAddress net
= InetAddress
.getByName("192.168.56.1");
然后客户端得有一个socket对象指明自己连接那个端口
Socket socket
= new Socket(net
, 8989);
得有一个文件输入流,取出本地的数据
FileInputStream fis
= new FileInputStream("本地的一个待传输的文件。");
利用socket对象创建一个输出流,进行网络的数据输出
OutputStream os
= socket
.getOutputStream();
进行本地数据的读入,和网络数据的输出
byte[] buffer
= new byte[1024];
int len
;
while((len
= fis
.read(buffer
)) != -1){
os
.write(buffer
, 0, len
);
}
服务器端
@Test
public void test1(){
ServerSocket ss
= null
;
Socket socket
= null
;
InputStream is
= null
;
FileOutputStream fos
= null
;
try {
ss
= new ServerSocket(8989);
socket
= ss
.accept();
is
= socket
.getInputStream();
fos
= new FileOutputStream("netTest\\photo1-1.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 (fos
!= null
) {
try {
fos
.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();
}
}
}
}
客户端
@Test
public void test2(){
Socket socket
= null
;
FileInputStream fis
= null
;
OutputStream os
= null
;
try {
InetAddress net
= InetAddress
.getByName("192.168.56.1");
socket
= new Socket(net
, 8989);
fis
= new FileInputStream("netTest\\photo1.jpg");
os
= socket
.getOutputStream();
byte[] buffer
= new byte[1024];
int len
;
while ((len
= fis
.read(buffer
)) != -1){
os
.write(buffer
, 0, len
);
}
} catch (IOException e
) {
e
.printStackTrace();
} finally {
if (os
!= null
){
try {
os
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
if (fis
!= null
){
try {
fis
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
if (socket
!= null
){
try {
socket
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
}
2.3 例题3:从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端,并关闭相应的连接。
客户端
@Test
public void client() throws IOException
{
InetAddress net
= InetAddress
.getByName("192.168.56.1");
Socket socket
= new Socket(net
, 8989);
FileInputStream fis
= new FileInputStream("netTest\\photo1.jpg");
OutputStream os
= socket
.getOutputStream();
byte[] buffer
= new byte[1024];
int len
;
while ((len
= fis
.read(buffer
)) != -1){
os
.write(buffer
, 0, len
);
}
socket
.shutdownOutput();
InputStream is
= socket
.getInputStream();
ByteArrayOutputStream baos
= new ByteArrayOutputStream();
byte[] buffer1
= new byte[1024];
int len1
;
while ((len1
= is
.read(buffer1
)) != -1){
baos
.write(buffer1
, 0, len1
);
}
System
.out
.println(baos
);
}
服务器端
@Test
public void server() throws IOException
{
ServerSocket ss
= new ServerSocket(8989);
Socket socket
= ss
.accept();
InputStream is
= socket
.getInputStream();
FileOutputStream fos
= new FileOutputStream("netTest\\photo1-2.jpg");
byte[] buffer
= new byte[1024];
int len
;
while ((len
= is
.read(buffer
)) != -1){
fos
.write(buffer
, 0, len
);
}
System
.out
.println("图片传输完成");
OutputStream os
= socket
.getOutputStream();
os
.write("你好美女,照片已经收到。".getBytes());
os
.close();
fos
.close();
is
.close();
socket
.close();
ss
.close();
}
3 UDP网络编程
接收端
首先得有一个DatagramSocket对象指明自己的端口号。
DatagramSocket socket
= new DatagramSocket(8989);
然后准备一个数据报包准备接受数据
byte[] buffer
= new byte[1024];
DatagramPacket packet
= new DatagramPacket(buffer
, 0, buffer
.length
);
等待数据,接收数据
socket
.receive();
发送端
首先发送端得有一个DatagramSocket对象
DatagramSocket socket
= new DatagramSocket();
准备数据报包,准备发送数据
其中new DatagramPacket(byte[] data, int off, int len, InetAddress net, int post);
String data
= "我是数据~";
DatagramPacket packet
= new DatagramPacket(data
.getBytes(), 0,data
.length(), InetAddress
.getLocalHost(), 8989);
发送数据
socket
.send(packet
);
public class UDPTest {
@Test
public void sender() throws IOException
{
DatagramSocket socket
= new DatagramSocket();
String str
= "我是UDP方式发送的导弹";
byte[] data
= str
.getBytes();
InetAddress inet
= InetAddress
.getLocalHost();
DatagramPacket packet
= new DatagramPacket(data
,0,data
.length
,inet
,9090);
socket
.send(packet
);
socket
.close();
}
@Test
public void receiver() throws IOException
{
DatagramSocket socket
= new DatagramSocket(9090);
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()));
socket
.close();
}
}
4 URL编程
4.1 URL的理解
URL(Uniform Resource Locator)
统一资源定位符,对应着互联网的某一资源地址。
4.2URL结构
http://localhost:8080/examples/beauty.jpg?username=Tom协议 主机名 端口号 资源地址 参数列表
4.3 实例化URL
URL url
= new URL("http://localhost:8080/examples/beauty.jpg?username=Tom");
4.4 常用方法
4.5 读取、下载对应的URL资源
5.可以读取、下载对应的url资源:
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\\beauty3.jpg");
byte[] buffer
= new byte[1024];
int len
;
while((len
= is
.read(buffer
)) != -1){
fos
.write(buffer
,0,len
);
}
System
.out
.println("下载完成");
} 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();
}
}
}