⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.java

📁 IVJ MUD (Masive Multiplayer Text Game) is a SSL-based soft. IVJ-MUD(Masive多用户文本游戏)是一个基于SSL的应用程序。
💻 JAVA
字号:
package ivj.MUD.client;import java.io.*;import java.net.*;import javax.net.ssl.*;import java.security.KeyStore;import javax.net.SocketFactory;import ivj.logging.*;/** This is a simple client.  It simply connects to a given server, sends some text, * reads some text, and exits.  Nothing special. * @author Ivan Jouikov (ivj@comcast.net) * @version 0.0.1 (Alpha) */public class Test implements Runnable{    /** Port to connect to. */    public static final int PORT = 5555;        /** Host to connect to. */    public static final String HOST = "localhost";        /** Keystore file. */    public static final String KEYSTORE_FILE = "clientKeys";        /** Password to the keystore file. */    public static final String PASSWORD = "client";        /** Algorithm to use for encryption. */    public static final String ALGORITHM = "sunx509";        /** Logging */    protected LogManager log = null;    public static void main(String[] args)    {        new Thread(new Test()).start();    }        public Test()    {        log = new ThreadedLogManager();        log.addLogger( new ConsoleLogger() );    }    public void run()    {        Socket socket = null;        try        {            log.info("Beginning initialization...");            KeyManagerFactory kmf;            KeyStore ks;            TrustManagerFactory tmf;            SSLContext sslc;            kmf = KeyManagerFactory.getInstance(ALGORITHM);            log.info("Key Manager Factory created using algorithm '" + ALGORITHM + "'...");                        ks = KeyStore.getInstance( "JKS" );            log.info("Keystore initialized as JKS...");                        ks.load(new FileInputStream(KEYSTORE_FILE), PASSWORD.toCharArray());            log.info("Keystore loaded from '" + KEYSTORE_FILE + "'...");                        kmf.init(ks, PASSWORD.toCharArray());            log.info("Key Manager Factory initialized using Keystore...");                        tmf = TrustManagerFactory.getInstance(ALGORITHM);            log.info("Trust Manager Factory created using algorithm '" + ALGORITHM + "'...");                        tmf.init(ks);            log.info("Trust Manager Factory initialized using Keystore...");                        sslc = SSLContext.getInstance("TLS");            log.info("SSLContext created using TLS protocol...");                        sslc.init(kmf.getKeyManagers(), new TrustManager[]{new ClientTrustManager()}, null);            log.info("SSLContext initialized using Key Managers, CUSTOM trust manager (trusts everything), and default SecureRandom object...");            // The process is different from here on the client. Instead of            // getting a ServerSocketFactory, we ask for a SocketFactory from            // the SSL context.            SocketFactory sf = sslc.getSocketFactory();            log.info("Socket factory created (hopefully SSLSocketFactory)...");                        // Check to see if the factory is in fact SSL            if ( ! (sf instanceof SSLSocketFactory) )                throw new Exception( "Socket Factory is not SSLSocketFactory!" );            // Then we get the socket from the factory and treat it            // as if it were a standard (plain) socket.            socket = sf.createSocket(HOST, PORT);            log.info("Socket created on host '" + HOST + "' and port '" + PORT + "'...");            doQuery(socket);        }        catch(Exception e)        {            log.fatal(e);        }        finally        {            if(socket!=null)            {                try{ socket.close(); }                catch(IOException e) {}            }            System.exit(0);        }    }    private void doQuery(Socket s) throws Exception    {        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));        // Say something to the server...        out.write("Hello from the client!\n");        out.flush();        log.info("Client (I) say: " + "Hello from the client!");                // Read a line of text from the server...        String text = in.readLine();        log.info("Server says: " + text);                log.info("Closing the connection...");        s.close();    }}class ClientTrustManager implements X509TrustManager{        public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificate, String str) throws java.security.cert.CertificateException    {        // do nothing == trust by default    }        public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificate, String str) throws java.security.cert.CertificateException    {        // do nothing == trust by default    }        public java.security.cert.X509Certificate[] getAcceptedIssuers()    {        return null;    }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -