Created
August 19, 2018 21:34
-
-
Save edwingsm/2595979c7a9947a39d8c491faa5c562d to your computer and use it in GitHub Desktop.
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
import java.io.IOException; | |
import java.net.InetAddress; | |
import java.net.InetSocketAddress; | |
import java.nio.ByteBuffer; | |
import java.nio.channels.SocketChannel; | |
public class MySelectorClientExample { | |
private static final int BUFFER_SIZE = 1024; | |
private static String[] messages = { "The best way to predict the future is to create it.", | |
"As you think, so shall you become.", "The noblest pleasure is the joy of understanding.", | |
"Courage is grace under pressure.", "*exit*" }; | |
public static void main(String[] args) { | |
logger("Starting MySelectorClientExample..."); | |
try { | |
int port = 9990; | |
InetAddress hostIP = InetAddress.getLocalHost(); | |
InetSocketAddress myAddress = new InetSocketAddress(hostIP, port); | |
SocketChannel myClient = SocketChannel.open(myAddress); | |
logger(String.format("Trying to connect to %s:%d...", myAddress.getHostName(), myAddress.getPort())); | |
for (String msg : messages) { | |
ByteBuffer myBuffer = ByteBuffer.allocate(BUFFER_SIZE); | |
ByteBuffer myBuffer2 = ByteBuffer.allocate(BUFFER_SIZE); | |
myBuffer.put(msg.getBytes()); | |
myBuffer.flip(); | |
int bytesWritten = myClient.write(myBuffer); | |
myClient.read(myBuffer2); | |
String data = new String(myBuffer2.array()).trim(); | |
myBuffer2.clear(); | |
logger(String.format("Sending Message...: %s\nbytesWritten...: %d", msg, bytesWritten)); | |
logger(String.format("Recieved Message...: %s\n", data)); | |
} | |
logger("Closing Client connection..."); | |
myClient.close(); | |
} catch (IOException e) { | |
logger(e.getMessage()); | |
e.printStackTrace(); | |
} | |
} | |
public static void logger(String msg) { | |
System.out.println(msg); | |
} | |
} |
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
import java.io.IOException; | |
import java.net.InetAddress; | |
import java.net.InetSocketAddress; | |
import java.net.ServerSocket; | |
import java.nio.ByteBuffer; | |
import java.nio.channels.SelectionKey; | |
import java.nio.channels.Selector; | |
import java.nio.channels.ServerSocketChannel; | |
import java.nio.channels.SocketChannel; | |
import java.util.Iterator; | |
import java.util.Set; | |
public class MySelectorServerExample { | |
private static final int BUFFER_SIZE = 1024; | |
private static Selector selector = null; | |
public static void main(String[] args) { | |
logger("Starting MySelectorExample..."); | |
try { | |
InetAddress hostIP = InetAddress.getLocalHost(); | |
int port = 9990; | |
logger(String.format("Trying to accept connections on %s:%d...", hostIP.getHostAddress(), port)); | |
selector = Selector.open(); | |
ServerSocketChannel mySocket = ServerSocketChannel.open(); | |
ServerSocket serverSocket = mySocket.socket(); | |
InetSocketAddress address = new InetSocketAddress(hostIP, port); | |
serverSocket.bind(address); | |
mySocket.configureBlocking(false); | |
int ops = mySocket.validOps(); | |
mySocket.register(selector, ops, null); | |
while (true) { | |
selector.select(); | |
Set<SelectionKey> selectedKeys = selector.selectedKeys(); | |
Iterator<SelectionKey> i = selectedKeys.iterator(); | |
while (i.hasNext()) { | |
SelectionKey key = i.next(); | |
if (key.isAcceptable()) { | |
processAcceptEvent(mySocket, key); | |
} else if (key.isReadable()) { | |
processReadEvent(key); | |
} | |
i.remove(); | |
} | |
} | |
} catch (IOException e) { | |
logger(e.getMessage()); | |
e.printStackTrace(); | |
} | |
} | |
private static void processAcceptEvent(ServerSocketChannel mySocket, SelectionKey key) throws IOException { | |
logger("Connection Accepted..."); | |
// Accept the connection and make it non-blocking | |
SocketChannel myClient = mySocket.accept(); | |
myClient.configureBlocking(false); | |
// Register interest in reading this channel | |
myClient.register(selector, SelectionKey.OP_READ); | |
} | |
private static void processReadEvent(SelectionKey key) throws IOException { | |
logger("Inside processReadEvent..."); | |
// create a ServerSocketChannel to read the request | |
SocketChannel myClient = (SocketChannel) key.channel(); | |
// Set up out 1k buffer to read data into | |
ByteBuffer myBuffer = ByteBuffer.allocate(BUFFER_SIZE); | |
myClient.read(myBuffer); | |
String data = new String(myBuffer.array()).trim(); | |
myBuffer.flip(); | |
if (data.length() > 0) { | |
logger(String.format("Message Received.....: %s\n", data)); | |
myClient.write(myBuffer); | |
if (data.equalsIgnoreCase("*exit*")) { | |
myClient.close(); | |
logger("Closing Server Connection..."); | |
} | |
} | |
} | |
public static void logger(String msg) { | |
System.out.println(msg); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment