+ Reply to Thread
Page 1 of 5 123 ... LastLast
Results 1 to 10 of 44

Thread: Simple Chat Client.

  1. #1
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Simple Chat Client.

    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
    Code:
    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
    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();
    		}
    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)
    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;
    		}
    	}
    }
    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.
    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);
    		}
    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
    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 !");
    			}
    		}
    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
    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 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
    Code:
    	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
    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
    Simple Chat Client.-jchatclient.png

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Simple Chat Client.

    Very cool! Are you planning on expanding this chat client?

    +rep
    Last edited by Jordan; 08-09-2009 at 04:25 PM.

  4. #3
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Simple Chat Client.

    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.

  5. #4
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Simple Chat Client.

    I love stuff like this +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Simple Chat Client.

    Quote Originally Posted by Jordan View Post
    Very cool! Are you planning on expanding this chat client?

    +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 !
    Quote Originally Posted by BlaineSch View Post
    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.
    I could do that, nice thinking.
    Quote Originally Posted by WingedPanther View Post
    I love stuff like this +rep
    Do you, why thank you

  7. #6
    Join Date
    Oct 2008
    Location
    Istog, Kosova
    Posts
    4,001
    Blog Entries
    1
    Rep Power
    40

    Re: Simple Chat Client.

    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!

  8. #7
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Simple Chat Client.

    Quote Originally Posted by MathX View Post
    I cannot +rep u now, but great stuff indeed!
    Haha, no need to +rep me
    I just want to contribute to everyone 4 3

  9. #8
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: Simple Chat Client.

    love it!
    when will you finish the codecall chat client?
    +rep indeed

    <forum.codecall.net>

    You must spread some Reputation around before giving it to Turk4n again.
    please remind me later if i forgot to rep you
    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"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  10. #9
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Simple Chat Client.

    Quote Originally Posted by amrosama View Post
    love it!
    when will you finish the codecall chat client?
    +rep indeed


    please remind me later if i forgot to rep you
    Lol no need, I will finish the basic stuff. Sooner or later I would really like to have some help. I though Chili5 might be able to help me?
    Since he seems to know more than me. Always ask the more knowledgeable for help :>

  11. #10
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,725
    Blog Entries
    2
    Rep Power
    90

    Re: Simple Chat Client.

    Me too...

    You must spread some Reputation around before giving it to Turk4n again.
    But really nice stuff, dude!
    Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

+ Reply to Thread
Page 1 of 5 123 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Client End of Chat Program Freezes
    By Hunter100 in forum C and C++
    Replies: 16
    Last Post: 07-21-2011, 01:07 PM
  2. Chat Client
    By Hunter100 in forum Java Help
    Replies: 8
    Last Post: 04-26-2010, 06:20 AM
  3. Creating a Chat Client and Server
    By Parabola in forum C# Programming
    Replies: 1
    Last Post: 08-25-2009, 09:47 AM
  4. Replies: 1
    Last Post: 04-21-2009, 04:57 PM
  5. Simple chat client and understanding the other side has logged in
    By penkomitev in forum General Programming
    Replies: 4
    Last Post: 10-01-2008, 05:20 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts