Reputation: 249
I'm writing a program that communcaties two clients through server When I run it without the start method, it works fine and looks like this
but when I try to listen for connections I only get blank white window. The program stops at conn = server.accept()
which is quite logical because i dont have a client yet, but why doesn't it how anything?
Also, can I get rid of that white border somehow?
import java.awt.*;
import java.awt.print.PrinterException;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class Server extends JFrame {
private ServerSocket server;
private Socket conn;
private JTextArea TA;
private JPanel panel1;
private final int port = 1234;
public Server() {
super("Server");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(new Dimension(400, 300));
Container c = getContentPane();
c.setBackground(Color.BLACK);
TA = new JTextArea();
TA.setEditable(true);
TA.setBackground(Color.BLACK);
TA.setForeground(Color.GREEN);
c.add(new JScrollPane(TA));
setVisible(true);
TA.append("Server started.");
}
public void start() {
try {
TA.append("ServerSocket created. \n Listering for connections...");
server = new ServerSocket(port, 2);
for (int i = 0; i < 2; i++) {
conn = server.accept();
TA.append("User " + i + " connected");
}
} catch (IOException ioe) {
System.out.println(ioe);
}
}
}
import javax.swing.SwingUtilities;
`
public class ServerLauncher {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
Server s = new Server();
// s.start();
}
});
}
}
i was following a youtube guide to write this, and it gets the same bug but looks like it's discontinued after part 3 so I never found out how to fix it. https://www.youtube.com/watch?v=YqTB-S_5mss
Upvotes: 0
Views: 354
Reputation: 21902
It's happening because scheduled in the AWT Event Dispatcher Thread and you're blocking it with the accept method, so nothing shows up.
Try this instead:
public static void main(String[] args) {
Server s = new Server();
s.start();
}
public Server() {
super("Server");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(new Dimension(400, 300));
Container c = getContentPane();
c.setBackground(Color.BLACK);
TA = new JTextArea();
TA.setEditable(true);
TA.setBackground(Color.BLACK);
TA.setForeground(Color.GREEN);
c.add(new JScrollPane(TA));
setVisible(true);
log(TA, "Server started.");
}
public void log(JTextArea txt, String message) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
txt.append(message);
}});
}
public void start() {
try {
log(TA, "ServerSocket created. \n Listering for connections...");
server = new ServerSocket(port, 2);
for (int i = 0; i < 2; i++) {
conn = server.accept();
log(TA, "User " + i + " connected");
}
} catch (IOException ioe) {
System.out.println(ioe);
}
}
Upvotes: 1
Reputation: 109813
invoking Server s = new Server();
from SwingUtilities.invokeLater(new Runnable()
{ in public class ServerLauncher {
is wrong idea, you wouldnt to start hard and long running code - Workers Thread
from invokeLater
(notifier for Event Dispatch Thread
), more read in Oracle tutorial Concurency in Swing you would need to start()
from SwingWorker
, Runnable#Thread
, Thread
TA.append("User " + i + " connected");
you have an issue with (again) with Concurency in Swing
, all updates from Worker Thread
must be done on EDT, wrapped into invokeLater()
from Runnable#Thread
to start Server s = new Server();
then inside loop to TA.append("User " + i + " connected");
wrapped into invokeLater
Upvotes: 2