Galera,
Fiz a aplicação abaixo e estou com o seguinte problema; Minha aplicação só recebe os dados do servidor, quando o mesmo é fechado, ou seja, quando o servidor é fechado eu recebo as informações, caso contrário eu não as recebo. Como faço para que estas informações cheguem de tempo em tempo, sem que o servidor precise ser fechado???
// EchoClient.java
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Janela extends JFrame
{
public JTextField tf1;
public JButton bt1;
public JLabel lb1;
public JLabel lb2;
public Janela()
{
super("Client - ATech - STI");
Container container = getContentPane();
container.setLayout(new FlowLayout());
tf1 = new JTextField(10);
tf1.setText("sti-nb2");
bt1 = new JButton("OK");
lb1 = new JLabel("Servidor");
lb2 = new JLabel("Conectando com o Servidor...");
bt1.setBounds(new Rectangle(515, 442, 75, 30));
container.add(lb1);
container.add(tf1);
container.add(bt1);
container.add(lb2);
setSize(320, 100);
tf1.setEditable(false);
setVisible(true);
}
public JTextField getTf1()
{
return tf1;
}
}
public class EchoClient
{
public static void main(String[] args) throws IOException
{
BufferedReader in = null;
Janela j = new Janela();
// Centraliza a janela na tela
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w = j.getSize().width;
int h = j.getSize().height;
int x = (dim.width-w)/2;
int y = (dim.height-h)/2;
j.setLocation(x, y);
j.setResizable(false);
j.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
j.show();
while (1 > 0)
{
try
{
InetAddress addr = InetAddress.getByName("sti-nb2");
int port = 40;
SocketAddress sockaddr = new InetSocketAddress(addr, port);
Socket sock = new Socket();
int timeoutMs = 60000; // 1 min
sock.connect(sockaddr, timeoutMs);
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
j.lb2.setText(" Conectado ");
String str;
while ((str = in.readLine()) != null)
{
str = in.readLine();
JOptionPane.showMessageDialog(null, str, "Atençao", JOptionPane.INFORMATION_MESSAGE);
}
in.close();
sock.close();
j.lb2.setText(" Desconectado ");
}
catch (UnknownHostException e)
{
JOptionPane.showMessageDialog(null, "Host desconhecido.", "Erro", JOptionPane.ERROR_MESSAGE);
//System.exit(1);
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, "Host de destino inacessivel.", "Erro", JOptionPane.ERROR_MESSAGE);
//System.exit(1);
}
}
}
}
