网络编程java

这篇博客探讨了TCP和UDP在简单网络编程中的使用,包括自定义客户端和服务器之间的图片传输。此外,还介绍了如何通过浏览器与Tomcat服务器建立连接,展示了一个HTTP请求读取服务器上文件的例子。同时,文章也提到了UDP的发送和接收操作,以及使用URL从Tomcat服务器下载文件的示例。
摘要由CSDN通过智能技术生成

在简单的网络编程中,需要服务器与客户端的连接

一.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();
               
          }
         
     }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海边的彩虹与你

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值