Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6112787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:47:05+00:00 2026-05-23T14:47:05+00:00

There is a plain socket server listening on port 12345 ; ServerSocket s =

  • 0

There is a plain socket server listening on port 12345;

ServerSocket s = new ServerSocket(12345);

What I want to know is that it is possible that:

  1. If the client send a http request, the server handle the request directly,
  2. If the client send a https request, the server change client socket to SSLSocket?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:47:06+00:00Added an answer on May 23, 2026 at 2:47 pm

    Is it possible to change plain socket
    to SSLSocket?

    Yes, it is. On the server side, the following works:

    ServerSocketFactory ssf = ServerSocketFactory.getDefault();
    ServerSocket serverSocket = ssf.createServerSocket(12345);
    
    // I've initialised an sslContext with a keystore, as you normally would.
    Socket socket = serverSocket.accept();
    SSLSocketFactory sslSf = sslContext.getSocketFactory();
    // The host name doesn't really matter, since we're turning it into a server socket
    // (No need to match the host name to the certificate on this side).
    SSLSocket sslSocket = (SSLSocket) sslSf.createSocket(socket, null,
        socket.getPort(), false);
    sslSocket.setUseClientMode(false);
    
    // Use the sslSocket InputStream/OutputStream as usual.
    

    SSLSocketFactory.createSocket(Socket, ...) will by default convert the existing Socket into a client-mode SSLSocket. Since the handshake only starts when you start reading/writing with the I/O streams, it’s still time to change the mode using setUseClientMode(false).

    Regarding the rest of the question:

    What I want to know is that it is
    possible that:

    • If the client send a http request, the server handle the request
      directly,
    • If the client send a https request, the server change client
      socket to SSLSocket?

    Again, yes, it’s possible. It’s sometimes referred to as “port unification” and it’s implemented in Grizzly and thus Glassfish.

    It works because both HTTP and TLS (upon which HTTPS works) are protocols where the client is expected to talk first. Therefore, the server can detect whether what the client initially sends is a TLS ClientHello message (in which case it should try to proceed with the TLS handshake) or a plain HTTP request (e.g. GET / HTTP/1.1…).

    I suspect port unification is “easier” to do using SSLEngine, otherwise, it might be hard to implement a read-ahead on a plain socket, which you would still be able to convert via SSLSocketFactory.createSocket(Socket, ...).

    Note that this is still rather unusual, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any good alternative for the plain if statements in PHP? I know
I know that there are a lot of discussions already on SO about SOAP,
I have a problem where I have an existing client server (socket based) application
I need to write a client-server application. I want to write it in python,
Is the new C++11 going to contain any socket library? So that one could
Is there a way to run plain c code on top of the JVM?
Is there a way we can set an Image instead of plain colors as
Is there any good way of truncating text with plain HTML and CSS, so
Is there a way to extract all email addresses from a plain text using
Is there anything out there to convert html to plain text (maybe a nokogiri

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.