943,729 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1382
  • Java RSS
Jun 12th, 2009
0

Simple Chat Server

Expand Post »
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
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 39
Solved Threads: 1
Light Poster
smoore is offline Offline
36 posts
since Feb 2009
Jun 13th, 2009
0

Re: Simple Chat Server

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.
Featured Poster
Reputation Points: 1907
Solved Threads: 950
Posting Expert
JamesCherrill is offline Offline
5,768 posts
since Apr 2008
Jun 13th, 2009
0

Re: Simple Chat Server

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:
Java Syntax (Toggle Plain Text)
  1. requestOut = new PrintWriter(socket.getOutputStream(), true);
and than
Java Syntax (Toggle Plain Text)
  1. requestOut.println(userInput);

the server / client must be coordinated

in every ChatHandler add some variable that will contain the nickname
Reputation Points: 64
Solved Threads: 1
Light Poster
stewie griffin is offline Offline
35 posts
since Jul 2008
Jun 16th, 2009
0

Re: Simple Chat Server

Well, I don’t see the ChatHandler class ....
Here it is again but it is also in the first post:

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 39
Solved Threads: 1
Light Poster
smoore is offline Offline
36 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: why is this simple code using 100% CPU?
Next Thread in Java Forum Timeline: exe's within a jar





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC