-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
43 changed files
with
5,079 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.sem.io.bio; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.PrintWriter; | ||
import java.net.Socket; | ||
|
||
/** | ||
* Created by xuyao on 2017/8/7. | ||
*/ | ||
public class TimeClient { | ||
|
||
public static void main(String[] args) { | ||
int port = 8090; | ||
if (args != null && args.length >= 1) { | ||
|
||
try { | ||
port = Integer.valueOf( args[0] ); | ||
} catch (NumberFormatException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
Socket socket = null; | ||
BufferedReader in = null; | ||
PrintWriter out = null; | ||
try { | ||
socket = new Socket("127.0.0.1", port); | ||
in = new BufferedReader(new InputStreamReader(socket.getInputStream())); | ||
out = new PrintWriter(socket.getOutputStream(), true); | ||
out.println("QUERY TIME ORDER"); | ||
System.out.println("send order 2 server succed."); | ||
String resp = in.readLine(); | ||
System.out.println("now is:" + resp); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
}finally { | ||
if (in != null) { | ||
try { | ||
in.close(); | ||
in = null; | ||
} catch (IOException e1) { | ||
e1.printStackTrace(); | ||
} | ||
} | ||
if (out != null) { | ||
out.close(); | ||
out = null; | ||
} | ||
if (socket != null) { | ||
try { | ||
socket.close(); | ||
} catch (IOException e1) { | ||
e1.printStackTrace(); | ||
} | ||
socket = null; | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.sem.io.bio; | ||
|
||
import java.io.IOException; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
|
||
/** | ||
* Created by xuyao on 2017/8/7. | ||
*/ | ||
public class TimeServer { | ||
|
||
public static void main(String[] args) throws IOException { | ||
int port = 8090; | ||
if (args != null && args.length >= 1) { | ||
|
||
try { | ||
port = Integer.valueOf(args[0]); | ||
} catch (NumberFormatException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
ServerSocket server = null; | ||
try { | ||
server = new ServerSocket(port); | ||
System.out.println("start prot:" + port); | ||
Socket socket = null; | ||
while (true) { | ||
socket = server.accept(); | ||
new Thread(new TimerServerHandler(socket)).start(); | ||
} | ||
} finally { | ||
if (server != null) { | ||
System.out.println("server close"); | ||
server.close(); | ||
server = null; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.sem.io.bio; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.PrintWriter; | ||
import java.net.Socket; | ||
import java.util.Date; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* Created by xuyao on 2017/8/7. | ||
*/ | ||
public class TimerServerHandler implements Runnable { | ||
|
||
private Socket socket; | ||
|
||
public TimerServerHandler(Socket socket) { | ||
this.socket = socket; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
BufferedReader in = null; | ||
PrintWriter out = null; | ||
try { | ||
in = new BufferedReader(new InputStreamReader(socket.getInputStream())); | ||
out = new PrintWriter(socket.getOutputStream(), true); | ||
String currentTime = null; | ||
String body = null; | ||
while (true) { | ||
body = in.readLine(); | ||
if (body == null) { | ||
break; | ||
} | ||
System.out.println("server receive order:"+body); | ||
currentTime = ("QUERY TIME ORDER".equalsIgnoreCase(body)) ? new Date(System.currentTimeMillis()).toString() : "BAD ORDER"; | ||
out.println(currentTime); | ||
} | ||
|
||
} catch (Exception e) { | ||
if (in != null) { | ||
try { | ||
in.close(); | ||
in = null; | ||
} catch (IOException e1) { | ||
e1.printStackTrace(); | ||
} | ||
if (out != null) { | ||
out.close(); | ||
out = null; | ||
} | ||
if (socket != null) { | ||
try { | ||
socket.close(); | ||
} catch (IOException e1) { | ||
e1.printStackTrace(); | ||
} | ||
socket = null; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.sem.io.nio; | ||
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.nio.channels.SelectionKey; | ||
import java.nio.channels.Selector; | ||
import java.nio.channels.ServerSocketChannel; | ||
|
||
/** | ||
* Created by xuyao on 2017/8/8. | ||
*/ | ||
public class MultiplexerTimeServer implements Runnable { | ||
|
||
private Selector selector; | ||
private ServerSocketChannel servChannel; | ||
private volatile boolean stop; | ||
|
||
/** | ||
* 初始化多路复用器、绑定监听端口 | ||
* | ||
* @param port | ||
*/ | ||
public MultiplexerTimeServer(int port) { | ||
try { | ||
selector = Selector.open(); | ||
servChannel = ServerSocketChannel.open(); | ||
servChannel.configureBlocking(false); | ||
servChannel.socket().bind(new InetSocketAddress(port), 1024); | ||
servChannel.register(selector, SelectionKey.OP_ACCEPT); | ||
System.out.println("he time server is start in port : " + port); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
System.exit(1); | ||
} | ||
} | ||
|
||
public void stop() { | ||
this.stop = true; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
while (!stop) { | ||
try { | ||
selector.select(1000); | ||
|
||
} catch (Exception e) { | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.sem.io.nio; | ||
|
||
import com.sem.io.bio.TimerServerHandler; | ||
import java.io.IOException; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
|
||
/** | ||
* Created by xuyao on 2017/8/7. | ||
*/ | ||
public class TimeServer { | ||
|
||
public static void main(String[] args) throws IOException { | ||
int port = 8090; | ||
if (args != null && args.length >= 1) { | ||
|
||
try { | ||
port = Integer.valueOf(args[0]); | ||
} catch (NumberFormatException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
// MultiplexerTimeServer | ||
} | ||
} |
Oops, something went wrong.