| | |
Simple Chat Server
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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
ChatHandler.java:
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.
First off here is the code:
ChatServer.java
Java Syntax (Toggle Plain Text)
import java.io.IOException; import java.net.InetAddress; import java.net.Socket; import java.net.ServerSocket; import java.net.UnknownHostException; import java.util.Scanner; public class ChatServer { // set default port value here public static final int DEFAULT_PORT = 9800; //user can change port with optional command line argument public static void main (String[] args) throws UnknownHostException { // variables int port = DEFAULT_PORT; ServerSocket serverSocket = null; Socket socket = null; Scanner keyboard = new Scanner(System.in); System.out.print("Enter a port (enter to use default): "); String port_s = keyboard.nextLine(); try { // change the port number from the default one if(!port_s.equals("")) port = Integer.parseInt(port_s); InetAddress thisIp = InetAddress.getLocalHost(); System.out.println("\nServer info: \n"); System.out.println("IP: "+thisIp.getHostAddress()); System.out.println("Port: " + port); } catch(NumberFormatException nfe)// if the user entered something other than a number { System.err.println("Enter a valid number for port!"); System.exit(0); } try { serverSocket = new ServerSocket(port); while(true) { socket = serverSocket.accept(); ChatHandler handler = new ChatHandler(socket); handler.start(); } } catch(IOException ioe) { ioe.printStackTrace(); } finally { try{serverSocket.close();} catch(IOException ioe){ioe.printStackTrace();} } } }
ChatHandler.java:
Java Syntax (Toggle Plain Text)
import java.io.IOException; import java.net.InetAddress; import java.net.Socket; import java.net.ServerSocket; import java.net.UnknownHostException; import java.util.Scanner; public class ChatServer { // set default port value here public static final int DEFAULT_PORT = 9800; //user can change port with optional command line argument public static void main (String[] args) throws UnknownHostException { // variables int port = DEFAULT_PORT; ServerSocket serverSocket = null; Socket socket = null; Scanner keyboard = new Scanner(System.in); System.out.print("Enter a port (enter to use default): "); String port_s = keyboard.nextLine(); try { // change the port number from the default one if(!port_s.equals("")) port = Integer.parseInt(port_s); InetAddress thisIp = InetAddress.getLocalHost(); System.out.println("\nServer info: \n"); System.out.println("IP: "+thisIp.getHostAddress()); System.out.println("Port: " + port); } catch(NumberFormatException nfe)// if the user entered something other than a number { System.err.println("Enter a valid number for port!"); System.exit(0); } try { serverSocket = new ServerSocket(port); while(true) { socket = serverSocket.accept(); ChatHandler handler = new ChatHandler(socket); handler.start(); } } catch(IOException ioe) { ioe.printStackTrace(); } finally { try{serverSocket.close();} catch(IOException ioe){ioe.printStackTrace();} } } }
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.
•
•
Join Date: Jul 2008
Posts: 18
Reputation:
Solved Threads: 0
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:
and than
the server / client must be coordinated
in every ChatHandler add some variable that will contain the nickname
For exemple:
Java Syntax (Toggle Plain Text)
requestOut = new PrintWriter(socket.getOutputStream(), true);
Java Syntax (Toggle Plain Text)
requestOut.println(userInput);
the server / client must be coordinated
in every ChatHandler add some variable that will contain the nickname
Here it is again but it is also in the first post:
Java Syntax (Toggle Plain Text)
import java.io.IOException; import java.net.InetAddress; import java.net.Socket; import java.net.ServerSocket; import java.net.UnknownHostException; import java.util.Scanner; public class ChatServer { // set default port value here public static final int DEFAULT_PORT = 9800; //user can change port with optional command line argument public static void main (String[] args) throws UnknownHostException { // variables int port = DEFAULT_PORT; ServerSocket serverSocket = null; Socket socket = null; Scanner keyboard = new Scanner(System.in); System.out.print("Enter a port (enter to use default): "); String port_s = keyboard.nextLine(); try { // change the port number from the default one if(!port_s.equals("")) port = Integer.parseInt(port_s); InetAddress thisIp = InetAddress.getLocalHost(); System.out.println("\nServer info: \n"); System.out.println("IP: "+thisIp.getHostAddress()); System.out.println("Port: " + port); } catch(NumberFormatException nfe)// if the user entered something other than a number { System.err.println("Enter a valid number for port!"); System.exit(0); } try { serverSocket = new ServerSocket(port); while(true) { socket = serverSocket.accept(); ChatHandler handler = new ChatHandler(socket); handler.start(); } } catch(IOException ioe) { ioe.printStackTrace(); } finally { try{serverSocket.close();} catch(IOException ioe){ioe.printStackTrace();} } } }
![]() |
Similar Threads
- simple chat socket server form disappearing (C#)
- Python Chat Server (Python)
- help for Server Client Porgram (chat Server) (Visual Basic 4 / 5 / 6)
- gui based chat server (Python)
- need help for java chat application (Java)
Other Threads in the Java Forum
- Previous Thread: why is this simple code using 100% CPU?
- Next Thread: exe's within a jar
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation binary blackberry block bluetooth character chat class classes client code component consumer database desktop developmenthelp draw eclipse error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaee javaprojects jmf jni jpanel julia lego linked linux list loop loops mac map method methods mobile netbeans newbie notdisplaying number online oracle page print printf problem program programming project properties recursion researchinmotion rotatetext rsa scanner screen server set singleton size sms sort sql string swing template textfields threads time title tree tutorial-sample update windows working





