Question

I have written a client/server socket program that uses SSL sockets. the connection is getting established between the server and the client but I'm not able to write data on the stream . Following is the piece of code for server and client

SERVER(This creates SSLServerSocket and creates new thread for each client)

System.setProperty("javax.net.ssl.keyStore", "D:\\client\\server_keystore.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "helloworld");
boolean listening = true;
SSLServerSocketFactory sslserversocketfactory = null;
SSLServerSocket sslserversocket = null;

try {

    sslserversocketfactory =
        (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
    sslserversocket =
        (SSLServerSocket) sslserversocketfactory.createServerSocket(Integer.parseInt(args[0]));

} catch (IOException e) {

    System.out.println("Input/output error occured :" + e.getMessage());
    System.exit(-1);
}

while (listening) {
    try {
        Thread t = new Thread(new DeprovisionServerThread(sslserversocket.accept()));
        if (t != null) {
            System.out.println("New thread created: " + t.getName());
        }

        t.start();
    } catch (IOException ex) {
        Logger.getLogger(DeprovisionServer.class.getName()).log(Level.SEVERE, null, ex);
    }
}
try {
    sslserversocket.close();
} catch (IOException ex) {
    Logger.getLogger(DeprovisionServer.class.getName()).log(Level.SEVERE, null, ex);
}

Server(thread handling code)

 DeprovisionServerThread(Socket socket) {
    //  throw new UnsupportedOperationException("Not yet implemented");

    sslsocket = (SSLSocket) socket;


}

@Override
public void run() {
    //throw new UnsupportedOperationException("Not supported yet.");
    System.out.println("New client socket connection accpeted from: " +  sslsocket.getInetAddress() + " : " + sslsocket.getLocalPort());
    try {
        //PrintWriter out = new PrintWriter(sslsocket.getOutputStream(), true);

        BufferedWriter w = new BufferedWriter(new OutputStreamWriter(sslsocket.getOutputStream()));
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                sslsocket.getInputStream()));


        String inputLine, outputLine;


      //  out.println(outputLine);

        while ((inputLine = in.readLine()) != null) {
        // System.out.println(inputLine);
         if(inputLine!= null)System.out.println("command received" + inputLine);
         w.write("success");

        }
       // out.close();
        in.close();
        sslsocket.close();

    } catch (IOException e) {
        e.printStackTrace();
    }catch(Exception e){
        e.printStackTrace();
    }

client code:

System.setProperty("javax.net.ssl.trustStore", "D:\\server\\server_keystore.jks");
        System.setProperty("javax.net.ssl.trustStorePassword", "helloworld");
        BufferedReader in = new BufferedReader(
                new InputStreamReader(System.in));
        PrintStream out = System.out;
        SSLSocketFactory f =
                (SSLSocketFactory) SSLSocketFactory.getDefault();
        try {
            SSLSocket c =
                    (SSLSocket) f.createSocket("localhost", 4444);
          //  c.s
            c.addHandshakeCompletedListener(new MyListener());

           c.startHandshake();
           Thread.sleep(5000l);
           printSocketInfo(c);
            BufferedWriter w = new BufferedWriter(
                    new OutputStreamWriter(c.getOutputStream()));
            BufferedReader r = new BufferedReader(
                    new InputStreamReader(c.getInputStream()));

            w.write("deprovision command",0, 10);
            System.out.println("send..");
            w.flush();
            String m ;
            while ((m = r.readLine()) != null) {
            System.out.println("status received: " + m);


            }
            System.out.println("after while");
            w.close();
            r.close();
            c.close();
        } catch (IOException e) {
            System.err.println(e.toString());
        }
    }

    private static void printSocketInfo(SSLSocket s) {
        System.out.println("Socket class: " + s.getClass());
        System.out.println("   Remote address = "
                + s.getInetAddress().toString());
        System.out.println("   Remote port = " + s.getPort());
        System.out.println("   Local socket address = "
                + s.getLocalSocketAddress().toString());
        System.out.println("   Local address = "
                + s.getLocalAddress().toString());
        System.out.println("   Local port = " + s.getLocalPort());
        System.out.println("   Need client authentication = "
                + s.getNeedClientAuth());
        SSLSession ss = s.getSession();
        System.out.println("   Cipher suite = " + ss.getCipherSuite());
        System.out.println("   Protocol = " + ss.getProtocol());

    }
}

im testing this code on local system. i have also used listener to check whether there is no problem in handshaking. when a new client comes , the server accepts connection from the client and also print all the info that im printing in printSocketInfo() method. handshaking listener is also getting notified.but when i write the data from the client socket to server nothing happesn, no exception nothing. please help . thanks in advance

Was it helpful?

Solution

You write only 10 first characters from "deprovision command" string, and server expects string that ends with new line character. Change your client code to following:

w.write("deprovision command",0, 10);
w.newLine();
System.out.println("send..");
w.flush();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top