| | |
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: 20
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 |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component database desktop draw ebook eclipse encode equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number online open-source oracle parameter print problem program programming project properties recursion reference replaysolutions rotatetext scanner score screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows working xstream






