Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

URGENT: IOException in main: java.net.SocketException: Connection reset

843811Jul 19 2008 — edited Jul 20 2008
Hi all,

I'm developing a socket server with SSL and to do that i'm using JSSE.
I bought two books to know more about JSSE.
Right now, I have two classes, the SSLServer and the SSLClient (shown below).I also, have a key (created with keytool) and this key is in the server�s project and also in the client�s project.

I run the server and it works fine but when I run the client I receive this error:
IOException in main: java.net.SocketException: Connection reset

My classes are exactly like in the books and I don�t understand what is wrong!

Can someone help me?

Thanks in advance.

SSLServer:

import java.net.*;
import javax.net.*;
import javax.net.ssl.*;
import java.security.*;
import java.security.cert.*;
import java.io.*;
import java.security.KeyStore;
import javax.security.cert.X509Certificate;


public class SSLServer {

private SSLServerSocketFactory ssf;
public SSLServer() {

try {
//
// our keystore password as a byte array
//
char[] passphrase = "passphrase".toCharArray();
//
// get an instance of an SSLContext
//
SSLContext context = SSLContext.getInstance("TLS");

//
// get an instance of our X509 key manager
//
KeyManagerFactory keyManagerFactory =
KeyManagerFactory.getInstance("SunX509");

//
// get an instance of our keystore
//
KeyStore keyStore = KeyStore.getInstance("JKS");

//
// load our keystore and initialize
//
keyStore.load(
new FileInputStream("testkeys"), passphrase);
keyManagerFactory.init(keyStore, passphrase);

//
// initialize our SSL context using our key managers
//
context.init(keyManagerFactory.getKeyManagers(),
null,
null);

//
// create a server socket factory and store reference
// in our instance member
//
ssf = context.getServerSocketFactory();
}
catch ( IOException e) {
System.err.println(
"IOException in main: " + e );
}
catch ( KeyStoreException e) {
System.err.println(
"KeyStoreException in main: " + e );
}
catch ( KeyManagementException e) {
System.err.println(
"KeyManagementException in main: " + e );
}
catch ( NoSuchAlgorithmException e) {
System.err.println(
"NoSuchAlgorithmException in main: " + e );
}
catch ( CertificateException e) {
System.err.println(
"CertificateException in main: " + e );
}
catch ( UnrecoverableKeyException e) {
System.err.println(
"UnrecoverableKeyException in main: " + e );
}
}


public static void main( String[] args ) {


SSLServer sslServer = new SSLServer();

try {
//
// create a server socket on port 1500
//
ServerSocket ss = sslServer.ssf.createServerSocket(
1500 );
((SSLServerSocket)ss).setNeedClientAuth(true);


System.out.println(
"Secure socket created. Listening ... " );

//<
// listen for connections
//
boolean loop = true;
//while ( loop ) {
Socket socket = ss.accept();
System.out.println(
"Connection accepted ... " );
//
// start a thread to handle this
//
/*Thread t = new Thread(
new OrderRequestProcessor( socket ) );
t.start();*/

//}

}
catch ( IOException e) {
System.err.println("IOException in main: " + e );
}

}
}



SSLClient:

import java.io.*;

import javax.net.ssl.*;

import java.security.KeyStore;

public class SSLClient {

private SSLSocketFactory factory;

public SSLClient() {

try {
//
// our keystore password as a byte array
//
char[] passphrase = "passphrase".toCharArray();

//
// get an instance of an SSLContext
//
SSLContext context = SSLContext.getInstance("TLS");

//
// get an instance of our X509 key manager
//
KeyManagerFactory keyManagerFactory =
KeyManagerFactory.getInstance("SunX509");

//
// get an instance of our keystore
//
KeyStore keyStore = KeyStore.getInstance("JKS");

//
// load our keystore and initialize
//
keyStore.load(
new FileInputStream("testkeys"), passphrase);
keyManagerFactory.init(keyStore, passphrase);

//
// initialize our SSL context using our key managers
//
context.init(keyManagerFactory.getKeyManagers(),
null,
null);

factory = context.getSocketFactory();



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

public static void main( String[] args ) {

try {
SSLClient client = new SSLClient();

SSLSocket socket = (SSLSocket)
client.factory.createSocket("127.0.0.1", 1500);

System.out.println( "Starting handshake ... " );
socket.startHandshake();

//
// use our processing method inherited
// from examples.net.Client
//

//client.doProcessing( socket );
}
catch (IOException e) {
System.err.println("IOException in main: " + e );
}

}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 17 2008
Added on Jul 19 2008
1 comment
263 views