package com.atguigu.javase.net; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; // 客户端给服务器发送一个字符串,服务器收到后给客户端回复消息确认 public class NetTest { @Test public void server() { ServerSocket server = null; // 服务套接字 Socket socket1 = null; InputStream is = null; // 网络输入流 InputStreamReader isr = null; // 转换流 BufferedReader bufReader = null; // 缓冲流 OutputStream os = null; // 网络输出流 OutputStreamWriter osw = null; // 转换流 BufferedWriter bufWriter = null; // 缓冲流 try { server = new ServerSocket(9999); // 服务器绑定在端口号9999上 socket1 = server.accept(); // accept方法会导致程序阻塞, 等待客户端的连接请求, 一旦接受就返回一个Socket对象 System.out.println(socket1); is = socket1.getInputStream(); isr = new InputStreamReader(is); bufReader = new BufferedReader(isr); String line = bufReader.readLine(); System.out.println(line); os = socket1.getOutputStream(); osw = new OutputStreamWriter(os); bufWriter = new BufferedWriter(osw); bufWriter.write("我收到了你的消息,请放心!!"); bufWriter.newLine(); bufWriter.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { bufReader.close(); } catch (Exception e2) { } try { bufWriter.close(); } catch (Exception e2) { } try { socket1.close(); } catch (Exception e2) { } try { server.close(); } catch (Exception e2) { } } } @Test public void client() { Socket socket2 = null; OutputStream os = null; OutputStreamWriter osw = null; BufferedWriter bufWriter = null; InputStream is = null; InputStreamReader isr = null; BufferedReader bufReader = null; try { socket2 = new Socket("127.0.0.1", 9999); // 创建Socket对象的过程,就是发起请求的过程, 连接到服务器需要的信息是主机(IP)和端口Port System.out.println(socket2); os = socket2.getOutputStream(); // 获取输出流对象 osw = new OutputStreamWriter(os); bufWriter = new BufferedWriter(osw); bufWriter.write("你好服务器, 我是客户端 !!"); bufWriter.newLine(); bufWriter.flush(); // 把数据刷入网线 is = socket2.getInputStream(); isr = new InputStreamReader(is); bufReader = new BufferedReader(isr); String line = bufReader.readLine(); System.out.println(line); } catch (Exception e) { e.printStackTrace(); } finally { try { bufWriter.close(); } catch (Exception e2) { } try { bufReader.close(); } catch (Exception e2) { } try { socket2.close(); } catch (Exception e2) { } } } } class ExerServer { // 服务器主动给客户端 发送一个UTF8字符串, 客户端接收到这个字符串以反向处理再返回给服务器 public static void main(String[] args) { ServerSocket server = null; Socket socket1 = null; BufferedWriter bufWriter = null; BufferedReader bufReader = null; try { server = new ServerSocket(8888);// 对象一旦创建成功, 标志此程序和端口8888绑定 while (true) { System.out.println("服务器正在8888端口监听........"); try { socket1 = server.accept(); bufWriter = new BufferedWriter(new OutputStreamWriter(socket1.getOutputStream(), "UTF8")); bufReader = new BufferedReader(new InputStreamReader(socket1.getInputStream(), "UTF8")); bufWriter.write("随便写个字符串, 发送给客户端, 我是服务器"); bufWriter.newLine(); bufWriter.flush(); String line = bufReader.readLine(); System.out.println(line); } catch (Exception e) { } finally { try { bufReader.close(); } catch (Exception e2) { } try { bufWriter.close(); } catch (Exception e2) { } try { socket1.close(); } catch (Exception e2) { } } } } catch (Exception e) { e.printStackTrace(); } finally { try { server.close(); } catch (Exception e2) { } } } } class ExerClient { // 客户端接收到这个字符串以反向处理再返回给服务器 public static void main(String[] args) { Socket socket2 = null; BufferedReader bufReader = null; BufferedWriter bufWriter = null; try { socket2 = new Socket("localhost", 8888); bufReader = new BufferedReader(new InputStreamReader(socket2.getInputStream(), "UTF8")); bufWriter = new BufferedWriter(new OutputStreamWriter(socket2.getOutputStream(), "UTF8")); String line = bufReader.readLine(); System.out.println(line); line = new StringBuilder(line).reverse().toString(); bufWriter.write(line); bufWriter.newLine(); bufWriter.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { bufReader.close(); } catch (Exception e2) { } try { socket2.close(); } catch (Exception e2) { } } } } package com.atguigu.javase.net; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; class TestServer2 { // 接收从客户端发送的文件, 并保存在本地, 处理完后,给客户端反馈 public static void main(String[] args) { ServerSocket server = null; Socket socket1 = null; InputStream is = null; // 网络输入流 FileOutputStream fos = null; // 文件输出流 OutputStream os = null; // 网络输出流 try { server = new ServerSocket(6666); socket1 = server.accept(); is = socket1.getInputStream(); os = socket1.getOutputStream(); fos = new FileOutputStream("girl2.jpg"); byte[] buf = new byte[8192]; int realCount = is.read(buf); // 从网络输入流中读取数据 while (realCount != -1) { fos.write(buf, 0, realCount); // 把读到的数据写入本地文件中 realCount = is.read(buf); // 网络的另一端关闭了输出 返回-1 } os.write("接收完毕".getBytes()); // 通过网络输出流反馈一个字符串 os.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { fos.close(); } catch (Exception e2) { } try { is.close(); } catch (Exception e2) { } try { os.close(); } catch (Exception e2) { } try { socket1.close(); } catch (Exception e2) { } try { server.close(); } catch (Exception e2) { } } } } class TestClient2 { // 发送一个本文件到服务器端 , 再接收服务器反馈 public static void main(String[] args) { Socket socket2 = null; FileInputStream fis = null; // 读本地文件的输入流 OutputStream os = null; // 发送文件,网络输出流 InputStream is = null; // 网络输入流 try { String host = "localhost"; int port = 6666; socket2 = new Socket(host, port); os = socket2.getOutputStream(); is = socket2.getInputStream(); fis = new FileInputStream("girl.jpg"); byte[] buf = new byte[8192]; int realCount = fis.read(buf); //从文件输入流中获取数据 while (realCount != -1) { // 1) 处理 os.write(buf, 0, realCount); // 把读到的数据直接写入网络输出流 // 2) 继续 realCount = fis.read(buf); } socket2.shutdownOutput(); // 关闭输出流, 发送一个终止信号给网络另一端 realCount = is.read(buf); // 从网络输入流中获取反馈信息,是一个字符串, 所以需要解码 String line = new String(buf, 0, realCount); System.out.println(line); } catch (Exception e) { e.printStackTrace(); } finally { try { fis.close(); } catch (Exception e2) { } try { os.close(); } catch (Exception e2) { } try { is.close(); } catch (Exception e2) { } try { socket2.close(); } catch (Exception e2) { } } } } //使用UDP发送一个字符串 package com.atguigu.javase.net; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import org.junit.Test; public class NetTest3 { @Test public void udpSender() throws Exception { DatagramSocket ds = new DatagramSocket(); byte[] data = "来一个数据,是一个字符串".getBytes(); DatagramPacket dp = new DatagramPacket(data, 0, data.length, InetAddress.getByName("127.0.0.1"), 10000); ds.send(dp); // 把数据包发送给硬件 ds.close(); } @Test public void udpReceiver() throws Exception { DatagramSocket ds = new DatagramSocket(10000); // 绑定端口 byte[] buf = new byte[8192]; DatagramPacket dp = new DatagramPacket(buf, buf.length); // 接收数据 ds.receive(dp); // 接收到的数据保存在DatagramPacket对象中 String string = new String(buf, 0, dp.getLength()); System.out.println(string); ds.close(); } } package com.atguigu.javase.net; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; import org.junit.Test; // 创建一个服务器可以接收从客户端传来的信息,并在控制台上打印输出, // 客户端接收键盘输入的字符串,并发送到服务器,这里需要创建多线程,以防止多个客户端连接服务器时,其中一个服务器发生// 故障时,服务器仍能运行 class TcpService implements Runnable { private Socket socket1; public TcpService(Socket socket1) { this.socket1 = socket1; } @Override public void run() { BufferedReader bufReader = null; try { bufReader = new BufferedReader(new InputStreamReader(socket1.getInputStream())); String line = bufReader.readLine(); while (line != null) { System.out.println(line); line = bufReader.readLine(); } } catch (Exception e) { e.printStackTrace(); } finally { try { bufReader.close(); } catch (Exception e2) { } try { socket1.close(); } catch (Exception e2) { } } } } class TestServer1 { public static void main(String[] args) { ServerSocket server = null; try { server = new ServerSocket(7777); while (true) { System.out.println("服务器在端口7777监听..."); Socket socket1 = server.accept(); new Thread(new TcpService(socket1)).start(); } } catch (Exception e) { e.printStackTrace(); } finally { try { server.close(); } catch (IOException e) { e.printStackTrace(); } } } } class TestClient1 { public static void main(String[] args) { Socket socket2 = null; BufferedReader bufReader = null; BufferedWriter bufWriter = null; try { socket2 = new Socket("localhost", 7777); bufReader = new BufferedReader(new InputStreamReader(System.in)); bufWriter = new BufferedWriter(new OutputStreamWriter(socket2.getOutputStream())); String line = bufReader.readLine(); while (line != null) { bufWriter.write(line); bufWriter.newLine(); bufWriter.flush(); line = bufReader.readLine(); } } catch (Exception e) { e.printStackTrace(); } finally { try { bufReader.close(); } catch (Exception e2) { } try { bufWriter.close(); } catch (Exception e2) { } try { socket2.close(); } catch (Exception e2) { } } } } package com.atguigu.javase.net; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; /* * 创建服务器和客户端,使得服务器和客户端可以互相打字进行交流 */ class Tcp implements Runnable { // 接收从网络获取到的信息,并在控制台上,打印输出 private Socket socket; public Tcp(Socket socket) { this.socket = socket; } @Override public void run() { BufferedReader bufReader = null; try { bufReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line = bufReader.readLine(); while (line != null) { System.out.println(line); line = bufReader.readLine(); } } catch (Exception e) { e.printStackTrace(); } finally { try { bufReader.close(); } catch (Exception e2) { } try { socket1.close(); } catch (Exception e2) { } } } } class TestServer3 { public static void main(String[] args) { // 创建客户端 ServerSocket server = null; Socket socket1 = null; BufferedReader br = null; // 键盘输入流 BufferedReader br1 = null; // 网络输入流 BufferedWriter bw =null; // 网络输出流 try { server = new ServerSocket(8888); System.out.println("服务器正在端口8888监听......"); socket1 = server.accept(); br = new BufferedReader(new InputStreamReader(System.in)); bw = new BufferedWriter(new OutputStreamWriter(socket1.getOutputStream())); br1 = new BufferedReader(new InputStreamReader(socket1.getInputStream())); // 创建线程,并运行 new Thread(new Tcp(socket1)).start(); // 键盘输入的字符串并传给客户端 String line = br.readLine(); while (line != null) { bw.write(line); bw.newLine(); bw.flush(); line = br.readLine(); } } catch (Exception e) { e.printStackTrace(); } finally { try { br1.close(); } catch (IOException e) { } try { bw.close(); } catch (IOException e) { } try { socket1.close(); } catch (IOException e) { } try { server.close(); } catch (IOException e) { } } } } class TestClient3 { public static void main(String[] args) { Socket socket2 = null; BufferedReader br = null; // 网络输入流 BufferedReader br2 = null; // 键盘输入流 BufferedWriter bw = null; // 网络输出流 try { socket2 = new Socket("127.0.0.1" ,8888); br = new BufferedReader(new InputStreamReader(socket2.getInputStream())); br2 = new BufferedReader(new InputStreamReader(System.in)); bw = new BufferedWriter(new OutputStreamWriter(socket2.getOutputStream())); // 创建线程并运行 new Thread(new Tcp(socket2)).start(); // 接收键盘传入的字符串,并发送到服务器端 String line2 = br2.readLine(); while (line2 != null) { bw.write(line2); bw.newLine(); bw.flush(); line2 = br2.readLine(); } } catch (Exception e) { e.printStackTrace(); } finally { try { bw.close(); } catch (IOException e) { } try { br2.close(); } catch (IOException e) { } try { br.close(); } catch (IOException e) { } try { socket2.close(); } catch (IOException e) { } } } }