Javaでは、オープンソケットを取得するにはどうすればよいですか? 2 つの JFrame があります。最初の JFrame で、クライアント ソケットの接続を開きます。この同じ JFrame 内に、別の JFrame (JFrame2) のインスタンスを作成します。ここで、JFrame1 から JFrame2 に同じソケットを取得して、サーバーのソケットとの通信を続けたいと考えています。
login.java (最初の 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 (最初の 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());
}