I successfully developed an app which you can create/connect to a server. I have a class named Person in which I have a public static LinkedList. In this list I’m storing clients who are connected to the server. The server/client classes are in the same package. The server and the client classes are extending Threads (they are working in the background).
What I’m trying to do.
- I want to send the online client list (LinkedList) to all clients who are connected to the server.
- I want to read data (String from StyledText) from clients and write it to the global main chat area (StyledText).
(something like mIRC program).
I have read and searched all day long, but I never saw a good explanation, example or documentation about “Java chat app”. I found with AWT, Swing, but the problem is I have knowledge only in SWT. Also, I found tutorials with TELNET, and the Input and Output Stream was read and wrote in the console (CMD). Am I need to use InputStreamReader, BufferedStreamReader and PrintWriter? If I have, am I need to convert those data in byte array then convert them into Objects?
I post some screenshots for a better understanding:


This is how I create the server:
DialogServer dialog = new DialogServer(new Shell());
dialog.create();
if(dialog.open() == Window.OK){
new Server(serverAddress).start(); //dialog sets serverAddress as a String IP
}
Server class:
public class Server extends Thread{
private static ServerSocket serverSocket;
private static Socket socket;
private static String address;
public Server(String address){
this.address = address;
}
public void run(){
try {
serverSocket = new ServerSocket(5000, 10, InetAddress.getByName(address));
View.serverUp = true;
View.log("Server is up. Listening on " + address + ":5000");
Person p = new Person(View.nickname, true, 100.0);
Person.listPeople.add(p); //add ADMIN to static LinkedList
while(true){
socket = serverSocket.accept();
new Client(socket).start(); // client class thread
refresOnlineList(); // refresh UI table
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void refresOnlineList(){
View.display.syncExec(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
View.tableViewer.refresh();
// TableViewer which contains online client list
}
});
}
}
Dialog to connect to the server:
DialogConnect dialog = new DialogConnect(new Shell());
dialog.create();
if(dialog.open() == Window.OK){
new Client(connectAddress, connectNickname).start(); // dialog sets String parameters. new Client start a new Thread for a client.
}
Client class:
public class Client extends Thread{
private static Socket socket;
private static String nickname;
public Client(Socket socket){
this.socket = socket;
}
public Client(String address, String nickname){
socket = new Socket();
try {
socket.connect(new InetSocketAddress(address, 5000), 10);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
View.log("Cannot connect to server.");
}
this.nickname = nickname;
}
public void run(){
try {
Person p = new Person(nickname, false, 15.4);
Person.listPeople.add(p); // add PERSON to static LinkedList
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Developed in Eclipse RCP 3.8
Your goal can be divided into three tasks:
Stack Overflow is, AFAIK, not aimed to guide through whole project.
You need to state specific question regarding specific problem.
I’d suggest you to start with a console applications (client and server) to get rid of the burden of creating UI. When you’re done with that, you can create some UI and bind the events into it.