I am making a TCP chat server from a simple tutorial I found and I’m kind of new to java. I will eventually make a client class but I’ve been testing it through telnet so far. I have a couple of problems. I have setup the server to take commands from the client. For example “EXIT” which closes the client connection, and “Username” which prints “OK”. Shown below:
USER 1: TIM
Welcome to Bob's Chat Server!
Please Enter a User Name:
Tim
Welcome Tim we hope you enjoy your chat today
Bob:Hello
Hi Bob
Tim
OK
EXIT
Closing Connection . . . Goodbye
USER 2: BOB
Welcome to Bob's Chat Server!
Please Enter a User Name:
Bob
Welcome Bob we hope you enjoy your chat today
Hello
Tim:Hi Bob
Tim:Tim
I have three problems that I want to address:
-
You’ll notice that when USER 1 typed “Tim” (his username) it said “OK” like I wanted, but it also printed “Tim” to USER 2. Is there a way to make it not send my typed commands across? So when I type “Tim” it doesn’t print “Tim” to USER 2?
-
When messages are sent to other users it displays who said it. Is there a way to print the name on both connections? For example when USER 2 says “Hello” it looks more like “Bob: Hello” on both connections?
-
Is there a way to keep track of everything that’s said in the entire chat session and print off the entire contents of the chat to whatever user requested it?
Here is my server code:
// Chat Server runs at port no. 9020
import java.io.*;
import java.util.*;
import java.net.*;
import static java.lang.System.out;
public class TCPServer
{
Vector<String> users = new Vector<String>();
Vector<HandleClient> clients = new Vector<HandleClient>();
int PORT = 9020;
int NumClients = 10;
public void process() throws Exception
{
ServerSocket server = new ServerSocket(PORT,NumClients);
out.println("Server Connected...");
while( true)
{
Socket client = server.accept();
HandleClient c = new HandleClient(client);
clients.add(c);
} // end of while
}
public static void main(String ... args) throws Exception
{
new TCPServer().process();
} // end of main
public void boradcast(String user, String message)
{
// send message to all connected users
for (HandleClient c : clients)
if (!c.getUserName().equals(user))
{
c.sendMessage(user,message);
}
}
class HandleClient extends Thread
{
String name = "";
BufferedReader input;
PrintWriter output;
public HandleClient(Socket client) throws Exception
{
// get input and output streams
input = new BufferedReader(new InputStreamReader(client.getInputStream())) ;
output = new PrintWriter (client.getOutputStream(),true);
output.println("Welcome to Bob's Chat Server!\n");
// read name
output.println("Please Enter a User Name: ");
name = input.readLine();
users.add(name); // add to vector
output.println("Welcome "+name+" we hope you enjoy your chat today");
start();
}
public void sendMessage(String uname,String msg)
{
output.println( uname + ":" + msg);
}
public String getUserName()
{
return name;
}
public void run()
{
String line;
try
{
while(true)
{
line = input.readLine();
if("EXIT".equals(line))
{
output.println("Closing Connection . . . Goodbye");
clients.remove(this);
users.remove(name);
break;
}
else if(name.equals(line))
{
output.println("OK");
}
boradcast(name,line); // method of outer class - send messages to all
}// end of while
} // try
catch(Exception e)
{
System.out.println(e.getMessage());
}
} // end of run()
} // end of inner class
} // end of Server
Any help is appreciated. Thanks.
I had modified your code as per your liking , do have a look :
For Point 1:
For Point 2 :
Seems to me in this you are typing to the console to get the input, in that case atleast for once you have to type what you have to send to the other side, then you can add your modification to this as shown in the first line of the method, broadcast();, so that you can satisfy your need for Point 2, or (else You can let the server side send it back to the client, the message which containts the name, it will be like a client send his message to the server and the server is sending all the clients back.)
And for Point 3, you can write your conversation to the file or save it to the database, which ever way is appropriate to you.
And since you said you are new to Java, you better look at java.nio. This package is better than the previous java.io package.
LATEST EDIT :
Try this small sample code, hope this might do what you asking for :
Hope this might help you in some way.
Regards