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

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:

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.

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.

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:

requestOut = new PrintWriter(socket.getOutputStream(), true);

and than

requestOut.println(userInput);

the server / client must be coordinated

in every ChatHandler add some variable that will contain the nickname

Well, I don’t see the ChatHandler class ....

Here it is again but it is also in the first post:

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();}
		}
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.