I was creating a chat application in Java Swing/Socket Programming. The same when I created in non GUI app. it worked [url]http://www.daniweb.com/code/snippet448.html [/url]. But in GUI, they can't connect now. I have tried it thru many ways but still m getting errors for connection.
Can anyone can help me......
/*********************
ChatServer.java
**********************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class ChatServer extends JFrame implements ActionListener
{
JPanel jpA = new JPanel();
JTextArea txtA = new JTextArea(10,10);
JTextArea txtB = new JTextArea(10,10);
JButton send = new JButton("Send");
JTextField status = new JTextField(20);
int PORT = 8145;
InputStream inStream;
DataInputStream inDataStream;
OutputStream outStream;
DataOutputStream outDataStream;
String message = "";
ServerSocket sock = null;
Socket conn = null;
public ChatServer(String title){
setTitle(title);
Container c = this.getContentPane();
send.addActionListener(this);
jpA.add(txtA);
jpA.add(send);
jpA.add(txtB);
jpA.add(status);
c.add(jpA);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==send){
try{
InetAddress HOST = InetAddress.getLocalHost();
Socket sock = new Socket(HOST,PORT);
outStream = conn.getOutputStream();
outDataStream = new DataOutputStream (outStream);
message = txtA.getText();
outDataStream.writeUTF(message);
status.setText("Message have been sent");
}catch(Exception e){status.setText(message);}
}
}
public void connectClient(){
try{
sock = new ServerSocket(PORT);
sock.accept();
message = "Client Connected";
System.out.println(message);
}catch(Exception e){status.setText(message);System.out.println("Connection Problem..."+e);}
status.setText(message);
}
public static void main(String args[]){
ChatServer cs = new ChatServer("This is the Server");
cs.setVisible(true);
cs.setSize(400,350);
cs.connectClient();
}
}
/*********************
ChatClient.java
**********************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class ChatClient extends JFrame implements ActionListener
{
JPanel jpA = new JPanel();
JTextField tfa = new JTextField(15);
JTextField tfb = new JTextField(5);
JButton connect = new JButton("Connect");
JTextArea txtA = new JTextArea(10,10);
JTextArea txtB = new JTextArea(10,10);
JButton send = new JButton("Send");
JButton recieve = new JButton("Recieve");
JTextField status = new JTextField(20);
int PORT = 8145;
String HOST = "";
InputStream inStream;
DataInputStream inDataStream;
OutputStream outStream;
DataOutputStream outDataStream;
String message = "";
Socket sock = null;
public ChatClient(String title){
setTitle(title);
Container c = this.getContentPane();
connect.addActionListener(this);
send.addActionListener(this);
recieve.addActionListener(this);
jpA.add(tfa);
jpA.add(connect);
jpA.add(txtA);
jpA.add(send);
jpA.add(recieve);
jpA.add(txtB);
jpA.add(status);
c.add(jpA);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==connect){
status.setText("Connecting.......");
HOST = tfa.getText();
try{
sock = new Socket(HOST,PORT);
inStream = sock.getInputStream ();
inDataStream = new DataInputStream ( inStream );
message = inDataStream.readUTF();
status.setText("You are connected");
}catch(Exception e){status.setText(message);}
}
if(ae.getSource()==recieve){
try{
System.out.println("sock details: "+sock);
inStream = sock.getInputStream ();
System.out.println("sock inoutstream details: "+inStream);
inDataStream = new DataInputStream ( inStream );
message = inDataStream.readUTF();
System.out.println("message is that : "+message);
if(message==null)
txtB.setText("No message was recieved");
else
txtB.setText(message);
}catch(Exception e){status.setText(""+e);}
}
}public static void main(String args[]){
ChatClient cc = new ChatClient("This is the Client");
cc.setVisible(true);
cc.setSize(400,350);
}
}