Skip to content

Instantly share code, notes, and snippets.

@MarcusTalbotNL
Created April 21, 2017 21:34
Show Gist options
  • Save MarcusTalbotNL/8d86025b8349926212da8aeba3df86fa to your computer and use it in GitHub Desktop.
Save MarcusTalbotNL/8d86025b8349926212da8aeba3df86fa to your computer and use it in GitHub Desktop.
Basic setup to get chat-server up and running
/*
* Hey there @aeren108, I saw your question on Reddit, so I thought this might be the easiest way to help you out a bit.
* I promise this won't be a complete solution, but will only give you a push in the right direction!
*/
public class ChatServer { //First, we will need a ServerSocket to listen for connecting Sockets.
private static final int DEFAULT_PORT = 1337; // In Java, the basic way to create a constant (without the
// use of enumerables) is to use the keywords private and static.
private static boolean isRunning = true; // We'll use this boolean to set the running state of the server.
private ServerSocket server; // Note that we use the private-keyword here. You should always make sure
// that as little classes as possible can reach data from other classes.
public ChatServer() {
this.server = new ServerSocket(DEFAULT_PORT); // Here we initialise the server using the default port.
}
public ChatServer(int port) {
this.server = new ServerSocket(port); // However, we can also initialise it using a manually set port.
}
// Now for some basic logic:
private void acceptConnections() throws IOException {
Socket socketBuffer = null;
while(ChatServer.isRunning) { // Here we create the loop the loop, that will accept connections, untill further notice.
socketBuffer = this.server.accept();
ChatUtilities.CLIENT_LIST.add(new ClientThread(socketBuffer));
socketBuffer = null;
}
}
public abstract class ChatUtilities {
// This ArrayList will hold the created clients (we'll get to that in a bit) and their Sockets.
public static final ArrayList<ClientThread> CLIENT_LIST = new ArrayList<ClientThread>();
public static void sendMessage(String message) throws IOException {
ChatUtilities.CLIENT_LIST.forEach(client -> () {
client.sendMessage(message);
});
}
}
public class ClientThread {
private final Socket clientSocket;
private final BufferedWriter writer;
private final BufferedReader reader;
public ClientThread(Socket clientSocket) throws IOException {
this.clientSocket = clientSocket;
this.writer = new BufferedWriter(new OutputStreamWriter(this.clientSocket.getOutputStream()));
this.reader = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
new Thread(() -> {
String message = "";
while((message = reader.readLine()).isEmpty) {
Thread.sleep(500);
}
ChatUtilities.sendMessage(message);
message = "";
});
}
public void sendMessage(String message) {
// Here you just send the message using the BufferedReader.
}
}
@MarcusTalbotNL
Copy link
Author

@aeren108 , this might help you get setup! It is a very, very rudimentary implementation of a server, and requires many tweaks to get it right, but it may be what you need regarding the problem of the clients not getting the messages.

@aeren108
Copy link

aeren108 commented Apr 22, 2017

Thanks a lot.You helped me so much but I didn't understand the these lines : // This ArrayList will hold the created clients (we'll get to that in a bit) and their Sockets. public static final ArrayList<ClientThread> CLIENT_LIST = new ArrayList<ClientThread>();
How do we get the list of clients, isn't it null ?
I'm thankful to you, you are the best mentor I've ever seen.

Edit: Okay I understood it.In ChatServer class we are adding the them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment