Reputation: 69
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:
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);
}
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());
}
Upvotes: 3
Views: 8151
Reputation: 33534
Make that client Socket static.
eg:
static Socket client = new Socket;
As static member is for the class, and all objects share the same static variable, so the 2nd frame will have the same client socket.
Another thing you can do is to pass the object reference of the client socket to the 2nd frame.
Upvotes: 0
Reputation: 23903
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.
Upvotes: 2
Reputation: 82579
The best thing you can do is only do things that have to do with displaying things in your JFrame.
A JFrame shouldn't have a socket in it. I know it CAN, but it shouldn't. You should have a class outside of your JFrame for business logic. Then your controller object can spawn both your JFrames and your sockets.
Think of it like this: imagine instead of usign a JFrame you were writing your application for the command line. Then, when you make your JFrames, have them issue commands to that command line application. You'll find issues like this simply go away when you write your program with good layering.
Upvotes: 1
Reputation: 2482
You can share the reference to the Client socket inside both JFrames.
This is possible through one of these two methods:
Inside JFrame2, have a method like this:
public void setSocket(Socket s) {
this.socket = s;
}
Then, call this method from JFrame1, and pass the socket in.
Upvotes: 0