网络编程java
网络编程,听起来高大上,但其实就是一个“传声筒”游戏。就像你在教室里传纸条,服务器和客户端就是两个同学,纸条就是数据。TCP和UDP就是两种不同的传纸条方式。
首先,TCP,就像你亲手把纸条递给对方,安全可靠,但效率低,对方可能会磨蹭半天才回你。例子就是上面的代码,客户端把图片传给服务器,服务器再确认收到。这就像你发一张照片给朋友,朋友看完再回你“收到”。虽然稳,但慢啊!
然后是UDP,就像你把纸条扔向对方,不管对方接没接到,效率高但不可靠。上面的代码就是发送方把信息打包扔出去,接收方看运气接。这就像你在微信上发“我爱你”,对方可能在忙,根本没看见。快是快,但不保准啊!
最后,URL就是通过浏览器和Tomcat服务器连接,下载数据。这就像你在网上下载一首歌,点一下,等一会儿,歌就到了。简单粗暴,但常用。
所以,网络编程就是选择不同的“传纸条”方式,关键看你是要稳,还是要快。至于怎么选,那就是你的自由了!
在简单的网络编程中,需要服务器与客户端的连接
一.TCP的简单使用(可信但效率低)
①自定义客户端与服务器,进行图片的复制
客户端
public void client() { OutputStream os = null; FileInputStream file1 = null; InputStream is = null; ByteArrayOutputStream bo = null; try { //ip地址 Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9090); os = socket.getOutputStream(); file1 = new FileInputStream(new File("OIP1-C.jpg")); //读取图片的信息 byte[] buffer = new byte[100]; int lent; while ((lent = file1.read(buffer)) != -1) { os.write(buffer,0,lent); } //告诉服务器传输的数据结束 socket.shutdownInput(); //读入来自服务器的数据 is = socket.getInputStream(); bo = new ByteArrayOutputStream(); byte[] arr = new byte[100]; int len; while ((len = is.read(arr) ) != -1) { bo.write(arr,0,len); } System.out.println(bo.toString()); } catch (IOException e) { e.printStackTrace(); } finally { //流的关闭 if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if (file1 != null) { try { file1.close(); } catch (IOException e) { e.printStackTrace(); } } if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (bo != null) { try { bo.close(); } catch (IOException e) { e.printStackTrace(); } } } }
服务器
public void server() { ServerSocket ss = null; Socket accept = null; InputStream is = null; FileOutputStream file2 = null; OutputStream outputStream = null; try { //自己的端口值 ss = new ServerSocket(9090); accept = ss.accept(); is = accept.getInputStream(); file2 = new FileOutputStream("`OIP12-C.jpg"); //复制图片的操作 byte[] buffer = new byte[100]; int lent; while ((lent = is.read()) != -1) { file2.write(buffer,0,lent); } //传给客户端的信息 outputStream = accept.getOutputStream(); outputStream.write("已收到".getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { e.printStackTrace(); } finally { while (ss != null) { try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } while (accept != null) { try { accept.close(); } catch (IOException e) { e.printStackTrace(); } } while (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } while (file2 != null) { try { file2.close(); } catch (IOException e) { e.printStackTrace(); } } while (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
②使用浏览器与Tomcat服务器的连接
首先需要配置tomcat,在控制台输入catalina run后在浏览器输入 http://localhost:8080
可随意在一个地方创建一个txt,然后在浏览器输入http://localhost:8080/1/hello.txt(举例)
便可在网页看到自己在文本输入的内容了
2.UDP(不可信但效率高)
发送
public void sender() { DatagramSocket socket = null; try { socket = new DatagramSocket(); String arr = "我传输了信息过来"; byte[] data = arr.getBytes(StandardCharsets.UTF_8); InetAddress ient = InetAddress.getLocalHost(); DatagramPacket packet = new DatagramPacket(data,0,data.length,ient,8080); //将信息打包并发送出去 socket.send(packet); } catch (IOException e) { e.printStackTrace(); } finally { if(socket != null) socket.close(); } }
接收
public void reader() throws Exception { //服务器的端口 DatagramSocket data = new DatagramSocket(8080); byte[] bytes = new byte[100]; DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length); data.receive(packet); System.out.println(new String(packet.getData(),0,packet.getLength())); }
③URL(较为常用)实现tomcat服务端数据下载
public static void main(String[] args) { HttpURLConnection urlConnection = null; InputStream inputStream = null; FileOutputStream fileOutputStream = null; try { URL url = new URL("http://localhost:8080/1/bea.jpg"); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.connect(); inputStream = urlConnection.getInputStream(); fileOutputStream = new FileOutputStream("bea1,jpg"); byte[] bytes = new byte[1000]; int len; while ((len = inputStream.read()) != -1) { fileOutputStream.write(bytes,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (urlConnection != null) urlConnection.disconnect(); }}