Need to connect a Java client to a Cpp server using TCP and OpenSSL
I have looked everywhere and noone seems to ba able to explain how to connect a Java client to a Cpp server using TCP and OpenSSL. The connection I need to make is over TCP. This Java program will also be an applet. I am concerned about security issues so I need to use OpenSSL. I would appreciate a responce. If it could please be as simple as possible and include all the Java packages and Cpp libraries listed. I will show you what I have so far. All my source should be able to compile right away. I'm listing both Cpp and java source.
/*******************
* Java Client *
*******************/
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import javax.net.ssl.*;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public class gaia_applet extends Applet {
int width, height;
public static void init(String[] args) throws IOException {
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("localhost", 6001);
}
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.drawString("My Applet",30,180);
}
}
/*****************
* CPP Server *
*****************/
#include <iostream>
#include <stdio.h>
#pragma comment (lib, "ssleay32.lib")
#pragma comment (lib, "libeay32.lib")
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/ssl.h>
#include <openssl/x509v3.h>
#include <windows.h>
#define PORT "6001"
void init_OpenSSL(void);
using na开发者_开发知识库mespace std;
int main(int argc, char *argv[])
{
BIO *acc, *client;
// init_OpenSSL();
acc = BIO_new_accept(PORT);
if (!acc)
cout << "Error creating server socket";
if (BIO_do_accept(acc) <= 0)
cout << "Error binding server socket";
while(1) {
cout << "Socket OK!";
if (BIO_do_accept(acc) <= 0)
cout << "Error accepting connection";
client = BIO_pop(acc);
cout << "New connection!";
}
BIO_free(acc);
return 0;
}
Java applets are only capable of opening sockets to the server from which they were originally requested. All other socket requests will fail unless you sign your applet. It is unclear if this is your problem or not since we don't know where your applet resides.
精彩评论