So, I wanted to present a ****ty chat client. I thought might be something cool to add in codecall. Since the IRC channel seems dead...
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
To the code !
Packages
For does who are unfamiliar with these packages try to go read them later on...to get a hum about.Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*;
The receiver class
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)Code:class Receiver implements Runnable { Thread activity = new Thread(this); MulticastSocket so; JTextArea txt; Receiver(MulticastSocket sock, JTextArea txtAr) { so = sock; txt = txtAr; activity.start(); }
Running the application(Not the main)
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...).Code: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; } } }
The Constructor and GUI base.
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.Code: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); sendMess("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); }
sendMess method
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...)Code: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 !"); } }
Our actionListener
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...Code: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 main
Now. with our main we are actually going to start the application and do something creativeCode: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); } }
While 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
![]()
Very cool! Are you planning on expanding this chat client?
+rep
Last edited by Jordan; 08-09-2009 at 04:25 PM.
You should put the applet on your .codecall domain, I may get on IRC more often, I started using meebo again which does not support IRC.
You must spread some Reputation around before giving it to Turk4n again.
I love stuff like this+rep
Yeah, I am trying to make it read C-class IP or just connect to a server which will hold it up...I am having a lot of time to recode and debugging due to my habit of being lazy and getting massive brainfarts !
I could do that, nice thinking.
Do you, why thank you![]()
I cannot +rep u now, but great stuff indeed!
Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!
love it!
when will you finish the codecall chat client?
+rep indeed
please remind me later if i forgot to rep you<forum.codecall.net>
You must spread some Reputation around before giving it to Turk4n again.
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
www.amrosama.com | the unholy methods of javascriptCode:eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
Me too...
But really nice stuff, dude!You must spread some Reputation around before giving it to Turk4n again.
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks