Reputation: 25
I'm writing a simple client-server appa. It shoud work like that. I run the server app than i click button to create server. When server turn on i can the client app and connect to the server. Server also have JTextFild tfLog where message log pops out. My problem is that when i click button to create server if freez. Image
All looks like this:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class ServerGui extends JFrame implements ActionListener{
protected JButton bCreateServer;
public static JTextArea taLOg;
public ServerGui(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
dispose();
}
});
initLayout();
setVisible(true);
}
private void initLayout() {
setSize(600, 400);
setResizable(false);
setLayout(null);
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dimension.width/2-this.getSize().width/2,
dimension.height/2 - this.getSize().height/2);
bCreateServer = new JButton("Utwórz serwer");
bCreateServer.setBounds(20,20,150,25);
bCreateServer.addActionListener(this);
bCreateServer.setActionCommand("Create");
add(bCreateServer);
taLOg = new JTextArea();
taLOg.setEditable(false);
JScrollPane pane = new JScrollPane(taLOg);
pane.setBounds(20, 50, 400,300);
add(pane);
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ServerGui();
}
});
}
public void insertText(final String line){
if(SwingUtilities.isEventDispatchThread()){
taLOg.insert(line, 0);
}
else{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run(){
taLOg.insert(line, 0);
}
});
}
}
@Override
public void actionPerformed(ActionEvent e) {
String tmp = e.getActionCommand();
if(tmp.equals("Create")){
Server server = new Server(8080);
if(server.serverCreated){
insertText("Kliknięto Utwórz serwer\n");
server.clientsListener();
}
else{
insertText("Nie udało się utworzyć serwera\n");
}
}
}
}
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server{
private ServerSocket serverSocket;
private Socket socket;
public Boolean serverCreated = false;
public Server(int port){
try{
serverSocket = new ServerSocket(port);
}
catch(IOException e){
System.out.println("Błąd przy tworzeniu gniazda serwerowego.");
System.exit(-1);
}
serverCreated = true;
}
public void clientsListener(){
try{
socket = serverSocket.accept();
}
catch(IOException e){
System.out.println(e);
}
System.out.println(socket);
try{
serverSocket.close();
}
catch(IOException e){
System.out.println("Błąd przy zamykaniu gniazda serwerowego.");
System.exit(-1);
}
}
}
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client{
Socket clientSocket;
public Client(String hostname, int port){
try{
clientSocket = new Socket(hostname, port);
}
catch(UnknownHostException e){
System.out.println("Nieznany host.");
}
catch(IOException e){
System.out.println(e);
System.exit(-1);
}
System.out.println(clientSocket);
}
public static void main(String args[]){
Client client = new Client("localhost", 8080);
}
}
Upvotes: 0
Views: 98
Reputation: 2777
Your actionPerformed handler (which executes on the main UI thread of your application) makes a call to clientsListen() on the server object which then makes a call to .accept(). accept() will block until a client connects. This is why your GUI is hanging.
You need to create a new thread that will execute the code for your server.
Something like this in actionPerformed:
Thread t = new Thread(new Runnable() {
@Override
public void run() {
Server server = new Server(8080);
if(server.serverCreated){
server.clientsListener();
}
}
});
t.setDaemon(true);
t.start();
Also having the client start the server is a little strange. As an experiment I can understand it but if you give the client the ability to start the server you should probably give them a way to stop it. That will involve some sort of IPC between the threads which may be getting away from your concept of simple.
Upvotes: 3