Hello everyone,
I am in big trouble, actually, I am developing a chat in Java. I am still on the server part, but the problem is that when i am clicking on a button to instantiates a class, the whole program freeze, please help me to figure out this problem. I am using Netbeans as compiler. I just want to server to listen to the port when the button "btnServerOn" (in ServerConnect) is clicked.
Thanks in advance.
This is the "Server" class:
package ignisv4;
import java.io.*;
import java.net.*;
public class Server {
int portNumber = 5000;
String serverStatus;
Socket clientSocket = null;
PrintWriter out = null;
BufferedReader in = null;
public void setPortNumber(int PN){
this.portNumber = PN;
}
public int getPortNumber(){
return portNumber;
}
public String getServerStatus(){
return serverStatus;
}
public void setServerStatus(String status){
serverStatus = status;
}
public void createServer(){
try{
ServerSocket ss = new ServerSocket(portNumber);
Socket incoming = ss.accept();
//InputStream incomingStream = new InputStream();
//incomingStream = incoming.getInputStream();
BufferedReader in = new BufferedReader (new InputStreamReader(incoming.getInputStream()));
PrintWriter out = new PrintWriter (incoming.getOutputStream(), true /* autoFlush */);
boolean done = false;
while (!done)
{
String messageline = in.readLine();
if (messageline == null){
done = true;
setServerStatus("OFF");
}
else
{
out.println("Echo: " + messageline);
setServerStatus("ON");
if (messageline.trim().equals("BYE"))
done = true;
setServerStatus("OFF");
}
}
incoming.close();
}catch(Exception e){}
}
This is the "ServerConnect" class:
package ignisv4;
public class ServerConnect extends javax.swing.JFrame {
/** Creates new form ServerConnect */
public ServerConnect() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btnServerOn = new javax.swing.JButton();
txtServerStatus = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btnServerOn.setText("Server On");
btnServerOn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnServerOnActionPerformed(evt);
}
});
jLabel1.setText("Server Status");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(112, 112, 112)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnServerOn)
.addComponent(txtServerStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(85, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(141, Short.MAX_VALUE)
.addComponent(btnServerOn)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtServerStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(110, 110, 110))
);
pack();
}// </editor-fold>
private void btnServerOnActionPerformed(java.awt.event.ActionEvent evt) {
Server cs1 = new Server();
cs1.createServer();
txtServerStatus.setText(cs1.getServerStatus());
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ServerConnect().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnServerOn;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField txtServerStatus;
// End of variables declaration
}