DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   creating an instant messenger program in java (http://www.daniweb.com/forums/thread20811.html)

stupidenator Mar 24th, 2005 3:26 pm
creating an instant messenger program in java
 
Hey Everyone,
This is my first post. Recently, I have been working on an instant messenger program in java. I have created the networking part of the client, and I have almost finished the server side. I know the program works and I am allowed to send messages, but now I am thinking about adding a feature that lets the user see who else is connected to the server, but i can not think of how to do this. Anyone have any ideas?

aj.wh.ca Mar 24th, 2005 6:01 pm
Re: creating an instant messenger program in java
 
Do you intent to allow the client A to see all the other clients who are logged in .
I think you should use broadcast from the server to clients whenever a new client gets added or leaves. The server must already be maintaing the list of clients.
Also whenever the new client gets added , the server should send him the list of all logged clients.

cheers,
aj.wh.ca

stupidenator Mar 24th, 2005 6:43 pm
Re: creating an instant messenger program in java
 
I do want all other clients to see who is logged in. I was originally thinking that I would save all names to a linked list... but the problem I am having is that I can not figure out how I am going to send the username to the server from the client, and then I can't figure out how to get the server to conitinuousely update who has logged in/off and then send the list back to the client.

aj.wh.ca Mar 24th, 2005 7:14 pm
Re: creating an instant messenger program in java
 
Quote:

Originally Posted by stupidenator
I do want all other clients to see who is logged in. I was originally thinking that I would save all names to a linked list... but the problem I am having is that I can not figure out how I am going to send the username to the server from the client, and then I can't figure out how to get the server to conitinuousely update who has logged in/off and then send the list back to the client.

Are you using TCP or UDP sockets for networking. mostly for instant messenger people use UDP.
As per your saying -- "I can not figure out how I am going to send the username to the server from the client".
I really do not understand what is the problem in this. If you are implementing some thing like yahoo messenger then the very first step from your client to server would be authenticating the user name.
I think I could help more if you specify more details. And what exactly you looking for -- code , logic, idea ?

stupidenator Mar 25th, 2005 1:37 am
Re: creating an instant messenger program in java
 
I was using a TCP connection.... But I may look at UDP... I'm not going through yahoo or aim or anything else, I am basically just setting up my client to connect to another server that just receives the message and relays it back to everyone else signed in.

stupidenator Mar 25th, 2005 11:16 am
Re: creating an instant messenger program in java
 
Here is the code for my server program.

import java.io.*;
import java.net.*;
import java.util.*;

public class Server
{
        ArrayList clientOutputStreams;
       
        public void go()
        {
                clientOutputStreams = new ArrayList();
               
                try
                {
                        ServerSocket serverSock = new ServerSocket(4999);
                       
                        while (true)
                        { // set up the server writer function and then begin at the same
                          // the listener using the Runnable and Thread
                                Socket clientSock = serverSock.accept();
                                PrintWriter writer = new PrintWriter(clientSock.getOutputStream());
                                clientOutputStreams.add(writer);
                               
                                // use a Runnable to start a 'second main method that will run
                                // the listener
                                Thread listener = new Thread(new ClientHandler(clientSock));
                                listener.start();
                                System.out.println("got a connection");
                        } // end while
                } // end try
                catch (Exception ex)
                {
                        System.out.println("error making a connection");
                } // end catch
               
        } // end go()
       
        public class ClientHandler implements Runnable
        {
                BufferedReader reader;
                Socket sock;
               
                public ClientHandler(Socket clientSocket)
                { // new inputStreamReader and then add it to a BufferedReader
                       
                        try
                        {
                                sock = clientSocket;
                                InputStreamReader isReader = new InputStreamReader(sock.getInputStream());
                                reader = new BufferedReader(isReader);
                        } // end try
                        catch (Exception ex)
                        {
                                System.out.println("error beginning StreamReader");
                        } // end catch
                       
                } // end ClientHandler()
               
                public void run()
                {
                        String message;
                       
                        try
                        {
                                while ((message = reader.readLine()) != null)
                                {
                                        System.out.println("read" + message);
                                        tellEveryone(message);
                                } // end while
                        } // end try
                        catch (Exception ex)
                        {
                                System.out.println("lost a connection");
                        } // end catch
                } // end run()
        } // end class ClientHandler
       
        public void tellEveryone(String message)
        { // sends message to everyone connected to server
                Iterator it = clientOutputStreams.iterator();
               
                while (it.hasNext())
                {
                        try
                        {
                                PrintWriter writer = (PrintWriter) it.next();
                                writer.println(message);
                                writer.flush();
                        } // end try
                        catch (Exception ex)
                        {
                                System.out.println("error telling everyone");
                        } // end catch
                } // end while
        } // end tellEveryone()
} // end class Server

aj.wh.ca Mar 25th, 2005 10:17 pm
Re: creating an instant messenger program in java
 
OK I see your code. But as far as I understand you have not implemented any user mechanism in your server. So basically any once with the client program can connect to your server. Now coming back to your requirement for "a feature that lets the user see who else is connected to the server" you must implement a user name based feature so that when the client sends a connection request to your server, it must authenticate using a unique id. Otherwise with current approach all you can tell your clients is which all IP Address are live at a particular moment.

stupidenator Mar 27th, 2005 2:25 am
Re: creating an instant messenger program in java
 
Thanks for the help. I think I understand it now!

nausherwan644 Jun 8th, 2005 10:53 am
Re: creating an instant messenger program in java
 
dear,
if you have made the messenger program in java. please send it to me i need it. i need it as soon as possible,thanks,
please e-mail it to me at my e-mail adress <email snipped>
i shall be very grateful to you for this.

server_crash Jun 8th, 2005 11:57 am
Re: creating an instant messenger program in java
 
please do NOT send it. Clearly they want it for homework.


All times are GMT -4. The time now is 2:11 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC