creating an instant messenger program in java

Closed Thread

Join Date: Mar 2005
Posts: 192
Reputation: stupidenator is an unknown quantity at this point 
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

creating an instant messenger program in java

 
0
  #1
Mar 24th, 2005
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?
Quick reply to this message  
Join Date: Mar 2005
Posts: 53
Reputation: aj.wh.ca is an unknown quantity at this point 
Solved Threads: 1
aj.wh.ca aj.wh.ca is offline Offline
Junior Poster in Training

Re: creating an instant messenger program in java

 
0
  #2
Mar 24th, 2005
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
Quick reply to this message  
Join Date: Mar 2005
Posts: 192
Reputation: stupidenator is an unknown quantity at this point 
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Re: creating an instant messenger program in java

 
0
  #3
Mar 24th, 2005
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.
Quick reply to this message  
Join Date: Mar 2005
Posts: 53
Reputation: aj.wh.ca is an unknown quantity at this point 
Solved Threads: 1
aj.wh.ca aj.wh.ca is offline Offline
Junior Poster in Training

Re: creating an instant messenger program in java

 
0
  #4
Mar 24th, 2005
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 ?
Quick reply to this message  
Join Date: Mar 2005
Posts: 192
Reputation: stupidenator is an unknown quantity at this point 
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Re: creating an instant messenger program in java

 
0
  #5
Mar 25th, 2005
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.
Quick reply to this message  
Join Date: Mar 2005
Posts: 192
Reputation: stupidenator is an unknown quantity at this point 
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Re: creating an instant messenger program in java

 
0
  #6
Mar 25th, 2005
Here is the code for my server program.

  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.*;
  4.  
  5. public class Server
  6. {
  7. ArrayList clientOutputStreams;
  8.  
  9. public void go()
  10. {
  11. clientOutputStreams = new ArrayList();
  12.  
  13. try
  14. {
  15. ServerSocket serverSock = new ServerSocket(4999);
  16.  
  17. while (true)
  18. { // set up the server writer function and then begin at the same
  19. // the listener using the Runnable and Thread
  20. Socket clientSock = serverSock.accept();
  21. PrintWriter writer = new PrintWriter(clientSock.getOutputStream());
  22. clientOutputStreams.add(writer);
  23.  
  24. // use a Runnable to start a 'second main method that will run
  25. // the listener
  26. Thread listener = new Thread(new ClientHandler(clientSock));
  27. listener.start();
  28. System.out.println("got a connection");
  29. } // end while
  30. } // end try
  31. catch (Exception ex)
  32. {
  33. System.out.println("error making a connection");
  34. } // end catch
  35.  
  36. } // end go()
  37.  
  38. public class ClientHandler implements Runnable
  39. {
  40. BufferedReader reader;
  41. Socket sock;
  42.  
  43. public ClientHandler(Socket clientSocket)
  44. { // new inputStreamReader and then add it to a BufferedReader
  45.  
  46. try
  47. {
  48. sock = clientSocket;
  49. InputStreamReader isReader = new InputStreamReader(sock.getInputStream());
  50. reader = new BufferedReader(isReader);
  51. } // end try
  52. catch (Exception ex)
  53. {
  54. System.out.println("error beginning StreamReader");
  55. } // end catch
  56.  
  57. } // end ClientHandler()
  58.  
  59. public void run()
  60. {
  61. String message;
  62.  
  63. try
  64. {
  65. while ((message = reader.readLine()) != null)
  66. {
  67. System.out.println("read" + message);
  68. tellEveryone(message);
  69. } // end while
  70. } // end try
  71. catch (Exception ex)
  72. {
  73. System.out.println("lost a connection");
  74. } // end catch
  75. } // end run()
  76. } // end class ClientHandler
  77.  
  78. public void tellEveryone(String message)
  79. { // sends message to everyone connected to server
  80. Iterator it = clientOutputStreams.iterator();
  81.  
  82. while (it.hasNext())
  83. {
  84. try
  85. {
  86. PrintWriter writer = (PrintWriter) it.next();
  87. writer.println(message);
  88. writer.flush();
  89. } // end try
  90. catch (Exception ex)
  91. {
  92. System.out.println("error telling everyone");
  93. } // end catch
  94. } // end while
  95. } // end tellEveryone()
  96. } // end class Server
Quick reply to this message  
Join Date: Mar 2005
Posts: 53
Reputation: aj.wh.ca is an unknown quantity at this point 
Solved Threads: 1
aj.wh.ca aj.wh.ca is offline Offline
Junior Poster in Training

Re: creating an instant messenger program in java

 
0
  #7
Mar 25th, 2005
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.
Quick reply to this message  
Join Date: Mar 2005
Posts: 192
Reputation: stupidenator is an unknown quantity at this point 
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Re: creating an instant messenger program in java

 
0
  #8
Mar 27th, 2005
Thanks for the help. I think I understand it now!
Quick reply to this message  
Join Date: May 2005
Posts: 2
Reputation: nausherwan644 is an unknown quantity at this point 
Solved Threads: 0
nausherwan644 nausherwan644 is offline Offline
Light Poster

Re: creating an instant messenger program in java

 
0
  #9
Jun 8th, 2005
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.
Last edited by Ancient Dragon; Feb 1st, 2007 at 9:57 pm. Reason: email snipped
Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: creating an instant messenger program in java

 
0
  #10
Jun 8th, 2005
please do NOT send it. Clearly they want it for homework.
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC