// Base read operation byte[] read(SSLSocket sock) { try {
BufferedReader reader = new BufferedReader( new InputStreamReader(sock.getInputStream()));
String s = reader.readLine();
System.err.println("(read) " + name + ": " + s); return s.getBytes();
} catch (Exception e) {
e.printStackTrace();
} returnnull;
}
// Base write operation publicvoid write(SSLSocket sock, byte[] data) { try {
PrintWriter out = new PrintWriter( new OutputStreamWriter(sock.getOutputStream()));
out.println(new String(data));
out.flush();
System.err.println("(write)" + name + ": " + new String(data));
} catch (Exception e) {
e.printStackTrace();
}
}
/** *Serverconstructormustbecalledbeforeanyclientoperationsothe *tlsserverisready.Thereshouldbenotimingproblemsasthe
*/ staticclass Server extends TLSBase {
SSLServerSocketFactory fac;
SSLServerSocket ssock; // Clients sockets are kept in a hash table with the port as the key.
ConcurrentHashMap<Integer, SSLSocket> clientMap = new ConcurrentHashMap<>(); boolean exit = false; Thread t;
// Thread to allow multiple clients to connect
t = newThread(() -> { try { while (true) {
System.err.println("Server ready on port " +
serverPort);
SSLSocket c = (SSLSocket)ssock.accept();
clientMap.put(c.getPort(), c); try {
write(c, read(c));
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception ex) {
System.err.println("Server Down");
ex.printStackTrace();
}
});
t.start();
}
// Exit test to quit the test. This must be called at the end of the // test or the test will never end. void done() { try {
t.interrupt();
ssock.close();
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
// Read from the client byte[] read(Client client) {
SSLSocket s = clientMap.get(Integer.valueOf(client.getPort())); if (s == null) {
System.err.println("No socket found, port " + client.getPort());
} return read(s);
}
// Write to the client void write(Client client, byte[] data) {
write(clientMap.get(client.getPort()), data);
}
// Server writes to the client, then reads from the client. // Return true if the read & write data match, false if not. boolean writeRead(Client client, String s) {
write(client, s.getBytes()); return (Arrays.compare(s.getBytes(), client.read()) == 0);
}
// Get the SSLSession from the server side socket
SSLSession getSession(Client c) {
SSLSocket s = clientMap.get(Integer.valueOf(c.getPort())); return s.getSession();
}
// Close client socket void close(Client c) throws IOException {
SSLSocket s = clientMap.get(Integer.valueOf(c.getPort()));
s.close();
}
}
// Connect to server. Maybe runnable in the future public SSLSocket connect() { try {
sslContext = SSLContext.getDefault();
sock = (SSLSocket)sslContext.getSocketFactory().createSocket();
sock.connect(new InetSocketAddress("localhost", serverPort));
System.err.println("Client connected using port " +
sock.getLocalPort());
name = "client(" + sock.toString() + ")";
write("Hello");
read();
} catch (Exception ex) {
ex.printStackTrace();
} return sock;
}
// Read from the client socket byte[] read() { return read(sock);
}
// Write to the client socket void write(byte[] data) {
write(sock, data);
} void write(String s) {
write(sock, s.getBytes());
}
// Client writes to the server, then reads from the server. // Return true if the read & write data match, false if not. boolean writeRead(Server server, String s) {
write(s.getBytes()); return (Arrays.compare(s.getBytes(), server.read(this)) == 0);
}
// Get port from the socket int getPort() { return sock.getLocalPort();
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.