Java网络编程
1、IP地址:InetAddress
2、端口号
- 端口号标识正在计算机上运行的程序(进程)
- 端口号的范围:0~65535
- 端口分类:
- 公认端口:0~1023。被预先定义的服务通信占用(如HTTP占用端口80,FTP占用端口21,Telnet占用端口23)
- 注册端口:1024~49151。分配给用户进程或应用程序(如Tomcat占用端口8080,Mysql占用端口3306等)
- 动态/私有端口:49152~65535
- 端口号与IP地址的组合得出一个网络套接字:Socket。
3、网络协议
3.1、TCP/IP协议簇:
- 传输层协议中有两个重要的协议:
- 传输控制协议TCP(Transmission Control Protocol)
- 用户数据报协议UDP(User Datagram Protocol)
- TCP/IP以其两个主要协议:传输控制协议(TCP)和网络互联协议(IP)而得名,实际上是一组协议,包括多个具有不同功能且互为关联的协议。
- IP(Internet Protocol)协议是网络层的主要协议,支持网间互联的数据通信。
- TCP/IP协议模型从更实用的角度出发,形成了高效的四层体系结构,即:物理链路层、IP层、传输层和应用层。
3.2、UDP协议与TCP协议的区别:
- UDP协议——发短信
- 将数据、源和目的封装成数据包中,不需要建立连接;
- 每个数据报的大小在限制在64k内;
- 发送不管对方是否准备好,接收方收到也不确认,故不可靠
- 可以广播发送
- 发送数据结束时无需释放资源,开销小,速度快
- TCP协议——打电话、视频
- 使用前必须先建立TCP连接,形成传输数据的通道;
- 传输前使用”三次握手“方式,点对点通信,是可靠的
- TCP协议进行通信的两个应用进程:客户端、服务端
- 在连接中可进行大数据量的传输;
- 传输完毕,需要释放已建立的连接,效率低;
4、TCP网络编程
4.1、模拟客户端给服务端发送消息,服务端接收消息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
@Test public void client() { Socket socket = null; OutputStream outputStream = null; try { InetAddress ip = InetAddress.getByName("127.0.0.1"); socket = new Socket(ip, 8848); outputStream = socket.getOutputStream(); outputStream.write("你好!这是客户端发送的一条数据。".getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
@Test public void server() { ServerSocket serverSocket = null; InputStream inputStream = null; Socket socket = null; ByteArrayOutputStream stream = null; try { serverSocket = new ServerSocket(8848); socket = serverSocket.accept(); inputStream = socket.getInputStream(); stream = new ByteArrayOutputStream(); byte[] bytes = new byte[5]; int len; while ((len = inputStream.read(bytes)) != -1) { stream.write(bytes, 0, len); } System.out.println("收到了来自" + socket.getInetAddress().getHostName() + "的消息:" + stream.toString()); } catch (IOException e) { e.printStackTrace(); } finally { try { if (serverSocket != null) serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } try { if (socket != null) socket.close(); } catch (IOException e) { e.printStackTrace(); } try { if (inputStream != null) inputStream.close(); } catch (IOException e) { e.printStackTrace(); } try { if (stream != null) stream.close(); } catch (IOException e) { e.printStackTrace(); } } }
|
4.2、模拟客户端给服务端发送文件,服务端接收文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
@Test public void client() throws IOException { Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),8888); OutputStream outputStream = socket.getOutputStream(); FileInputStream fileInputStream = new FileInputStream("1.jpg"); byte[] bytes = new byte[1024]; int len; while ((len = fileInputStream.read(bytes))!=-1){ outputStream.write(bytes,0,len); } socket.close(); fileInputStream.close(); outputStream.close(); }
@Test public void server() throws IOException { ServerSocket serverSocket = new ServerSocket(8888); Socket socket = serverSocket.accept(); InputStream inputStream = socket.getInputStream(); FileOutputStream fileOutputStream = new FileOutputStream("2.jpg"); byte[] bytes = new byte[1024]; int len; while ((len = inputStream.read(bytes))!=-1){ fileOutputStream.write(bytes,0,len); } serverSocket.close(); socket.close(); inputStream.close(); fileOutputStream.close(); }
|
4.3、模拟客户端给服务端发送文件,服务端接收文件,并且给予客户端反馈,客户端接收反馈并打印反馈信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
@Test public void client() throws IOException { FileInputStream fileInputStream = new FileInputStream("1.jpg"); Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 8999); OutputStream outputStream = socket.getOutputStream(); byte[] bytes = new byte[1024]; int len; while ((len = fileInputStream.read(bytes)) != -1) { outputStream.write(bytes, 0, len); } socket.shutdownOutput(); InputStream inputStream = socket.getInputStream(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] bytes1 = new byte[5]; int len1; while ((len1 = inputStream.read(bytes1)) != -1) { byteArrayOutputStream.write(bytes1, 0, len1); } System.out.println(byteArrayOutputStream.toString());
socket.close(); fileInputStream.close(); outputStream.close(); inputStream.close(); byteArrayOutputStream.close(); }
@Test public void server() throws IOException { ServerSocket serverSocket = new ServerSocket(8999); Socket socket = serverSocket.accept(); InputStream inputStream = socket.getInputStream(); FileOutputStream fileOutputStream = new FileOutputStream("2.jpg"); byte[] bytes = new byte[1024]; int len; while ((len = inputStream.read(bytes)) != -1) { fileOutputStream.write(bytes, 0, len); } OutputStream outputStream = socket.getOutputStream(); outputStream.write("服务器已经接收文件!".getBytes());
serverSocket.close(); socket.close(); inputStream.close(); outputStream.close(); fileOutputStream.close(); }
|
5、UDP网络编程
5.1、发送端发送数据,接收端接收数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
@Test public void sender() throws Exception { DatagramSocket socket = new DatagramSocket(); String str = "我是UDP发送的数据"; byte[] data = str.getBytes(); InetAddress ip = InetAddress.getByName("127.0.0.1"); DatagramPacket packet = new DatagramPacket(data, 0, data.length, ip, 8848); socket.send(packet); socket.close(); }
@Test public void receiver() throws Exception { DatagramSocket socket = new DatagramSocket(8848); byte[] buffer = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length); socket.receive(packet); System.out.println(new String(packet.getData(), 0, packet.getLength())); socket.close(); }
|
6、URL编程
- URL(Uniform Resource Locator):统一资源定位符,它表示Internet上某一资源的地址
- 它是一种具体的URI,即URL可以用来标识一个资源,而且还指明了如何locate这个资源
- URL的基本结构由5部分组成:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @Test public void Test(){ try { URL url = new URL("http://localhost:8080/shop/index.html?username=Tom"); System.out.println(url.getProtocol()); System.out.println(url.getHost()); System.out.println(url.getPort()); System.out.println(url.getPath()); System.out.println(url.getFile()); System.out.println(url.getQuery()); } catch (Exception e){ e.printStackTrace(); } }
|
从服务器下载资源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| @Test public void Test() { HttpURLConnection connection = null; InputStream inputStream = null; FileOutputStream fileOutputStream = null; try { URL url = new URL("http://localhost:8080/hello/1.jpg"); connection = (HttpURLConnection) url.openConnection(); connection.connect(); inputStream = connection.getInputStream(); fileOutputStream = new FileOutputStream("3.jpg"); byte[] bytes = new byte[1024]; int len; while ((len = inputStream.read(bytes)) != -1) { fileOutputStream.write(bytes, 0, len); } System.out.println("下载完成"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (inputStream != null) inputStream.close(); } catch (IOException e) { e.printStackTrace(); } try { if (fileOutputStream != null) fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } if (connection != null) connection.disconnect(); } }
|