Why not have an Applet chat client, so I done a simple chat client making it possible for multiply users to type and chat, hopefully it will work...PROPERLY :D
To the code !
Packages
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*;For does who are unfamiliar with these packages try to go read them later on...to get a hum about.
The receiver class
class Receiver implements Runnable {
Thread activity = new Thread(this);
MulticastSocket so;
JTextArea txt;
Receiver(MulticastSocket sock, JTextArea txtAr) {
so = sock;
txt = txtAr;
activity.start();
}
Here is our class that will represent the data we recieve :> MulticastSocket. This is something we will need if we want to have computers communicating each other. Since we are going to use a server holding up this client...So the MulticastSocket would be your "real" ip to the users.(Also 127.0.0.1 does not work :X)Running the application(Not the main)
public void run() {
byte[] data = new byte[1024];
while(true)
try {
DatagramPacket packet = new DatagramPacket(data,data.length);
so.receive(packet);
String mess = new String(data,0,packet.getLength());
txt.append(mess+ "\n");
}
catch(IOException e) {
break;
}
}
}
While running we need to have something that actually will respond to our needs and so. Hence we send data in form of byte...(You can change if you want to something else like long or so...)Our data, will be represented as text lines.(Which is the lines you send and get...).The Constructor and GUI base.
public class JChat extends JFrame implements ActionListener {
String name;
InetAddress iadr;
int port;
MulticastSocket so;
JTextArea txt = new JTextArea();
JScrollPane sp = new JScrollPane(txt);
JTextField write = new JTextField();
JButton quit = new JButton("Go Offline");
public JChat(String username, String groupAdr, int portNr) throws IOException {
name = username;
iadr = InetAddress.getByName(groupAdr);
port = portNr;
so = new MulticastSocket(port);
so.joinGroup(iadr);
new Receiver(so,txt);
[B]sendMess[/B]("Online");
setTitle("Chatting with "+ name);
txt.setEditable(true);
add(quit,BorderLayout.NORTH);
add(sp,BorderLayout.CENTER);
add(write,BorderLayout.SOUTH);
quit.addActionListener(this);
write.addActionListener(this);
setSize(400,250);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
Here we have our constructor and GUI base for the client.By using standards, such as username and port and IP we can actually send and receive data...With ScrollPane we are able to use the scrolling function that a normal window have when content tend to be more than viewable by standard resolution...Plus we are adding each component to a bordered layout, making it simple for us to just place components and items.sendMess method
public void sendMess(String s) {
byte[] data = (name + ": " + s).getBytes();
DatagramPacket packet = new DatagramPacket(data,data.length,iadr,port);
try {
so.send(packet);
}
catch(IOException ie) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, "Data overflow !");
}
}
Now, by sending out text lines as bytes through Datagram packets we can view our sent lines... else we will get an overflow(Our Exception...)Our actionListener
public void actionPerformed(ActionEvent e) {
if(e.getSource()==write) {
sendMess(write.getText());
write.setText("");
}
else if(e.getSource()==quit) {
sendMess("Offline");
try {
so.leaveGroup(iadr);
}
catch(IOException ie) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, "Data overflow, connection error !");
}
so.close();
dispose();
System.exit(0);
}
}
The action listener, will register everything we do by the GUI. Typing and sending it...Also by using our offline button we can exit the application and terminate our connection with the client...The main
public static void main(String[] arg) throws IOException {
String in = JOptionPane.showInputDialog(null,"What's your name?");
if(arg.length>0)
in = arg[0];
new JChat(in,"Add a D-Class IP",9876);
}
}
Now. with our main we are actually going to start the application and do something creative :DWhile I am using our arg(which is also a container), to store the currently user(YOU and no one else...). Which makes it covient in my opinion... SO by adding our parameters while creating a new JChat(username,D-Class IP,port).
And we have our fantastic chat client !
Cheers !
Output
[ATTACH]1879[/ATTACH]


Sign In
Create Account




Back to top










