网络编程 发表于 2020-03-13 | 分类于 Java 字数统计: 989 | 阅读时长 ≈ 5 TCP网络编程使用TCP连接进行通信的客户端和服务端 12345678910111213141516171819202122232425262728293031323334@Test // 客户端public void client() { // 1.创建Socket对象,指明服务器端的IP和端口号 Socket socket = null; OutputStream os = null; try { InetAddress inet = InetAddress.getByName("127.0.0.1"); socket = new Socket(inet, 9999); // 2.创建输出流 os = socket.getOutputStream(); // 3.向外写数据 os.write("hello? hello? hello?".getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { try { if (os != null) { // 4.关闭输入流 os.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (socket != null) { // 5.关闭Socket socket.close(); } } catch (IOException e) { e.printStackTrace(); } }} 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556@Test // 服务端 public void server() { Socket socket = null; InputStream is = null; ByteOutputStream byteOutputStream = null; ServerSocket serverSocket = null; try { // 1.创建服务器端的ServerSocket,指明端口号 serverSocket = new ServerSocket(9999); // 2.接受来自客户端的Socket socket = serverSocket.accept(); // 3.创建输入流 is = socket.getInputStream(); byteOutputStream = new ByteOutputStream(); byte[] buffer = new byte[5]; int len; // 4.读取数据 while ((len = is.read(buffer)) != -1) { // 将buffer数组中的数据写到ByteOutputStream类的一个数组中 byteOutputStream.write(buffer, 0, len); } System.out.println("收到消息,来自:"+socket.getInetAddress()); System.out.println(byteOutputStream.toString()); } catch (IOException e) { e.printStackTrace(); } finally {// 5.关闭流和socket if (byteOutputStream != null) { try { byteOutputStream.close(); } catch (Exception 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 (serverSocket != null) { try { serverSocket.close(); } catch (Exception e) { e.printStackTrace(); } } } } 使用TCP实现文件传输123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 @Test public void client() throws IOException { // 创建socket并绑定IP和端口号 Socket socket = new Socket("127.0.0.1",9999); // 创建输出流,发送数据 OutputStream outputStream = socket.getOutputStream(); // 创建文件输入流,外部读取文件内容 FileInputStream fi = new FileInputStream(new File("../java_08/pic2.png")); byte[] buffer = new byte[20]; int len; while ((len = fi.read(buffer))!=-1) { outputStream.write(buffer,0,len); } // 关闭数据输出 socket.shutdownOutput(); // 客户端接收服务端的反馈信息 InputStream inputStream = socket.getInputStream(); ByteOutputStream byteOutputStream = new ByteOutputStream(); byte[] buffer1 = new byte[20]; int len1; while((len=inputStream.read(buffer1))!=-1){ byteOutputStream.write(buffer1,0,len); } System.out.println(byteOutputStream.toString()); byteOutputStream.close(); inputStream.close(); outputStream.close(); fi.close(); socket.close(); } @Test public void server() throws IOException { // 创建服务端ServerSocket并绑定端口 ServerSocket serverSocket = new ServerSocket(9999); // 接收客户端socket Socket socket = serverSocket.accept(); // 创建输入流,接收数据 InputStream inputStream = socket.getInputStream(); // 创建文件输出流,用于保存接收到的文件 FileOutputStream fo = new FileOutputStream(new File("rec1.png")); byte[] buffer = new byte[20]; int len; while((len = inputStream.read(buffer))!=-1){ fo.write(buffer,0,len); } // 服务器反馈消息给客户端 OutputStream outputStream = socket.getOutputStream(); outputStream.write("图片已接收到!".getBytes()); outputStream.close(); fo.close(); inputStream.close(); socket.close(); serverSocket.close(); }} UDP网络编程123456789101112131415161718192021222324252627282930@Testpublic void sender() throws IOException { // 1.创建套接字 DatagramSocket socket = new DatagramSocket(); InetAddress inet = InetAddress.getByName("127.0.0.1"); String str = "哈哈哈哈哈哈哈~"; byte[] data = str.getBytes(); // 2.初始化数据包,绑定IP,和端口号,确定数据的字节长度 DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9999); // 3.发送数据包 socket.send(packet); // 4.关闭Socket socket.close();}@Testpublic void receiver() throws IOException { // 1.创建套接字 DatagramSocket socket = new DatagramSocket(9999); // 2.创建用于接收数据的字节数组 byte[] buffer = new byte[1024]; // 3.创建数据包 DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length); // 4.接收数据 socket.receive(packet); // 5.将接收的数据打印到控制台 System.out.println(new String(packet.getData(),0,packet.getLength())); // 6.关闭socket socket.close();} URL编程12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849@Test public void test() { HttpURLConnection con = null; InputStream is = null; FileOutputStream fo = null; try { // 创建URL对象 URL url = new URL("http://localhost:8888/index.html"); // 创建连接对象 con = (HttpURLConnection) url.openConnection(); // 创建连接 con.connect(); // 创建输入流,用于读取数据 is = con.getInputStream(); // 创建文件输出流,将读取的数据写到本地 fo = new FileOutputStream(new File("index.html")); byte[] buffer = new byte[20]; int len; while ((len = is.read(buffer)) != -1) { fo.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); } finally { } if (fo != null) { try { fo.close(); } catch (IOException e) { e.printStackTrace(); } if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (con != null) { try { con.disconnect(); } catch (Exception e) { e.printStackTrace(); } } } } 本文作者: Zhangyuzhen 本文链接: https://sweetboyzhang.github.io/2020/03/13/java-09/ 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!