1.IP
(1)唯⼀的标识Internet 上的计算机(通信实体)
(2)本地回环地址(hostAddress):127.0.0.1 主机名(hostName):localhost
(3)域名(hostName):https://www.baidu.com/
(4)IP地址(hostAddress):110.242.68.3
(5)本地回路地址:
①IP:127.0.0.1
②域名:localhost
(6)主机两种方法表示地址
try {
InetAddress byName = InetAddress.getByName("www.baidu.com");
System.out.println(byName);
InetAddress byName1 = InetAddress.getByName("110.242.68.3");
System.out.println(byName1);
//本地回路地址
InetAddress byName2 = InetAddress.getByName("localhost");
System.out.println(byName2);
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
(7)InetAddress实例化
①getByName(String host) //确定主机名称的IP地址
②getLocalHost( ) //返回本地主机的地址
③常用方法
1)getHostName //获取此IP地址的主机名
2)getHostAddress //返回文本显示中的IP地址字符串
2.端口
(1)端⼝号标识正在计算机上运⾏的进程,不同的进程有不同的端口号
(2)被规定为一个16位整数0~65535
(3)分类
①公认端口:0~1023——被预先定义的服务通信占用
②注册端口:1024~49151——分配给用户进程或应用程序(Tomcat占用8080,MySQL占 用3306)
③动态/私有端口:49152~65535
3.Socket
(1)端⼝号与IP地址的组合得出⼀个⽹络套接字:Socket
(2)IP:端口
4.网络分层思想
(1)在制定协议时,把复杂成份分解成⼀些简单的成份,再将它们复合起来。
(2)最常⽤的复合⽅式是层次⽅式,即同层间可以通信、上⼀层可以调⽤下⼀层,⽽与再下⼀层不发⽣关系。各层互不影响,利于系统的开发和扩展
5.TCP——传输控制协议
(1)使⽤TCP协议前,须先建⽴TCP连接,形成传输数据通道
(2)传输前,采⽤“三次握⼿”⽅式,点对点通信,是可靠的
(3)TCP协议进⾏通信的两个应⽤进程:客户端、服务端。
(4)在连接中可进⾏⼤数据量的传输传输完毕,需释放已建⽴的连接,效率低
(5)TCP的使用——客户端发消息,服务端接收
①客户端——client
1)创建Socket对象,指明服务器端的ip和端⼝号
2)获取⼀个输出流,⽤于输出数据
3)写出数据的操作
4)资源的关闭
@Test //相当于main方法
public void client(){
//1.创建Socket对象,
Socket socket = null;
OutputStream outputStream = null;
try {
//1.指明服务器端的ip和端口号
InetAddress byName = InetAddress.getByName("192.168.0.116");
socket = new Socket(byName, 8989);
//2.获取一个输出流,用于输出数据
outputStream = socket.getOutputStream();
//3.写出数据的操作
outputStream.write("你好,我是客户端峰峰".getBytes());
} catch (UnknownHostException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
//4.资源的关闭
if (socket != null){
try {
socket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (outputStream!=null){
try {
outputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
②服务端.——server
1)创建服务器端的ServerSocket,指明⾃⼰的端⼝号
2)调⽤accept()表示接收来⾃于客户端的socket
3)获取输⼊流
4)读取输⼊流中的数据
5)关闭资源
@Test
public void server(){
// 1.创建服务器端的ServerSocket,
ServerSocket serverSocket = null;
InputStream inputStream = null;
try {
// 1.指明自己的端口号
serverSocket = new ServerSocket(8989);
//2.调用accept()表示接收来自于客户端的socket
Socket accept = serverSocket.accept();
// 3.获取输入流
inputStream = accept.getInputStream();
// 4.读取输入流中的数据
// byte[] bytes = new byte[1024];
// int len;
// while ((len = inputStream.read(bytes))!=-1){
// String s = new String(bytes, 0, len);
// System.out.println(s);
// }
//推荐用法,写入byte数组——没有乱码问题用
// ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//有乱码问题时用
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
//无乱码 byte[] bytes = new byte[5];
char [] bytes1 = new char[5];
int len1;
//无乱码 while ((len1 = byteArrayOutputStream.read(bytes1))!=-1){}
while ((len1 = inputStreamReader.read(bytes1))!=-1){
//无乱码 byteArrayOutputStream.write(bytes1,0,len1);
System.out.println(new String(bytes1,0,len1));
}
//无乱码 System.out.println(byteArrayOutputStream.toString());
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
// 5.关闭资源
if (serverSocket!=null){
try {
serverSocket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
// if (byteArrayOutputStream!=null){
// byteArrayOutputStream.close();
// }
}
}
(6)TCP的使用——客户端发消息,服务端接收并反馈,客户端接收反馈
①客户端——client
@Test
public void client(){
// 1.创建Socket对象,指明服务器端的ip和端口号
// 2.获取一个输出流,用于输出数据
// 3.写出数据的操作
// 4.资源的关闭
Socket socket=null;
OutputStream outputStream=null;
FileInputStream fileInputStream = null;
BufferedInputStream bufferedInputStream = null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
// 1.创建Socket对象,指明服务器端的ip和端口号
InetAddress byName = InetAddress.getByName("127.0.0.1");
socket = new Socket(byName, 8989);
// 2.获取一个输出流,用于输出数据
outputStream = socket.getOutputStream();
// 3.写出数据的操作
//客户端发送一个图片,发给服务端
File file = new File("1.jpeg");
//将图片文件转换为流的形式,写入socketoutputStream中
fileInputStream =new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);
//以流的形式读取图片
byte[] bytes = new byte[1024];
int len ;
while ((len = bufferedInputStream.read(bytes))!=-1){
//写入数据
outputStream.write(bytes,0,len);
}
//传完数据后关闭输出流——通知服务端关闭流
socket.shutdownOutput();
//接收服务端发出的内容
inputStream = socket.getInputStream();
byteArrayOutputStream = new ByteArrayOutputStream();
int len2;
byte[] bytes1 = new byte[1024];
while ((len2 = inputStream.read(bytes1))!=-1){
byteArrayOutputStream.write(bytes1,0,len2);
}
System.out.println(byteArrayOutputStream.toString());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (outputStream!=null) {
outputStream.close();
}
if (socket!=null) {
socket.close();
}
if (fileInputStream!=null){
fileInputStream.close();
}
if (bufferedInputStream != null){
bufferedInputStream.close();
}
if (inputStream!=null){
inputStream.close();
}
if (byteArrayOutputStream!=null){
byteArrayOutputStream.close();
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
②服务端.——server
@Test
public void server(){
//1.创建服务器端的ServerSocket,指明自己的端口号
// 2.调用accept()表示接收来自于客户端的socket
// 3.获取输入流
// 4.读取输入流中的数据
// 5.关闭资源
ServerSocket serverSocket=null;
Socket socket=null;
InputStream inputStream =null;
BufferedOutputStream bufferedOutputStream = null;
OutputStream outputStream = null;
try {
// 1.创建服务器端的ServerSocket,指明自己的端口号
serverSocket = new ServerSocket(8989);
// 2.调用accept()表示接收来自于客户端的socket
socket = serverSocket.accept();
// 3.获取输入流
inputStream = socket.getInputStream();
//服务端,接受图片信息,保存到本地
//创建一个File文件
File file = new File("1.1.jpeg");
FileOutputStream fileOutputStream = new FileOutputStream(file);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
byte [] bytes=new byte[1];
int len;
while ((len=inputStream.read(bytes))!=-1){
//写入图片
bufferedOutputStream.write(bytes, 0,len );
}
//反馈一个内容给客户端
//拿到输出流
outputStream = socket.getOutputStream();
outputStream.write("图片收到了".getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally {
// 5.关闭资源
try {
if (serverSocket!=null){
serverSocket.close();
}
if (socket!=null){
socket.close();
}
if (inputStream!=null){
inputStream.close();
}
if(bufferedOutputStream!=null){
bufferedOutputStream.close();
}
if (outputStream!=null){
bufferedOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
6.UDP——用户数据报协议
(1)将数据、源、⽬的封装成数据包,不需要建⽴连接
(2)每个数据宝的⼤⼩限制在64K内
(3)发送不管对⽅是否准备好,接收⽅收到也不确认,故是不可靠的
(4)可以⼴播发送
(5)发送数据结束时⽆需释放资源,开销⼩,速度快
(6)UDP的使用
①类DatagramSocket和DatagramPacket实现了基于UDP 协议⽹络程序
②1DatagramSocket与DatagramPacket
③建⽴发送端,接收端
④调⽤Socket的发送、接收⽅法
⑤关闭Socket
1)客户端
@Test
public void client(){
DatagramSocket datagramSocket = null;
try {
//1.DatagramSocket与DatagramPacket
datagramSocket = new DatagramSocket();
//2.建立发送端,接收端
InetAddress byAddress = InetAddress.getByName("127.0.0.1");
String content = "你好我是客户端";
//3.建立数据包
DatagramPacket datagramPacket1 = new DatagramPacket(content.getBytes(),0,content.getBytes().length,byAddress,9988);
//4.调用Socket的发送、接收方法
datagramSocket.send(datagramPacket1);
} catch (SocketException e) {
throw new RuntimeException(e);
} catch (UnknownHostException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
// 5.关闭Socket
if (datagramSocket!=null){
datagramSocket.close();
}
}
}
2)服务端
@Test
public void server(){
DatagramSocket datagramSocket = null;
try {
// 1.DatagramSocket与DatagramPacket
// 2.建立发送端,接收端
datagramSocket = new DatagramSocket(9988);
// 3.建立数据包
byte[] bytes = new byte[100];
DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length);
// 4.调用Socket的发送、接收方法
datagramSocket.receive(datagramPacket);
String s = new String(bytes, 0, datagramPacket.getLength());
System.out.println(s);
} catch (SocketException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
// 5.关闭Socket
if (datagramSocket!=null){
datagramSocket.close();
}
}
}