Java SSL Socket Programming -


i read sslsocket when had finished chat program java use normal serversocket. trying replace normal serversocket sslsocket, there not on internet found something. whserver class this: class 1 start socket in selected port, if need see other classes edit question:

import java.io.ioexception; import java.net.*; import javax.net.serversocketfactory; import javax.net.ssl.sslcontext; import javax.net.ssl.sslsocket; import javax.net.ssl.sslsocketfactory;  public class whserver extends thread {    private int port;   private serversocket server;   private channelsmanager manager;   sslcontext context;   sslsocketfactory sslsf;    public whserver(int port, channelsmanager manager) throws ioexception {     this.port = port;     this.manager = manager;        }    public void serverstop() throws ioexception{       server.close();         }    public whserver(int port) throws ioexception {     this(port, new channelsmanager());   }    public int getport() {     return port;   }    public void run() {     try {       while(true) {              serversocketfactory ssf = serversocketfactory.getdefault();         server = ssf.createserversocket(port);               socket socket = server.accept();                 sslsf = context.getsocketfactory();         sslsocket sslsocket = (sslsocket) sslsf.createsocket(socket, null,socket.getport(), false);         sslsocket.setuseclientmode(false);               manager.initialite(socket);       }     } catch(exception ex) {         ex.printstacktrace();     }   }  } 

firstly, sslcontext context instance variable never initialised, null. nothing specific ssl or sockets here, it's basic java error: if try call on this, throw npe.

secondly, if it's not null (for example, can create new instance context = sslcontext.getinstance("tls"), see sslcontext section of java cryptography architecture standard algorithm name documentation indicated in sslcontext api doc), still need initialise sslcontext via init method.

since you're trying implement server, you'll need provide non-null keymanager, otherwise you'll sslhandshakeexception saying "no cipher suites in common". can find details in this answer example.

in addition, don't need use plain sockets , upgrade them sslsockets after accepting do. it's not wrong, following might easier:

 // assuming you've initialised sslcontext   sslserversocketfactory sslsf = context.getserversocketfactory();   sslserversocket server = (sslserversocket) sslsf.createserversocket(port);   sslserversocketfactory ssf = serversocketfactory.getdefault();   server = ssf.createserversocket(port);         sslsocket socket = (sslsocket)server.accept(); 

your socket coming sslserversocketfactory in server mode. of course, there's no need factories within while loop.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -