I have a client/server program and applet. I will show the code below. Can you please tell me why and where the programs are blocking if at all. The applet seems to stick but when I close the Cpp side it finishes executing. Heres the code (I left out the imports and includes).
public class first extends JApplet {
PrintWriter toServer = null;
BufferedReader fromServer = null;
public void init() {
System.setProperty("javax.net.ssl.keyStore", "javakeys");
System.setProperty("javax.net.ssl.keyStorePassword", "javakeys");
try {
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("localhost", 4000);
toServer = new PrintWriter(sslsocket.getOutputStream(), true);
InputStreamReader isr = new InputStreamReader(sslsocket.getInputStream());
fromServer = new BufferedReader(isr);
} catch (Exception exception) {
exception.printStackTrace();
}
toServer.println("Flystar".getBytes());
}
public void paint(Graphics g) {
g.setColor( Color.red );
g.drawString("Welcome to Java!!", 50, 60 );
}
}
And here’s the Cpp…
int conn_new_server( int );
__attribute__((constructor)) void construct_ssl()
{
SSL_load_error_strings();
SSL_library_init();
OpenSSL_add_all_algorithms();
}
__attribute__((destructor)) void destruct_ssl()
{
ERR_free_strings();
EVP_cleanup();
}
int main()
{
int sockfd, client;
SSL_CTX *tlsctx;
SSL *ssl;
char recvit[256];
printf("Status %d\n", RAND_status());
tlsctx = SSL_CTX_new( TLSv1_server_method());
SSL_CTX_set_options(tlsctx, SSL_OP_SINGLE_DH_USE);
SSL_CTX_use_certificate_file(tlsctx, "server.crt" , SSL_FILETYPE_PEM);
SSL_CTX_use_PrivateKey_file(tlsctx, "server.key", SSL_FILETYPE_PEM);
sockfd = conn_new_server( 1337 );
while (1)
{
client = accept(sockfd, NULL, NULL);
ssl = SSL_new(tlsctx);
SSL_set_fd(ssl, client);
SSL_accept(ssl);
cout << "connection detected!\n\r";
while(1) {
// SSL_write( ssl, "hello", sizeof("hello"));
SSL_read( ssl, recvit, sizeof(recvit));
cout << recvit << "\n\r";
}
// SSL_write(ssl, "Hi :3\n\r", 6);
SSL_shutdown(ssl);
SSL_free(ssl);
// close(client);
}
SSL_CTX_free(tlsctx);
// close(sockfd);
return 0;
}
int conn_new_server( int port)
{
WSADATA g_wsadata; // Winsock data holder
int err;
StartSocketLib;
char *messageman = "Hello.\r\n";
int recv_stat;
int connsock = 0;
char *testsendbuf = "This is very good!\r\n";
list<int> socklist;
// BEGIN CODE BLOCK 2.3 - Create a Listening Socket on port 4000
int sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if( sock == -1 ) {
cout << "Socket creation error!" << endl;
return 0;
}
cout << "Socket created! Standing By." << endl;
// create a sockaddr_in for binding, listening on port 4000
struct sockaddr_in socketaddress;
socklen_t sa_size = sizeof( struct sockaddr_in );
socketaddress.sin_family = AF_INET;
socketaddress.sin_port = htons( 4000 );
socketaddress.sin_addr.s_addr = htonl( INADDR_ANY );
memset( &(socketaddress.sin_zero), 0, 8 );
// bind the socket
err = bind( sock, (struct sockaddr*)&socketaddress, sa_size );
// listen on the socket
err = listen( sock, 16 );
The number one issue here is using
BufferedReader. It will try to fill up an internal buffer before returning any data to you, and if there’s not enough data to fill the buffer, it will block. If you’re usingBufferedReaderjust to get thereadLine()method — which is what you’re doing here — then when you construct it, pass a second constructor argument of1, to set a buffer size of one character, essentially turning off buffering. That will most likely fix your problem.