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

  • Home
  • SEARCH
  • 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 8061375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:19:10+00:00 2026-06-05T10:19:10+00:00

In Java, how I can get an open socket? I have 2 JFrames; in

  • 0

In Java, how I can get an open socket? I have 2 JFrames; in the first JFrame I open the connection of my Client socket. Inside this same JFrame I create an instance of another JFrame (JFrame2). Now I want to get the same Socket from JFrame1 into JFrame2 to continue talking with my server Socket:

login.java(First JFrame)

try {
            cliente = new Socket("localhost", 4444);
            salida = new ObjectOutputStream(cliente.getOutputStream());
            entrada = new ObjectInputStream(cliente.getInputStream());
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: localhost.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: localhost.");
            System.exit(1);
        }

login.java(First Jframe)

try {           

            while ((mensaje_entrada=(String)entrada.readObject()) != null) {
                try {
                    me=td.encrypt(mensaje_entrada);
                    m2=td.decrypt(me);
                } catch (Exception ex) {
                    Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
                }
                System.out.println("e:"+ me);
                System.out.println("de:"+ m2);

                System.out.println(mensaje_entrada);
                if(mensaje_entrada.equals("20")){
                    mensaje_salida=""+txt_usuario.getText()+","+txt_password.getText();
                    System.out.println(mensaje_salida);
                    salida.writeObject( mensaje_salida );
                    salida.flush();
                    mensaje_entrada=(String)entrada.readObject();
                    System.out.println(mensaje_entrada);
                    if(mensaje_entrada.equals("1")){
                        m.setLocationRelativeTo(null); <---- **m is another JFrame(Jframe2)**
                        m.setVisible(true);
                        //JOptionPane.showMessageDialog(this,"Funciona!!");
                        break;
                    }else if(mensaje_entrada.equals("2")){
                        JOptionPane.showMessageDialog(this,"Usuario o contraseña incorrecta!","Error!",JOptionPane.ERROR_MESSAGE);
                        break;
                    }
                }

            }

        } catch (EOFException ex) { //This exception will be caught when EOF is reached
            System.out.println("End of file reached.");    
        } catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(this,ex.getMessage());
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this,ex.getMessage());
        }
  • 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-06-05T10:19:12+00:00Added an answer on June 5, 2026 at 10:19 am

    Please take a look at the implementation of Singleton

    With this you can access your object in an elegant way from everywhere and warranties that it will be uniquely instantiated.

    A simple implementation following the approach of singleton for it:

    package foo.bar;
    
    import java.io.IOException;
    import java.net.Socket;
    
    public final class MySingletonSocket extends Socket {
    
        private static Socket clientSocket;
    
        static {
            try {
                clientSocket = new MySingletonSocket("localhost", 4444);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private MySingletonSocket(final String address, final int port) throws IOException {
            super(address, port);
        }
    
        public static final Socket getInstance() {
            return clientSocket;
        }
    
    }
    

    From JFrame1 you can access it like:

    MySingletonSocket.getInstance()
    

    From JFrame2 you can access it in the same way.

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

Sidebar

Related Questions

This is more or less my first java project. I can't get my while
How can I get java classes from an xml file? In this situation, I
So, i have some question about xml Documents in Java. Can i get all
Im coding in Java.. Does anyone know how i can get the content of
How can I get output from Java anonymous classes? In .Net I would use
How can I get an equivalent of java.lang.Integer.MIN_VALUE on C++?
how can I get session symmetric key in Java? Is it hidden? (Looked for
How can I get an entity manager in a normal java class? I tried
How can I get video and audio streams from web cameras with Java (in
I'm using Spring Security 3.0.7 How can I get with java code the access

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.