Sunday, September 7, 2008 | |

Into GUI Java Network Chat

Finally I managed to make a simple GUI Java Network Chat Work.

  • It is two way. I meant client-server. Earlier I did a client to server messaging application.
  • Connection is given inside the code itself. Its temporarily given as localhost. You can make it work in any LAN by substituting it with clients IP address.
  • You will find many errors with this. Yet this will atleast teach you how not to programin JAVA. ;)
Code given here:
client.java

import java.net.*;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

class client extends JFrame implements ActionListener
{

Socket soc=null;
PrintWriter pr=null;
DataInputStream din =null;

JPanel panel1=new JPanel();
JButton button1=new JButton("Send");
JTextArea text1=new JTextArea(20,30);
JTextArea text2=new JTextArea(5,30);


class msgs implements Runnable
{
Thread t=null;
msgs()
{
t=new Thread(this);
t.start();
}
public void run()
{
while(true){
try{

text1.setText(text1.getText()+din.readLine());
}catch(Exception ae){}
}
}
}



client()
{
try{
soc=new Socket("localhost",2008);
din= new DataInputStream(soc.getInputStream());
pr=new PrintWriter(soc.getOutputStream(),true);

panel1.setLayout(null);
setLayout(new BorderLayout());
text1.setBounds(5,5,200,200);
text2.setBounds(5,210,200,50);
button1.setBounds(5,290,200,50);
panel1.add(text1);
panel1.add(text2);
panel1.add(button1);
getContentPane().add(panel1);
button1.addActionListener(this);

msgs m = new msgs();
}catch(Exception e){}


}

public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("Send"))
{

pr.println("Client: "+text2.getText());
text1.setText(text1.getText()+ "\nClient: "+text2.getText());
}
}
public static void main(String args[])
{
client client1=new client();
client1.setSize(250,400);
client1.setVisible(true);
}
}



server.java:

import java.net.*;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

class server extends JFrame implements ActionListener
{

ServerSocket servSoc = null;
Socket soc=null;
DataInputStream din=null;
PrintWriter pr = null;

JPanel panel1=new JPanel();
JButton button1=new JButton("Send");
JTextArea text1=new JTextArea(20,30);
JTextArea text2=new JTextArea(5,30);

class readMsg implements Runnable
{
Thread t=null;

readMsg()
{
t=new Thread(this);
t.start();
}
public void run()
{
while(true){
try{
text1.setText(text1.getText()+din.readLine());
}catch(Exception e){}
}
}
}

server()
{
super("Server");
try{


servSoc=new ServerSocket(2008);


panel1.setLayout(null);
setLayout(new BorderLayout());
text1.setBounds(5,5,200,200);
text2.setBounds(5,210,200,50);
button1.setBounds(5,290,200,50);
panel1.add(text1);
panel1.add(text2);
panel1.add(button1);
getContentPane().add(panel1);

setSize(250,400);
setVisible(true);

button1.addActionListener(this);

soc=servSoc.accept();
din= new DataInputStream(soc.getInputStream());
pr=new PrintWriter(soc.getOutputStream(),true);

readMsg r=new readMsg();


}catch(Exception e){}
}

public void actionPerformed(ActionEvent ae)
{
System.out.println("Aaaaa");
if(ae.getActionCommand().equals("Send"))
{

pr.println("Server: "+text2.getText());
text1.setText(text1.getText()+ "\nServer: "+text2.getText());
}
}
public static void main(String args[]) throws Exception
{
server server1=new server();

}
}

3 comments:

aindraneela said...

taanxx machaa

Seashells said...

Thanx... i hv been searching for days now but cudnt get such simple chat program. Thanx a lot.

kvanjava1 said...

Arigatou Gozaimashita...