0

I'm new to java and I was trying to build an echo server and a client using SSL SOCKETS. Then I compile the server code first. but the command prompt gave me the following error.package com.sun.net.ssl.internal.ssl is not visible. And there's another message within brackets..package com.sun.net.ssl.internal.ssl is declared in module java.base,which does not export it Can somebody please help me with this?? Or provide me an alternative code. But please don't make it complex..I'm just a beginner to this.

server code

import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import java.security.Security;
import com.sun.net.ssl.internal.ssl.Provider;

public class Server 
{
    public static void main(String args[])
    {
        //The Port number through which this server will accept client connections
        int port = 9000;
        /*Adding the JSSE (Java Secure Socket Extension) provider which provides SSL and TLS protocols
        and includes functionality for data encryption, server authentication, message integrity, 
        and optional client authentication.*/
        Security.addProvider(new Provider());
        //specifing the keystore file which contains the certificate/public key and the private key
        System.setProperty("javax.net.ssl.keyStore","myKeyStore.jks");
        //specifing the password of the keystore file
        System.setProperty("javax.net.ssl.keyStorePassword","123456");
        //This optional and it is just to show the dump of the details of the handshake process 
        System.setProperty("javax.net.debug","all");
        try
        {
            //SSLServerSocketFactory establishes the ssl context and and creates SSLServerSocket 
            SSLServerSocketFactory sslServerSocketfactory = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
            //Create SSLServerSocket using SSLServerSocketFactory established ssl context
            SSLServerSocket sslServerSocket = (SSLServerSocket)sslServerSocketfactory.createServerSocket(port);
            System.out.println("Echo Server Started & Ready to accept Client Connection");
            //Wait for the SSL client to connect to this server
            SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept();
            //Create InputStream to recive messages send by the client
            DataInputStream inputStream = new DataInputStream(sslSocket.getInputStream());
            //Create OutputStream to send message to client
            DataOutputStream outputStream = new DataOutputStream(sslSocket.getOutputStream());
            outputStream.writeUTF("Hello Client, Say Something!");
            //Keep sending the client the message you recive unless he sends the word "close"
            while(true)
            {
                String recivedMessage = inputStream.readUTF();
                System.out.println("Client Said : " + recivedMessage);
                if(recivedMessage.equals("close"))
                {
                    outputStream.writeUTF("Bye");
                    outputStream.close();
                    inputStream.close();
                    sslSocket.close();
                    sslServerSocket.close();
                    break;
                }
                else
                {
                    outputStream.writeUTF("You Said : "+recivedMessage);
                }
            }
        }
        catch(Exception ex)
        {
            System.err.println("Error Happened : "+ex.toString());
        }
    }
}

Client code

import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.security.Security;
import com.sun.net.ssl.internal.ssl.Provider;

public class Client
{
    public static void main(String args[])
    {
        //The Port number through which the server will accept this clients connection
        int serverPort = 9000;
        //The Server Address
        String serverName = "192.168.5.1";
        /*Adding the JSSE (Java Secure Socket Extension) provider which provides SSL and TLS protocols
        and includes functionality for data encryption, server authentication, message integrity, 
        and optional client authentication.*/
        Security.addProvider(new Provider());
        //specifing the trustStore file which contains the certificate & public of the server
        System.setProperty("javax.net.ssl.trustStore","myTrustStore.jts");
        //specifing the password of the trustStore file
        System.setProperty("javax.net.ssl.trustStorePassword","123456");
        //This optional and it is just to show the dump of the details of the handshake process 
        System.setProperty("javax.net.debug","all");
        try
        {
            //SSLSSocketFactory establishes the ssl context and and creates SSLSocket 
            SSLSocketFactory sslsocketfactory = (SSLSocketFactory)SSLSocketFactory.getDefault();
            //Create SSLSocket using SSLServerFactory already established ssl context and connect to server
            SSLSocket sslSocket = (SSLSocket)sslsocketfactory.createSocket(serverName,serverPort);
            //Create OutputStream to send message to server
            DataOutputStream outputStream = new DataOutputStream(sslSocket.getOutputStream());
            //Create InputStream to read messages send by the server
            DataInputStream inputStream = new DataInputStream(sslSocket.getInputStream());
            //read the first message send by the server after being connected
            System.out.println(inputStream.readUTF());
            //Keep sending sending the server the message entered by the client unless the it is "close"
            while (true)
            {
                System.out.println("Write a Message : ");
                String messageToSend = System.console().readLine();
                outputStream.writeUTF(messageToSend);
                System.err.println(inputStream.readUTF());
                if(messageToSend.equals("close"))
                {
                    break;
                }
            }
        }
        catch(Exception ex)
        {
            System.err.println("Error Happened : "+ex.toString());
        }
    }
}
manula
  • 23
  • 1
  • 4
  • Are you using Java 9+? You should use the `--add-exports` option – user May 13 '20 at 14:38
  • 3
    This sample code looks very old to me. SSL/TLS functionality is always available in java without adding any provider ([this question](https://stackoverflow.com/q/13160829/150978) indicates that it was already legacy 8 years ago...). Hence you should just remove the line with `addProvider` and the correspondent import. – Robert May 13 '20 at 14:38
  • I removed those parts. then i could compile the program. But when I execute it, command prompt keeps generating logs without stopping.. – manula May 13 '20 at 14:50
  • 1
    Remove this statement: `Security.addProvider(new Provider());` – Andreas May 13 '20 at 14:55
  • I did that. I could compile the program. but I can't execute it.command prompt is showing logs without stopping when I execute the program. – manula May 13 '20 at 15:03

0 Answers0