Reputation: 13
I am in the early stages of working with a server and client connections through sockets. In my "ServerMain" class, I give the server a port to run on through a JTextField, which enters it through an ActionListener on a JButton. As soon as I press the button, it greys out, and the JTextArea below it stops appending text. What changes do I need to make so that the button and textarea both continue working after the server starts up?
ServerMain:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class ServerMain extends JFrame{
GraphicPanel m;
final static String newLine = "\n";
public static void main(String[] args) {
ServerMain s = new ServerMain("Testing Server");
s.setUpUI();
s.pack();
s.setVisible(true);
}
private void setUpUI() {
m = new GraphicPanel(this);
m.initialize();
this.add(m);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}
public void hostPort(String port){
System.out.println("Running hostPort method");
int p=-1;
try{
p= Integer.parseInt(port);
}
catch(Exception e){
JOptionPane.showInternalMessageDialog(this, "Must enter a number(0 - 65500) for the port");
}
m.print(""+p);
m.print("Running Try.");
try {
System.out.println("About to create socket");
@SuppressWarnings("resource")
ServerSocket serverSocket = new ServerSocket(p);
m.print("got the socket."+serverSocket);
Socket[] clientSocket = new Socket[2];
clientSocket[0] = serverSocket.accept();
clientSocket[1] = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket[0].getInputStream()));
m.print("Server Socket Initialized.");
System.out.println("S");
String inputLine;
while ((inputLine = in.readLine()) != null) {
m.print(inputLine);
m.print("Client: "+in.readLine());
}
}
catch (IOException e) {
m.print("Exception caught when trying to listen on port "
+ port + " or listening for a connection "+newLine+e+newLine);
m.print(e.getMessage());
}
}
public ServerMain(String s){
super(s);
}
}
And finally the GraphicPanel:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class GraphicPanel extends JPanel{
private String ip = "";
private String port = "";
private JTextField txt_port = new javax.swing.JTextField(20);
private JTextArea txa_info;
private JButton but_conDC;
private ServerMain s;
final static String newLine = "\n";
public GraphicPanel(ServerMain serv){
this.s = serv;
this.setPreferredSize(new Dimension(800,200));
}
public void initialize(){
txa_info = new JTextArea(10,70);
but_conDC = new JButton("Connect");
txt_port.setLocation(100, 100);
add(txt_port);
but_conDC.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
port = txt_port.getText();
System.out.println(port + " was in the text box");
if(port.equals("")){
emptyPort();
return;
}
but_conDC.setText("Disconnect");
repaint();
txa_info.setText("Hosting on port "+port);
s.hostPort(port);
}
});
but_conDC.setSize(5, 5);
add(but_conDC);
txa_info.setSize(800, 100);
txa_info.setEditable(false);
add(txa_info);
}
public void print(String s){
txa_info.append(s);
}
public void emptyPort() {
txa_info.append("ERROR: Attempted to host with no port! Try entering something like 4444"+newLine);
}
}
Upvotes: 0
Views: 147
Reputation: 3554
In your button's action, you basically open a server socket and wait for some communication. As you do this in the same thread as the UI is in, you block the tread, and make further interaction with the UI impossible.
You'll need to start a different thread and go into inter-thread communication between the UI-thread (called AWT Event Handler Thread in Java, in case you want to look it up) and whichever thread you start for your communication.
Upvotes: 1