Jump to content

Simple Chat Client.

* * * * - 1 votes

  • Please log in to reply
43 replies to this topic

#1
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
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 :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 :D
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
[ATTACH]1879[/ATTACH]

Attached Files


Posted Image

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Very cool! Are you planning on expanding this chat client?

+rep

Edited by Jordan, 09 August 2009 - 03:25 PM.


#3
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
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.

Quote

You must spread some Reputation around before giving it to Turk4n again.


#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
I love stuff like this :) +rep
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

Jordan said:

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 !

BlaineSch said:

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.

WingedPanther said:

I love stuff like this :) +rep
Do you, why thank you :D
Posted Image

#6
MathX

MathX

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,001 posts
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!


#7
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

MathX said:

I cannot +rep u now, but great stuff indeed!

Haha, no need to +rep me :)
I just want to contribute to everyone 4 3 :D
Posted Image

#8
amrosama

amrosama

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 8,674 posts
love it!
when will you finish the codecall chat client?
+rep indeed

Quote

<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"
eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
www.amrosama.com | the unholy methods of javascript

#9
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

amrosama said:

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 :>
Posted Image

#10
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
Me too...

Quote

You must spread some Reputation around before giving it to Turk4n again.

But really nice stuff, dude!
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#11
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

marwex89 said:

Me too...



But really nice stuff, dude!

Haha, thanks :D
No problem on rep I don't mind if you rep- me even :D
Posted Image

#12
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
Hmmm *evil smile*

Nah. Can't -rep you either. :(
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users