网络编程
TCP服务器端:
@Test public void test3() throws IOException { ServerSocket serverSocket = new ServerSocket(8888); Socket socket1 = serverSocket.accept(); System.out.println(socket1); OutputStream outputStream = socket1.getOutputStream(); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream); BufferedWriter bufferedWriter =new BufferedWriter(outputStreamWriter); LocalDateTime now = LocalDateTime.now(); DateTimeFormatter fmt =DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String format = fmt.format(now); bufferedWriter.write(format); bufferedWriter.flush(); bufferedWriter.close(); socket1.close(); serverSocket.close(); }
TCP客户端:
Socket socket =new Socket("127.0.0.1",8888); InputStream inputStream = socket.getInputStream(); InputStreamReader inputStreamReader =new InputStreamReader(inputStream); BufferedReader bufferedReader =new BufferedReader(inputStreamReader); String s = bufferedReader.readLine(); System.out.println(s); bufferedReader.close(); socket.close();
多线程版TCP 服务器:
package com.lagou.task19; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; public class ServerStringTest { public static void main(String[] args) { ServerSocket ss = null; Socket s = null; try { // 1.创建ServerSocket类型的对象并提供端口号 ss = new ServerSocket(8888); // 2.等待客户端的连接请求,调用accept方法 while(true) { System.out.println("等待客户端的连接请求..."); // 当没有客户端连接时,则服务器阻塞在accept方法的调用这里 s = ss.accept(); System.out.println("客户端" + s.getInetAddress() + "连接成功!"); // 每当有一个客户端连接成功,则需要启动一个新的线程为之服务 new ServerThread(s).start(); } } catch (IOException e) { e.printStackTrace(); } finally { // 4.关闭Socket并释放有关的资源 if (null != ss) { try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
package com.lagou.task19; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.InetAddress; import java.net.Socket; public class ServerThread extends Thread { private Socket s; public ServerThread(Socket s) { this.s = s; } @Override public void run() { BufferedReader br = null; PrintStream ps = null; try { // 3.使用输入输出流进行通信 br = new BufferedReader(new InputStreamReader(s.getInputStream())); ps = new PrintStream(s.getOutputStream()); while(true) { // 实现对客户端发来字符串内容的接收并打印 // 当没有数据发来时,下面的方法会形成阻塞 String s1 = br.readLine(); InetAddress inetAddress = s.getInetAddress(); System.out.println("客户端" + inetAddress + "发来的字符串内容是:" + s1); // 当客户端发来的内容为"bye"时,则聊天结束 if ("bye".equalsIgnoreCase(s1)) { System.out.println("客户端" + inetAddress + "已下线!"); break; } // 实现服务器向客户端回发字符串内容"I received!" ps.println("I received!"); System.out.println("服务器发送数据成功!"); } } catch (IOException e) { e.printStackTrace(); } finally { if (null != ps) { ps.close(); } if (null != br) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != s) { try { s.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
UDP 服务器:
package com.lagou.task19; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class SendTest { public static void main(String[] args) { DatagramSocket ds = null; try { // 1.创建DatagramSocket类型的对象 ds = new DatagramSocket(); // 2.创建DatagramPacket类型的对象并提供接收方的通信地址和端口号 byte[] bArr = "hello".getBytes(); DatagramPacket dp = new DatagramPacket(bArr, bArr.length, InetAddress.getLocalHost(), 8888); // 3.通过Socket发送Packet,调用send方法 ds.send(dp); System.out.println("发送数据成功!"); // 接收回发的数据内容 byte[] bArr2 = new byte[20]; DatagramPacket dp2 = new DatagramPacket(bArr2, bArr2.length); ds.receive(dp2); System.out.println("接收到的回发消息是:" + new String(bArr2, 0, dp2.getLength())); } catch (IOException e) { e.printStackTrace(); } finally { // 4.关闭Socket并释放有关的资源 if (null != ds) { ds.close(); } } } }
UDP客户端:
package com.lagou.task19; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class ReceiveTest { public static void main(String[] args) { DatagramSocket ds = null; try { // 1.创建DatagramSocket类型的对象并提供端口号 ds = new DatagramSocket(8888); // 2.创建DatagramPacket类型的对象并提供缓冲区 byte[] bArr = new byte[20]; DatagramPacket dp = new DatagramPacket(bArr, bArr.length); // 3.通过Socket接收数据内容存放到Packet里面,调用receive方法 System.out.println("等待数据的到来..."); ds.receive(dp); System.out.println("接收到的数据内容是:" + new String(bArr, 0, dp.getLength()) + "!"); // 实现将字符串内容"I received!"回发过去 byte[] bArr2 = "I received!".getBytes(); DatagramPacket dp2 = new DatagramPacket(bArr2, bArr2.length, dp.getAddress(), dp.getPort()); ds.send(dp2); System.out.println("回发数据成功!"); } catch (IOException e) { e.printStackTrace(); } finally { // 4.关闭Socket并释放有关的资源 if (null != ds) { ds.close(); } } } }
URL类:
URLConnection类:
java.net.URLConnection类是个抽象类,该类表示应用程序和URL之间的通信链接的所有类的超 类,主要实现类有支持HTTP特有功能的HttpURLConnection类。
HttpURLConnection类方法:
package com.lagou.task19; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class URLTest { public static void main(String[] args) { try { // 1.使用参数指定的字符串来构造对象 URL url = new URL("https://www.lagou.com/"); // 2.获取相关信息并打印出来 System.out.println("获取到的协议名称是:" + url.getProtocol()); System.out.println("获取到的主机名称是:" + url.getHost()); System.out.println("获取到的端口号是:" + url.getPort()); // 3.建立连接并读取相关信息打印出来 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); InputStream inputStream = urlConnection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); String str = null; while ((str = br.readLine()) != null) { System.out.println(str); } br.close(); // 断开连接 urlConnection.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }