Java 原生网络编程.

匿名 (未验证) 提交于 2019-12-02 21:53:52

一、概念

    public static void uRLTest(){         try {             //URL:统一资源定位符(网址)             URL url = new URL("http://www.baidu.com");             InputStream inputStream = url.openStream();             InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");             BufferedReader bufferedReader = new BufferedReader(inputStreamReader);             String line;             while ((line = bufferedReader.readLine())!=null){                 System.out.println(line);             }         } catch (UnknownHostException e) {             e.printStackTrace();         } catch (MalformedURLException e) {             e.printStackTrace();         } catch (IOException e) {             e.printStackTrace();         }     }

public static void uRLConnectionTest(){     try {         URL url = new URL("http://restapi.amap.com/v3/config/district?key=2c95fdacd3f72bdbfec55bd7eac7b5c0");         //1、创建连接对象         URLConnection urlConnection = url.openConnection();         HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;         //2、设置参数和一般请求属性         //应用程序要将参数写入URL连接         httpURLConnection.setDoOutput(true);         //应用程序要向URL连接读取数据         httpURLConnection.setDoInput(true);         //不使用缓存         httpURLConnection.setUseCaches(false);         //设置请求参数         httpURLConnection.setRequestProperty("Content-type","application/json");         //设置请求方法         httpURLConnection.setRequestMethod("POST");         //3、使用connect方法建立到远程对象的实际链接         httpURLConnection.connect();         //4、远程对象变为输入输出流,根据需求进一步操作         InputStream inputStream = httpURLConnection.getInputStream();         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));         BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("E:\\data.json"));         String line;         while ((line = bufferedReader.readLine()) != null){             bufferedWriter.write(line);             bufferedWriter.newLine();             bufferedWriter.flush();         }     } catch (MalformedURLException e) {         e.printStackTrace();     } catch (IOException e) {         e.printStackTrace();     } }

三、基于TCP的Socket编程

public static void main(String[] args) {     try {         System.out.println("启动服务器......");         //创建一个新的ServerSocket,用来监听指定端口上的连接请求         ServerSocket serverSocket = new ServerSocket(8989);         Socket socket = null;         while (true) {             try {                 //对accept()方法的调用将被阻塞,直到一个连接创建                 //该socket用于客户端和服务器之间的通信                 //流对象都派生于该套接字的流对象                 socket = serverSocket.accept();                 InputStream inputStream = socket.getInputStream();                 BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));                 PrintWriter out = new PrintWriter(socket.getOutputStream(), true);                 String request, response;                 while ((request = in.readLine()) != null) {                     //如果客户端发送了exit,则退出循环                     if ("exit".equals(request)) {                         break;                     }                     System.out.println("接收到客户端请求:" + request);                     //服务端处理方法                     response ="客户端:" + socket.getInetAddress().getHostAddress() + " 请求," + processRequest(request);                     //响应给客户端                     out.println(response);                 }             } catch (Exception e) {                 System.out.println("Error");             } finally {                 socket.close();             }         }     } catch (IOException e) {         e.printStackTrace();     } }

public static void main(String[] args) {     Socket socket = null;     try {         socket = new Socket("192.168.1.22", 8989);         //向服务器发送数据         //printWriter(字符流)/printStream(字节流)         PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);         //接收服务器的反馈         InputStream inputStream = socket.getInputStream();         BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));         //模拟交互         BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));         String request, responese;         while ((request = bf.readLine()) != null) {             printWriter.println(request);             responese = in.readLine();             System.out.println(responese);         }     } catch (IOException e) {         e.printStackTrace();     } finally {         try {             socket.close();         } catch (IOException e) {             e.printStackTrace();         }     } }

四、基于UDP的Socket编程

public static void main(String[] args) {     DatagramSocket server = null;     try {         server = new DatagramSocket(8088);         byte[] datas = new byte[1024];         //用一个字节数组接收UDP包,字节数组在传递给构造函数时是空的         DatagramPacket datagramPacket = new DatagramPacket(datas, datas.length);         server.receive(datagramPacket);         System.out.println(new String(datas));     } catch (SocketException e) {         e.printStackTrace();     } catch (IOException e) {         e.printStackTrace();     }finally {         server.close();     } }

public static void main(String[] args) {     DatagramSocket client = null;     try {         client = new DatagramSocket();         //创建数据报         byte[] buffer = "hello,world".getBytes();         InetAddress byName = InetAddress.getByName("192.168.1.22");         //建立将要传输的UDP包,并指定ip地址和端口号         DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length, byName, 8088);         client.send(datagramPacket);     } catch (SocketException e) {         e.printStackTrace();     } catch (UnknownHostException e) {         e.printStackTrace();     } catch (IOException e) {         e.printStackTrace();     } finally {         client.close();     } }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!