Hello,
I've been working on this chat program which has a gui.
Basically for now im just pressing start then connect to get a connection.
Start is for the server window and if you press connect that will be the client window.
I can send messages from the client to the server but not the other way around.
It's been driving me nuts cause I'm sure there is a simple solution but I could not figure it out.
Any help would be greatrly appreciated
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.*;
import java.net.Socket;
import java.net.ServerSocket;
public class Chat extends Frame implements ActionListener,WindowListener,Runnable {
/**
*
*/
private static final long serialVersionUID = 1L;
Socket client;
Socket server;
ServerSocket Listen_Socket;
BufferedReader br;
InputStreamReader is;
PrintWriter pw;
TextArea DialogScreen;
TextArea Status;//on bottom
TextField textChat;//for sending
TextField textHost;//Enter host
TextField textPort;//Enter Port
Button ChangeH;//change host
Button ChangeP;//change port
Button Send;
Button Start;
Button Connect;
Button Disconnect;
Label labelH;
Label labelP;
Frame Display;
String line;
int state;
boolean more=true;
protected final static boolean auto_flush=true;
private final static int DEFAULTPORT=44004;
public static void main(String[] args)
{
new Chat ();
}
public Chat(){
Display=new Frame("Chat") ;
Display.setSize(650, 500);
Display.setVisible(true);
Display.addWindowListener(this);
GridBagLayout displ=new GridBagLayout();
Display.setLayout(displ);
textChat=new TextField(7);
DialogScreen=new TextArea(" ",7,80);
ChangeH=new Button("Change Host");
Send=new Button("Send");
labelH=new Label("Host");
textHost=new TextField(4);
ChangeH=new Button("Change Host");
Start=new Button("Start Server");
labelP=new Label("Port");
textPort=new TextField(4);
ChangeP=new Button("Change Port");
Connect=new Button("Connect");
Disconnect=new Button("Disconnect");
Status=new TextArea("Status",4,80);
// textPort.setText("44044");//set default port value if server is selected
//ChangeP.setEnabled(false); //use setEnabled to disable buttons
// textChat.setEditable(false);//false until there is connection
double colWeight[]={1,1,1,1,1,1,1,1};
double rowWeight[]={1,1,1,1,1,1,1,1,1,1,1,1,1};
int colWidth[]={1,1,1,1,1,1,1,1};
int rowHight[]={1,1,1,1,1,1,1,1,1,1,1,1,1};
//apply weights
displ.rowHeights=rowHight;
displ.rowWeights=rowWeight;
displ.columnWeights=colWeight;
displ.columnWidths=colWidth;
GridBagConstraints c=new GridBagConstraints();
c.fill=GridBagConstraints.BOTH;
c.weightx=1;
c.weighty=1;
c.gridheight=7;
c.gridwidth=8;
c.gridx=0;
c.gridy=0;
displ.setConstraints(DialogScreen,c);
c.fill=GridBagConstraints.HORIZONTAL;
c.weightx=0;
c.weighty=0;
c.gridheight=1;
c.gridwidth=7;
c.gridx=0;
c.gridy=7;
displ.setConstraints(textChat,c);
c.fill=GridBagConstraints.HORIZONTAL;
c.weightx=0;
c.weighty=0;
c.gridheight=1;
c.gridwidth=1;
c.gridx=7;
c.gridy=7;
displ.setConstraints(Send,c);
c.fill=GridBagConstraints.HORIZONTAL;
c.weightx=0;
c.weighty=0;
c.gridheight=1;
c.gridwidth=1;
c.gridx=1;
c.gridy=8;
displ.setConstraints(labelH,c);
c.fill=GridBagConstraints.HORIZONTAL;
c.weightx=0;
c.weighty=0;
c.gridheight=1;
c.gridwidth=3;
c.gridx=2;
c.gridy=8;
displ.setConstraints(textHost,c);
c.fill=GridBagConstraints.HORIZONTAL;
c.weightx=0;
c.weighty=0;
c.gridheight=1;
c.gridwidth=1;
c.gridx=6;
c.gridy=8;
displ.setConstraints(ChangeH,c);
c.fill=GridBagConstraints.HORIZONTAL;
c.weightx=0;
c.weighty=0;
c.gridheight=1;
c.gridwidth=1;
c.gridx=7;
c.gridy=8;
displ.setConstraints(Start,c);
c.fill=GridBagConstraints.HORIZONTAL;
c.weightx=0;
c.weighty=0;
c.gridheight=1;
c.gridwidth=1;
c.gridx=1;
c.gridy=9;
displ.setConstraints(labelP,c);
c.fill=GridBagConstraints.HORIZONTAL;
c.weightx=0;
c.weighty=0;
c.gridheight=1;
c.gridwidth=3;
c.gridx=2;
c.gridy=9;
displ.setConstraints(textPort,c);
c.fill=GridBagConstraints.HORIZONTAL;
c.weightx=0;
c.weighty=0;
c.gridheight=1;
c.gridwidth=1;
c.gridx=6;
c.gridy=9;
displ.setConstraints(ChangeP,c);
c.fill=GridBagConstraints.HORIZONTAL;
c.weightx=0;
c.weighty=0;
c.gridheight=1;
c.gridwidth=1;
c.gridx=7;
c.gridy=9;
displ.setConstraints(Connect,c);
c.fill=GridBagConstraints.HORIZONTAL;
c.weightx=0;
c.weighty=0;
c.gridheight=1;
c.gridwidth=1;
c.gridx=7;
c.gridy=10;
displ.setConstraints(Disconnect,c);
c.fill=GridBagConstraints.BOTH;
c.weightx=1;
c.weighty=1;
c.gridheight=3;
c.gridwidth=8;
c.gridx=0;
c.gridy=11;
displ.setConstraints(Status,c);
Display.add(Status);
Display.add(Disconnect);
Display.add(Connect);
Display.add(ChangeP);
Display.add(textPort);
Display.add(labelP);
Display.add(ChangeH);
Display.add(Start);
Display.add(textHost);
Display.add(labelH);
Display.add(Send);
Display.add(textChat);
Display.add(DialogScreen);
Send.addActionListener(this);
Disconnect.addActionListener(this);
Connect.addActionListener(this);
ChangeP.addActionListener(this);
textPort.addActionListener(this);
ChangeH.addActionListener(this);
Start.addActionListener(this);
textHost.addActionListener(this);
textChat.addActionListener(this);
Display.pack();
new Thread(this).start();
}
@Override
public void run() {
while(more){
//System.out.println("Chat is Running");
while(server==null&&more){
if(br!=null){
System.out.println("Listening to Socket");
try {
while((line=br.readLine())!=null&&more){
System.out.println("cool");
DialogScreen.append(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
@Override
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
Display.dispose();
try {
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object source=e.getSource();
if(source== Start){
try {
Listen_Socket=new ServerSocket(DEFAULTPORT);
client=Listen_Socket.accept();
is=new InputStreamReader(client.getInputStream());
br=new BufferedReader(is);
pw=new PrintWriter(client.getOutputStream(),auto_flush);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if(source== Connect){
try {
server=new Socket("localhost",DEFAULTPORT);
//server.getPort();
is=new InputStreamReader(server.getInputStream());//
br=new BufferedReader(is);//
pw=new PrintWriter(server.getOutputStream(),auto_flush);
state=2;
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if(source== textChat){
String temp;
temp=textChat.getText();
System.out.println("ok");
pw.println(temp);
textChat.requestFocus();
}
}
}
.