Skip to content

Commit

Permalink
update stop
Browse files Browse the repository at this point in the history
  • Loading branch information
Sopage committed Nov 24, 2017
1 parent de011ea commit 17ee4d9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@ public void status(int status) {

public void stop() {
running = false;
put((T) new Message(){});
}
}
7 changes: 3 additions & 4 deletions src/main/java/com/dream/socket/runnable/SendRunnable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/com/dream/socket/TCPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,11 @@ public void encode(StringMessage data, ByteBuffer buffer) {
}
});
socket.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
socket.stop();
}
}
51 changes: 37 additions & 14 deletions src/test/java/com/dream/socket/TCPServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}

}

}

0 comments on commit 17ee4d9

Please sign in to comment.