Applet with SSL socket example
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Peter_Varju
Posted On:   Wednesday, July 7, 2004 07:05 AM

CoClTest applet I have a simple client-server program example. The client sending a number to server, and the server reply the square of the number. If the client is an JAVA application then the SSL communication is successfull. But the client is an applet then I have an error: java.net.SocketException: SSL implementation not available at javax.net.ssl.DefaultSSLSocketFactory.createSocket(DashoA6275) at CoClTestApplet.start(CoClTestApplet.java:41) at sun.applet.AppletPanel.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Here is my codes: file: CoSeTest.java (server) /* The server will be exit when the in   More>>
CoClTest applet

I have a simple client-server program example.

The client sending a number to server, and the server reply the square of the number.


If the client is an JAVA application then the SSL communication is successfull.

But the client is an applet then I have an error:

			java.net.SocketException: SSL implementation not available
			
at javax.net.ssl.DefaultSSLSocketFactory.createSocket(DashoA6275)
at CoClTestApplet.start(CoClTestApplet.java:41)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)



Here is my codes:



file: CoSeTest.java (server)
			/* The server will be exit when the input number is 0. */
			
import java.io.*;
import java.net.*;
import javax.net.*;
import javax.net.ssl.*;

public class CoSeTest {

public static void printUsage() {
System.out.println("Usage: java CoSeTest portnumber");
System.exit(-1);
}

public static void main(String[] args) {
int server_port = 0;
if (args.length != 1) printUsage();
try {
server_port = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
printUsage();
}
ServerSocket ss = null;
Socket s = null;
String inputString = null, outputString = null;
int inputNumber = 0;
boolean doExit = false;

try {
ServerSocketFactory ssocketFactory = SSLServerSocketFactory.getDefault();
ss = ssocketFactory.createServerSocket(server_port);

System.out.println("Server started on port " + server_port + ".");
while (!doExit) {
s = ss.accept();

BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter output = new PrintWriter(s.getOutputStream());

inputString = input.readLine();
System.out.println("Input message: " + inputString);
try {
inputNumber = Integer.parseInt(inputString);
outputString = Integer.toString(inputNumber * inputNumber);
} catch (NumberFormatException e) {
outputString = "It is not number !";
}
output.println(outputString);
output.flush();

if (inputNumber == 0)
doExit = true;

s.close();
}

} catch (IOException e) {
System.err.println(e);
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException ex) {
System.err.println("I can't close the connection !");
}
}
}
}



file: CoClTest.java (client application)
			import java.io.*;
			
import java.net.*;
import javax.net.*;
import javax.net.ssl.*;

public class CoClTest {
public static void printUsage() {
System.out.println("Usage: java CoClTest host portnumber number");
System.exit(-1);
}

public static void main(String[] args) {
int server_port = 0;
Socket s = null;
int outputNumber = 0;
String inputString = null;

if (args.length != 3) printUsage();
try {
server_port = Integer.parseInt(args[1]);
} catch (NumberFormatException e) {
printUsage();
}

try {
outputNumber = Integer.parseInt(args[2]);
} catch (NumberFormatException e) {
printUsage();
}

try {
SocketFactory socketFactory = SSLSocketFactory.getDefault();
s = socketFactory.createSocket(args[0], server_port);

BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter output = new PrintWriter(s.getOutputStream());

output.println(Integer.toString(outputNumber));
output.flush();
inputString = input.readLine();

System.out.println("The square of the number " + outputNumber + " = " + inputString);

} catch (IOException e) {
System.err.println(e);
} finally {
try {
s.close();
} catch (Exception ex) {
System.err.println("I can't close the connection !");
}
}
}
}



file: CoClTestApplet.java (client applet)
			import java.applet.Applet;
			
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;

public class CoClTestApplet extends Applet implements Runnable, ActionListener {
TextArea txtAnswer;
TextField txtMessage;
Socket socket;
Thread clientThread;
String host;
int port;
private PrintStream output;
private BufferedReader input;

public void init() {
txtAnswer = new TextArea("", 20, 80);
txtAnswer.setEditable(false);
add(txtAnswer);

txtMessage = new TextField(80);
txtMessage.addActionListener(this);
add(txtMessage);
}

public void start() {
try {
host = getParameter("host");
port = Integer.parseInt(getParameter("port"));
/* Remarked this section because I set variables in "Java Plug-in settings" on "Control Panel" */
/*
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
System.setProperty("javax.net.ssl.trustStore", "clientTrust");
System.setProperty("javax.net.ssl.trustStorePassword", "123456");
*/
SocketFactory socketFactory = SSLSocketFactory.getDefault();
socket = socketFactory.createSocket(host, port);
output = new PrintStream(socket.getOutputStream(), false);
clientThread = new Thread(this);
clientThread.start();
} catch (UnknownHostException e) {
txtAnswer.append("The host " + host + " is unknown !
");
e.printStackTrace();
} catch (IOException e) {
txtAnswer.append("IO error !
");
e.printStackTrace();
} catch (Exception e) {
txtAnswer.append("Error !
");
e.printStackTrace();
}
}

public void stop() {
try {
clientThread.destroy();
socket.close();
} catch (IOException e) {
txtAnswer.append("I can't close connection !
");
}
}

public void run() {
String inputText;
try {
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

while ((inputText = readData()) != null) {
txtAnswer.append("Incoming message: ");
txtAnswer.append(inputText + "
");
}
} catch (Exception e) {
System.err.print(e);
}
}

public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if (src == txtMessage) {
output.print(txtMessage.getText() + "\0");
output.flush();
txtMessage.setText("");
}
}

/* Read next message from socket */
private String readData() throws IOException {
StringBuffer s = new StringBuffer();
int ch;

while ((ch = input.read()) > 0)
s.append((char)ch);

return ch == 0 ? s.toString() : null;
}

}



file: CoClTest.html (html for applet)
			








Your browser not supported the JAVA applets !






Generate keys:
			keytool -genkey -alias peter -keystore clientKeys -storepass 123456
			
keytool -genkey -alias myserver -keystore serverKeys -storepass 123456
keytool -export -alias myserver -keystore serverKeys -file myserver.cer -storepass 123456
keytool -import -alias myserver -keystore clientTrust -file myserver.cer -storepass 123456



Running the server:
java -Djavax.net.ssl.keyStore=serverKeys -Djavax.net.ssl.keyStorePassword=123456 CoSeTest 4444



Running the client application:
java -Djavax.net.ssl.trustStore=clientTrust -Djavax.net.ssl.trustStorePassword=123456 CoClTest localhost 4444 5



Running the client applet:
Put next line into the Java Runtime Parameters in "JAVA plug-in" of Control panel.
-Djava.security.policy=c:/java/jdk131/jre/lib/security/java.policy -Djava.protocol.handler.pkgs=com.sun.net.ssl.internal.www.protocol -Djavax.net.ssl.trustStore=g:/clientTrust -Djavax.net.ssl.trustStorePassword=123456





and open the CoClTest.html file with your browser.


WHAT IS THE PROBLEM ???



HELP PLZ !!!



Ps: I have JDK 1.3.1_09, IE6.0 with all updates, Win2k with all service packs, and updates
   <<Less
About | Sitemap | Contact