Im tryng to build an async java client socket, which is always listening for responses from a server. My java program has a GUI so I understand I can not simply put the read method in a thread or runnable because it will block my gui from showing, etc.. I've tried using a swingworker and also an executorservice but it has not worked. Any help would be greatly appreciated, here's some code!
public class ClientWindow {
//Variable declarations
public Selector selector;
public SocketChannel channel;
//Connects when program starts up..
btnConnectActionPerformed (ActionEvent evt){
selector = Selector.open();
channel = SocketChannel.open();
channel.configureBlocking(false);
channel.register(selector, SelectionKey.OP_CONNECT);
channel.connect(new InetSocketAddress(getProperties.server, port));
connect(channel);
//..stuff to make gui let me know it connected
}
//connect method
private void connect(SocketChannel channel) throws IOException {
if (channel.isConnectionPending()) {
channel.finishConnect();
}
channel.configureBlocking(false);
channel.register(selector, SelectionKey.OP_WRITE);
}
//read method
private void read(SocketChannel channel) throws IOException
{
ByteBuffer readBuffer = ByteBuffer.allocate(1000);
readBuffer.clear();
int length;
try {
length = channel.read(readBuffer);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Trouble reading from server\nClosing connection", "Reading Problem", JOptionPane.ERROR_MESSAGE);
channel.close();
btnDiscon.doClick();
return;
}
if (length == -1) { //If -1 is returned, the end-of-stream is reached (the connection is closed).
JOptionPane.showMessageDialog(null, "Nothing was read from server\nClosing connection", "Closing Connection", JOptionPane.ERROR_MESSAGE);
channel.close();
btnDiscon.doClick();
return;
}
readBuffer.flip();
byte[] buff = new byte[1024];
readBuffer.get(buff, 0, length);
String buffRead = new String(buff);
System.out.println("Received: " + buffRead);
}
}
So I managed to get it working, probably was missing a line or two or something. Not sure since it's basically how i was tryng to do it before.
//* Nested class to constantly listen to server *//
public class ReaderWorker extends SwingWorker<Void, Void> {
private SocketChannel channel;
public ReaderWorker(SocketChannel channel) {
this.channel = channel;
}
@Override
protected Void doInBackground() throws Exception {
while (true) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
System.out.println("Executed");
try {
read(channel);
} catch (IOException ex) {
Logger.getLogger(ClientWindow.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.