1

Below is some code I found about the 'net and I can't get it working. As far as I know it should work. Sadly, when I used s_server in the OpenSSL toolkit it didn't register a connection. The server is written in Cpp and is working perfectly (at least I have that much). Can anyone make the proper corrections for me it would be greatly appreciated.

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.*;

import java.applet.*;
import java.awt.*;


public class jclientssl extends Applet {

    public static void main() {

        try {
            SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("localhost", 9999);

            InputStream inputstream = System.in;
            InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
            BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

            OutputStream outputstream = sslsocket.getOutputStream();
            OutputStreamWriter outputstreamwriter = new OutputStreamWriter(outputstream);
            BufferedWriter bufferedwriter = new BufferedWriter(outputstreamwriter);

            String string = null;
            while ((string = bufferedreader.readLine()) != null) {
                bufferedwriter.write(string + '\n');
                bufferedwriter.flush();
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    public void paint(Graphics g) {

        g.drawString("Welcome to Java!!", 50, 60 );

    }
}
Confident
  • 321
  • 4
  • 17
  • 1
    This is quite obviously server code (`SSLServerSocket`). Not sure what you're trying to do with `openssl s_server` (also a server) with it. – Bruno Sep 10 '12 at 14:30
  • sorry, I fixed the code but I get the same problem – Confident Sep 10 '12 at 14:35
  • what isn't working? are you getting an exception? – jtahlborn Sep 10 '12 at 14:41
  • Nope, no exception. It seems to be running fine but the SSL connection was never made – Confident Sep 10 '12 at 14:42
  • 1
    i seriously doubt it is "running fine" but not getting an exception _and_ not making a connection. your code swallows the exception, which might be why you don't see it (unless you are looking at the java console in your browser). – jtahlborn Sep 10 '12 at 14:45
  • 1
    also, your class extends applet, but you have the socket connection in the main method, which is irrelevant to an Applet. are you running this as an Applet or a standalone program? – jtahlborn Sep 10 '12 at 14:49
  • I don't see how you can have any evidence that no connection was made, as by default this code produces no local output. The only evidence would be at the server. More likely it just deadlocks with the server, both doing reads. At the moment this is not a real question. – user207421 Sep 11 '12 at 03:57

1 Answers1

2

If you are running this code as a Applet, then the main method is irrelevant, so your socket code is not being executed. (and, if you want to use that main method in a standalone application, then it needs to be public static void main(String[])).

Community
  • 1
  • 1
jtahlborn
  • 52,909
  • 5
  • 76
  • 118