From 17ee4d9231c2111a8da1d9737574ca13f2b3debe Mon Sep 17 00:00:00 2001 From: "Mr.Huang" <1123033157@qq.com> Date: Fri, 24 Nov 2017 09:29:26 +0800 Subject: [PATCH] update stop --- .../dream/socket/runnable/HandleRunnable.java | 1 + .../dream/socket/runnable/SendRunnable.java | 7 ++- src/test/java/com/dream/socket/TCPClient.java | 6 +++ src/test/java/com/dream/socket/TCPServer.java | 51 ++++++++++++++----- 4 files changed, 47 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/dream/socket/runnable/HandleRunnable.java b/src/main/java/com/dream/socket/runnable/HandleRunnable.java index 39faa8a..2f03830 100644 --- a/src/main/java/com/dream/socket/runnable/HandleRunnable.java +++ b/src/main/java/com/dream/socket/runnable/HandleRunnable.java @@ -59,5 +59,6 @@ public void status(int status) { public void stop() { running = false; + put((T) new Message(){}); } } diff --git a/src/main/java/com/dream/socket/runnable/SendRunnable.java b/src/main/java/com/dream/socket/runnable/SendRunnable.java index e3c016b..a35cc87 100644 --- a/src/main/java/com/dream/socket/runnable/SendRunnable.java +++ b/src/main/java/com/dream/socket/runnable/SendRunnable.java @@ -47,14 +47,13 @@ public void run() { public void stop() { sending = false; + send((T) new Message(){}); } public boolean send(T data) { try { - if (sending) { - this.queue.put(data); - return true; - } + this.queue.put(data); + return true; } catch (Exception e) { e.printStackTrace(); } diff --git a/src/test/java/com/dream/socket/TCPClient.java b/src/test/java/com/dream/socket/TCPClient.java index 19bc44d..5af0366 100644 --- a/src/test/java/com/dream/socket/TCPClient.java +++ b/src/test/java/com/dream/socket/TCPClient.java @@ -54,5 +54,11 @@ public void encode(StringMessage data, ByteBuffer buffer) { } }); socket.start(); + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + socket.stop(); } } diff --git a/src/test/java/com/dream/socket/TCPServer.java b/src/test/java/com/dream/socket/TCPServer.java index 28a2731..980a288 100644 --- a/src/test/java/com/dream/socket/TCPServer.java +++ b/src/test/java/com/dream/socket/TCPServer.java @@ -7,22 +7,45 @@ public class TCPServer { - public static void main(String[] args) throws Exception { - ServerSocket server = new ServerSocket(6969); - while (true){ - Socket socket = server.accept(); - OutputStream out = socket.getOutputStream(); - DataOutputStream dos = new DataOutputStream(out); - for(int i=1; i<=10; i++){ - byte[] array = ("Message -> " + i).getBytes(); - dos.writeInt(array.length); - dos.flush(); - Thread.sleep(1000); - dos.write(array); - dos.flush(); + public static void main(String[] args) { + ServerSocket server = null; + try { + server = new ServerSocket(6969); + } catch (Exception e) { + e.printStackTrace(); + } + if (server == null) { + return; + } + while (true) { + Socket socket = null; + try { + socket = server.accept(); + } catch (Exception e) { + e.printStackTrace(); + } + if (socket == null) { + continue; } - socket.close(); + try { + OutputStream out = socket.getOutputStream(); + DataOutputStream dos = new DataOutputStream(out); + for (int i = 1; i <= 10; i++) { + byte[] array = ("Message -> " + i).getBytes(); + dos.writeInt(array.length); + dos.flush(); + Thread.sleep(1000); + dos.write(array); + dos.flush(); + Thread.sleep(1000); + } + socket.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } }