14 - 1 网络编程概述
14 - 2 网络通信要素概述
一、实现网络通信需要解决的问题
1.如何准确地定位网络上一台或多台主机;定位主机上的特定的应用
2.找到主机后如何可靠高效地进行数据传输
二、网络编程中的两个要素:
1.对应问题一:IP和端口号
2.对应问题二:提供网络通信协议:TCP/IP参考模型(应用层、传输层、网络层、物理+数据链路层)
14 - 3 通信要素1: IP和端口号
1.IP
1.IP:唯一的标识Internet上的计算机(通信实体)
2.在Java中使用InetAddress类代表IP
3.IP分类:IPv4和IPv6
4.域名:www.baidu.com
5.本地回路地址127.0.0.1 对应着:localhost
6.如何实例化InetAddress:两个方法:getByName(String host)、getLocalHost()(获取实例对象)
两个常用方法:getHostName、getHostAddress
2.端口号
1.端口号:正在计算机上运行的进程
2.要求:不同的进程有不同的端口号
3.范围:被规定为一个16位的整数0~65535。
4.端口号与IP地址的组合得出一个网络套接字:Socket
public static void main(String[] args) { try { //File file = new File("hello.txt"); InetAddress inetAddress1 = InetAddress.getByName("192.168.10.14"); System.out.println(inetAddress1); InetAddress inetAddress2 = InetAddress.getByName("www.atguigu.com"); System.out.println(inetAddress2); InetAddress inetAddress3 = InetAddress.getByName("127.0.0.1"); System.out.println(inetAddress3); //获取本地ip InetAddress inetAddress4 = InetAddress.getLocalHost(); System.out.println(inetAddress4); //getHostName() System.out.println(inetAddress2.getHostName()); //getHostAddress() System.out.println(inetAddress2.getHostAddress()); } catch (UnknownHostException e) { e.printStackTrace(); } }
14 - 4 通信要素2:网络协议
一、网络通信协议
1.计算机网络中实现通信必须有一些约定。
即通信协议,对速率、传输代码、代码结构、传输控制步骤、出错控制等制定标准。
2.传输层协议中有两个非常重要的协议:
传输控制协议TCP(Transmission Control Protocol)
用户数据报协议UDP(User Datagram Protocol)。
3.TCP和UDP
1)TCP的三次握手
TCP的四次挥手
14 - 5 TCP网络编程
一、客户端
1.创建Socket对象,指明服务器端的IP和端口号
2.获取一个输出流,用于输出数据
3.写出数据
4.资源的关闭
@Test public void client() { Socket socket = null; OutputStream os = null; try { //1.创建Socket对象,指明服务器端的IP和端口号 InetAddress inetAddress = InetAddress.getByName("127.0.0.1"); socket = new Socket(inetAddres,8899); //2.获取一个输出流,用于输出数据 os = socket.getOutputStream(); //3.写出数据 os.write("你好,我是客户端".getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { //4.资源的关闭 if (os != null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket != null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
二、服务器端
1.创建服务器端的ServerSocket,指明自己的端口号
2.调用accept()表示接受来自于客户端的socket
3.获取一个输入流
4.读取输入流的数据
5.关闭资源
@Test public void server() { ServerSocket ss = null; Socket socket = null; InputStream is = null; ByteArrayOutputStream baos = null; try { //1.创建服务器端的ServerSocket,指明自己的端口号 ss = new ServerSocket(8899); //2.调用accept()表示接受来自于客户端的socket socket = ss.accept(); //3.获取一个输入流 is = socket.getInputStream(); //4.读取输入流的数据 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()); System.out.println("收到了来自于" + socket.getInetAddress().getHostAddress() + "的数据"); } catch (IOException e) { e.printStackTrace(); } finally { //5.关闭资源 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(); } } } }
package com.atguigu.java2; import org.junit.Test; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; /** * @author 张丁野 * @version v1.0 * @time 2022-07-27-22:27 * @Description * 例题三:从客户端发送文件给服务端,服务端保存到本地,并返回“发送成功”给客户端 */ public class TCPTest3 { @Test public void client() throws Exception { Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9090); OutputStream os = socket.getOutputStream(); FileInputStream fis = new FileInputStream(new File("Mypicture.jpg")); 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[20]; int len1; while ((len1 = is.read(buffer1)) != -1){ baos.write(buffer1,0,1); } System.out.println(baos.toString()); fis.close(); baos.close(); is.close(); os.close(); } @Test public void server()throws Exception{ ServerSocket ss = new ServerSocket(9090); Socket socket = ss.accept(); InputStream is = socket.getInputStream(); FileOutputStream fos = new FileOutputStream(new File("serverPicture.jpg")); byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) != -1){ fos.write(buffer,0,len); } //服务器端给予客户端反馈 OutputStream os = socket.getOutputStream(); os.write("服务器已成功接收".getBytes()); fos.close(); is.close(); socket.close(); os.close(); } }
14 - 6 UDP网络编程
public class UDPTest { @Test public void sender() throws Exception{ DatagramSocket socket = new DatagramSocket(); String str = "我是UPD方式发送的信息"; InetAddress inet = InetAddress.getLocalHost(); DatagramPacket packet = new DatagramPacket(str.getBytes(),str.getBytes("UTF-8").length,inet,9090); socket.send(packet); socket.close(); } @Test public void recevier()throws Exception{ 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(),packet.getLength())); socket.close(); } }