网络基础
-
网络编程目的:直接或间接的通过网络协议与其他计算机实现数据交换,进行通讯。
-
网络编程中有两个主要问题:
-
如何精准的定位网络上一台或者多台主机;定位主机上的特定的应用
-
找到主机后该如何可靠的高效的进行数据传输
-
网络通信要素
-
如何实现网络中的主机互相通信
-
IP
-
端口号
-
-
一定的规则(即:网络通信协议。有两套参考模型)
-
OSI参考模型:模型过于理想化,未能在因特网上进行广泛推广
-
应用层
-
表示层
-
会话层
-
传输层
-
网络层
-
数据链路层
-
物理层
-
-
TCP/IP参考模型(或TCP/IP协议):事实上的国际标准
-
应用层
-
传输层
-
网络层
-
物理+数据链路层
-
-
要素1:IP和端口号
package com.Pan.JavaWeb; import java.net.InetAddress; import java.net.UnknownHostException; /** * 通信要素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() * @author Pan */ 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.atguigu.com"); System.out.println(inet2); InetAddress inet3 = InetAddress.getByName("127.0.0.1"); System.out.println(inet3); //获取本机的IP InetAddress inet4 = InetAddress.getLocalHost(); System.out.println(inet4); //getHostName() System.out.println(inet2.getHostName()); //getHostAddress() System.out.println(inet2.getHostAddress()); } catch (UnknownHostException e) { e.printStackTrace(); } } }
通信要素2:网络通信协议
-
TCP协议
-
使用TCP协议前,需先建立TCP连接,形成传输数据通道
-
传输前,采用三次握手方式,点对点通信,是可靠的
-
TCP协议进行通信的两个应用进程:客户端、服务端
-
在连接中可进行大数据量的传输
-
传输完毕,需释放已建立的连接,效率低
package com.Pan.JavaWeb; 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; import java.net.UnknownHostException; import java.nio.charset.StandardCharsets; /** * 实现TCP的网络编程 * 例子一:客户端发送信息给服务器端,服务端将数据显示在控制台上 * @author Pan */ public class TCPTest1 { //客户端 @Test public void client() { Socket socket = null; OutputStream os = null; try { //1.创建Socket对象,指明服务器端的ip和端口号 InetAddress inet = InetAddress.getByName("127.0.0.1"); socket = new Socket(inet,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(); } } } } //服务端 @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(); //不建议,可能会有乱码 /* byte[] buf = new byte[1024]; int len; while ((len = is.read(buf)) != -1){ System.out.print(new String(buf,0,len)); }*/ //4.读取输入流中的数据 baos = new ByteArrayOutputStream(); byte[] buf = new byte[5]; int len; while ((len = is.read(buf)) != -1){ baos.write(buf,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.Pan.JavaWeb; import org.junit.Test; import java.io.*; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; /** * 实现TCP的网络编程 * 例题2:客户端发送文件给服务端,服务端将文件保存在本地 * @author Pan */ 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"),9090); os = socket.getOutputStream(); fis = new FileInputStream(new File("Test.jpg")); byte[] buf = new byte[1024]; int len; while ((len = fis.read(buf)) != -1){ os.write(buf,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(); } } } } @Test public void server() { ServerSocket ss = null; Socket socket = null; InputStream is = null; try { ss = new ServerSocket(9090); socket = ss.accept(); is = socket.getInputStream(); FileOutputStream fos = new FileOutputStream(new File("Test1.jpg")); byte[] buf = new byte[1024]; int len; while ((len = is.read(buf)) != -1){ fos.write(buf,0,len); fos.flush(); } } catch (IOException e) { e.printStackTrace(); } finally { if (ss != null){ try { ss.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(); } } } } }
package com.Pan.JavaWeb; import org.junit.Test; import java.io.*; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.nio.charset.StandardCharsets; /** * 实现TCP的网络编程 * 例题3:从客户端发送文件给服务器,服务器端保存到本地,并返回"发送成功"给客户端 * 并关闭相应连接 * @author Pan */ public class TCPTest3 { //客户端 @Test public void client() throws IOException { Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9090); OutputStream os = socket.getOutputStream(); FileInputStream fis = new FileInputStream("Test.jpg"); byte[] buf = new byte[1024]; int len; while ((len = fis.read(buf)) != -1){ os.write(buf,0,len); } //关闭数据的输出 socket.shutdownOutput(); InputStream is = socket.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf1 = new byte[20]; int len1; while ((len1 = is.read(buf1)) != -1){ baos.write(buf1,0,len1); } System.out.println(baos.toString()); socket.close(); os.close(); fis.close(); is.close(); baos.close(); } //服务器端 @Test public void server() throws IOException { ServerSocket ss = new ServerSocket(9090); Socket socket = ss.accept(); InputStream is = socket.getInputStream(); FileOutputStream fos = new FileOutputStream("Test2.jpg"); byte[] buf = new byte[1024]; int len; while ((len = is.read(buf)) != -1){ fos.write(buf,0,len); } OutputStream os = socket.getOutputStream(); os.write("发送成功".getBytes()); ss.close(); socket.close(); is.close(); fos.close(); os.close(); } }
-
-
UDP协议
-
将数据、源、目的封装成数据包,不需要建立连接
-
每个数据报的大小限制在64K内
-
发送不管对方是否准备好,接收方收到也不确认,故是不可靠的
-
可以广播发送
-
发送数据结束时无序释放资源,开销小,速度快
package com.Pan.JavaWeb; import org.junit.Test; import java.io.IOException; import java.net.*; /** * UDP协议的网络编程 * @author Pan */ public class UDPTest { @Test //发送端 public void sender() throws IOException { DatagramSocket socket = new DatagramSocket(); String str = "我是UDP发送的数据"; byte[] data = str.getBytes(); DatagramPacket packet = new DatagramPacket(data, data.length, InetAddress.getLocalHost(),9090); socket.send(packet); socket.close(); } @Test //接收端 public void receiver() throws IOException { DatagramSocket socket = new DatagramSocket(9090); byte[] buf = new byte[100]; DatagramPacket packet = new DatagramPacket(buf, 0,buf.length); socket.receive(packet); System.out.println(new String(packet.getData(),0,packet.getLength())); socket.close(); } }
-
URL网络编程
-
URL:统一资源定位符,他表示Internet上某一资源的地址
-
URL的基本结构由5部分组成:<传输协议>://<主机名>:<端口号>/<文件名>#片段名?参数列表
package com.Pan.JavaWeb; import java.net.MalformedURLException; import java.net.URL; /** * URL网络编程 * * @author Pan */ public class URLTest { public static void main(String[] args) { try { URL url = new URL("http://localhost:8080/examples/hello.txt?username=Tom"); System.out.println(url.getProtocol());//获取该URL的协议名 System.out.println(url.getHost());//获取该URL的主机名 System.out.println(url.getPort());//获取该URL的端口号 System.out.println(url.getPath());//获取该URL的文件路径 System.out.println(url.getFile());//获取该URL的文件名 System.out.println(url.getQuery());//获取该URL的查询名 } catch (MalformedURLException e) { e.printStackTrace(); } } }
package com.Pan.JavaWeb; 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/hello.txt"); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.connect(); is = urlConnection.getInputStream(); fos = new FileOutputStream("hello2.txt"); byte[] buf = new byte[1024]; int len; while ((len = is.read(buf)) != -1){ fos.write(buf,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(); } } } }