necesito de su ayuda. Algo extraño ocurre en esta parte del codigocódigo. abc = es un objeto creado que maneja la interfaz grafica y la conexion tcp. hasta ahiahí todo bien el objeto abc crea la interfaz gráfica y maneja maneja la conexionconexión tcp. la interfaz tiene un botonbotón de conexionconexión que al pulsar crea el socket y establece la conexionconexión con el servidor.
dentro del while esta System.out.println(); el cual no hace nada esta puesto ahiahí porque de no estarlo no entra en el ciclo if cuando se establece la conexionconexión. alguien puede explicarme esto. Por que funciona de esta manera? Alguna manera de mejorarlo o solucionarlo? package gui;
/** *
- @author Eliezer */
import java.awt.Color; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.BorderFactory; import javax.swing.border.Border; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.Dimension; import java.awt.Font; import java.awt.Dimension; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseListener;
import network.NetworkConector;
public class BindingControlConection extends JFrame{
private int numberDevice=0;
private Boolean conected=false;
private JPanel panelDevice[];
private JTextArea displayOfTrama[];
private JTextField fieldForSocket[];
private JButton btnConnect[], btnDisconnect[];
private JLabel lblIndicatorConection[], lblIpAddress[];
public NetworkConector socketDevice[];
public BindingControlConection(){
this.setLayout(null);
createPanelDevice(2);
this.btnConnect[0].addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent evt){
Object obj = evt.getSource();
if (btnConnect[0] == obj){
connectServer();
}
}
});
this.btnDisconnect[0].addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent evt){
Object obj = evt.getSource();
if (btnDisconnect[0] == obj){
disconnectServer();
}
}
});
}
private void connectServer(){
String strSocket[] = fieldForSocket[0].getText().split(":");
String ip = strSocket[0];
int port = Integer.parseInt(strSocket[1]);
socketDevice[0] = new NetworkConector(ip, port);
socketDevice[0].setConection();
System.out.println("ip: "+ip+"\nport: "+port);
this.conected=true;
/* displayOfTrama[0].setText(socketDevice[0].getData());
System.out.println(socketDevice[0].getData());
*/
}
public static void main(String[] args){
BindingControlConection abc = new BindingControlConection();
abc.setEnabled(true);
abc.setMinimumSize(new Dimension(280,320));
abc.setVisible(true);
abc.setLocationRelativeTo(null);
//abc.setLayout(null);
abc.setExtendedState(MAXIMIZED_BOTH);
abc.setDefaultCloseOperation(EXIT_ON_CLOSE);
while (true){
System.out.println(); // Al quitar esta linea no entra en el condicional if
if (abc.isConnected()){
System.out.println("eni is conected)=");
String data = abc.socketDevice[0].getData();
System.out.println(data);
System.out.println("imprimiendo data");
abc.showData(data);
}
}
}
}
package gui;
/**
*
* @author Eliezer
*/
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseListener;
import network.NetworkConector;
public class BindingControlConection extends JFrame{
private int numberDevice=0;
private Boolean conected=false;
private JPanel panelDevice[];
private JTextArea displayOfTrama[];
private JTextField fieldForSocket[];
private JButton btnConnect[], btnDisconnect[];
private JLabel lblIndicatorConection[], lblIpAddress[];
public NetworkConector socketDevice[];
public BindingControlConection(){
this.setLayout(null);
createPanelDevice(2);
this.btnConnect[0].addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent evt){
Object obj = evt.getSource();
if (btnConnect[0] == obj){
connectServer();
}
}
});
this.btnDisconnect[0].addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent evt){
Object obj = evt.getSource();
if (btnDisconnect[0] == obj){
disconnectServer();
}
}
});
}
private void connectServer(){
String strSocket[] = fieldForSocket[0].getText().split(":");
String ip = strSocket[0];
int port = Integer.parseInt(strSocket[1]);
socketDevice[0] = new NetworkConector(ip, port);
socketDevice[0].setConection();
System.out.println("ip: "+ip+"\nport: "+port);
this.conected=true;
/* displayOfTrama[0].setText(socketDevice[0].getData());
System.out.println(socketDevice[0].getData());
*/
}
public static void main(String[] args){
BindingControlConection abc = new BindingControlConection();
abc.setEnabled(true);
abc.setMinimumSize(new Dimension(280,320));
abc.setVisible(true);
abc.setLocationRelativeTo(null);
//abc.setLayout(null);
abc.setExtendedState(MAXIMIZED_BOTH);
abc.setDefaultCloseOperation(EXIT_ON_CLOSE);
while (true){
System.out.println(); // Al quitar esta linea no entra en el condicional if
if (abc.isConnected()){
System.out.println("eni is conected)=");
String data = abc.socketDevice[0].getData();
System.out.println(data);
System.out.println("imprimiendo data");
abc.showData(data);
}
}
}
}