Simple Chat Server

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2009
Posts: 36
Reputation: smoore is an unknown quantity at this point 
Solved Threads: 1
smoore's Avatar
smoore smoore is offline Offline
Light Poster

Simple Chat Server

 
0
  #1
Jun 12th, 2009
Okay so I found some code online to create a simple chat server and I was trying to teach myself with it. I have a few questions though.

First off here is the code:

ChatServer.java
  1. import java.io.IOException;
  2. import java.net.InetAddress;
  3. import java.net.Socket;
  4. import java.net.ServerSocket;
  5. import java.net.UnknownHostException;
  6. import java.util.Scanner;
  7.  
  8.  
  9. public class ChatServer
  10. {
  11. // set default port value here
  12. public static final int DEFAULT_PORT = 9800;
  13. //user can change port with optional command line argument
  14.  
  15. public static void main (String[] args) throws UnknownHostException
  16. {
  17. // variables
  18. int port = DEFAULT_PORT;
  19. ServerSocket serverSocket = null;
  20. Socket socket = null;
  21. Scanner keyboard = new Scanner(System.in);
  22.  
  23. System.out.print("Enter a port (enter to use default): ");
  24. String port_s = keyboard.nextLine();
  25.  
  26. try
  27. {
  28. // change the port number from the default one
  29. if(!port_s.equals(""))
  30. port = Integer.parseInt(port_s);
  31.  
  32. InetAddress thisIp = InetAddress.getLocalHost();
  33. System.out.println("\nServer info: \n");
  34. System.out.println("IP: "+thisIp.getHostAddress());
  35. System.out.println("Port: " + port);
  36.  
  37. }
  38. catch(NumberFormatException nfe)// if the user entered something other than a number
  39. {
  40. System.err.println("Enter a valid number for port!");
  41. System.exit(0);
  42. }
  43.  
  44. try
  45. {
  46. serverSocket = new ServerSocket(port);
  47.  
  48. while(true)
  49. {
  50. socket = serverSocket.accept();
  51. ChatHandler handler = new ChatHandler(socket);
  52. handler.start();
  53. }
  54. }
  55. catch(IOException ioe)
  56. {
  57. ioe.printStackTrace();
  58. }
  59. finally
  60. {
  61. try{serverSocket.close();}
  62. catch(IOException ioe){ioe.printStackTrace();}
  63. }
  64. }
  65. }

ChatHandler.java:
  1. import java.io.IOException;
  2. import java.net.InetAddress;
  3. import java.net.Socket;
  4. import java.net.ServerSocket;
  5. import java.net.UnknownHostException;
  6. import java.util.Scanner;
  7.  
  8.  
  9. public class ChatServer
  10. {
  11. // set default port value here
  12. public static final int DEFAULT_PORT = 9800;
  13. //user can change port with optional command line argument
  14.  
  15. public static void main (String[] args) throws UnknownHostException
  16. {
  17. // variables
  18. int port = DEFAULT_PORT;
  19. ServerSocket serverSocket = null;
  20. Socket socket = null;
  21. Scanner keyboard = new Scanner(System.in);
  22.  
  23. System.out.print("Enter a port (enter to use default): ");
  24. String port_s = keyboard.nextLine();
  25.  
  26. try
  27. {
  28. // change the port number from the default one
  29. if(!port_s.equals(""))
  30. port = Integer.parseInt(port_s);
  31.  
  32. InetAddress thisIp = InetAddress.getLocalHost();
  33. System.out.println("\nServer info: \n");
  34. System.out.println("IP: "+thisIp.getHostAddress());
  35. System.out.println("Port: " + port);
  36.  
  37. }
  38. catch(NumberFormatException nfe)// if the user entered something other than a number
  39. {
  40. System.err.println("Enter a valid number for port!");
  41. System.exit(0);
  42. }
  43.  
  44. try
  45. {
  46. serverSocket = new ServerSocket(port);
  47.  
  48. while(true)
  49. {
  50. socket = serverSocket.accept();
  51. ChatHandler handler = new ChatHandler(socket);
  52. handler.start();
  53. }
  54. }
  55. catch(IOException ioe)
  56. {
  57. ioe.printStackTrace();
  58. }
  59. finally
  60. {
  61. try{serverSocket.close();}
  62. catch(IOException ioe){ioe.printStackTrace();}
  63. }
  64. }
  65. }

My Questions:
1) How would I print something once a person enters the chat (telnets domain [port])
2) I would like to use this to ask each person as they enter for a username they would like to use
3) Is that ip address printing correctly for other people? I think it is merely printing my local ipaddress that is associated with the router (not too familiar with ip addresses and stuff like that)
4) More general question: What is the difference between a vector and ArrayList?

Thanks a lot guys.
Last edited by smoore; Jun 12th, 2009 at 11:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,013
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 150
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: Simple Chat Server

 
0
  #2
Jun 13th, 2009
4. Only real difference is that Vector is synchronized, ie it's automatically safe to use in a multi-threaded app. ArrayList isn't.
ps: You may not think you have a multi-threaded app, but if you use Swing, then you do.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 20
Reputation: stewie griffin is an unknown quantity at this point 
Solved Threads: 0
stewie griffin stewie griffin is offline Offline
Newbie Poster

Re: Simple Chat Server

 
0
  #3
Jun 13th, 2009
Well, I don’t see the ChatHandler class , but it should have a run method , in the run method (assuming you work with sockets) you shall ask for username, password exec,and use InputStreamReader and / or PrintWriter,
For exemple:
  1. requestOut = new PrintWriter(socket.getOutputStream(), true);
and than
  1. requestOut.println(userInput);

the server / client must be coordinated

in every ChatHandler add some variable that will contain the nickname
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 36
Reputation: smoore is an unknown quantity at this point 
Solved Threads: 1
smoore's Avatar
smoore smoore is offline Offline
Light Poster

Re: Simple Chat Server

 
0
  #4
Jun 16th, 2009
Originally Posted by stewie griffin View Post
Well, I don’t see the ChatHandler class ....
Here it is again but it is also in the first post:

  1. import java.io.IOException;
  2. import java.net.InetAddress;
  3. import java.net.Socket;
  4. import java.net.ServerSocket;
  5. import java.net.UnknownHostException;
  6. import java.util.Scanner;
  7.  
  8.  
  9. public class ChatServer
  10. {
  11. // set default port value here
  12. public static final int DEFAULT_PORT = 9800;
  13. //user can change port with optional command line argument
  14.  
  15. public static void main (String[] args) throws UnknownHostException
  16. {
  17. // variables
  18. int port = DEFAULT_PORT;
  19. ServerSocket serverSocket = null;
  20. Socket socket = null;
  21. Scanner keyboard = new Scanner(System.in);
  22.  
  23. System.out.print("Enter a port (enter to use default): ");
  24. String port_s = keyboard.nextLine();
  25.  
  26. try
  27. {
  28. // change the port number from the default one
  29. if(!port_s.equals(""))
  30. port = Integer.parseInt(port_s);
  31.  
  32. InetAddress thisIp = InetAddress.getLocalHost();
  33. System.out.println("\nServer info: \n");
  34. System.out.println("IP: "+thisIp.getHostAddress());
  35. System.out.println("Port: " + port);
  36.  
  37. }
  38. catch(NumberFormatException nfe)// if the user entered something other than a number
  39. {
  40. System.err.println("Enter a valid number for port!");
  41. System.exit(0);
  42. }
  43.  
  44. try
  45. {
  46. serverSocket = new ServerSocket(port);
  47.  
  48. while(true)
  49. {
  50. socket = serverSocket.accept();
  51. ChatHandler handler = new ChatHandler(socket);
  52. handler.start();
  53. }
  54. }
  55. catch(IOException ioe)
  56. {
  57. ioe.printStackTrace();
  58. }
  59. finally
  60. {
  61. try{serverSocket.close();}
  62. catch(IOException ioe){ioe.printStackTrace();}
  63. }
  64. }
  65. }
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC