Java tutorial
/* Copyright Vance Anderson 2015 @author Vance Anderson @version 04/2015 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import java.net.*; import java.io.*; import java.util.*; import org.json.JSONObject; public class ChatServer extends Thread { private Socket conn; private int id; private final int FOUR_KB = 4096; private static StringBuilder log = new StringBuilder("Welcome to the server"); private static DeviceUpdater updater; public static final String HIST_CONSTANT = "14159"; private static ArrayList<String> users = new ArrayList<String>(); JSONObject userJSON; private String nick; public ChatServer(Socket sock, int id) { this.conn = sock; this.id = id; updater = new DeviceUpdater(); } public void run() { try { OutputStream out = conn.getOutputStream(); InputStream in = conn.getInputStream(); // updater.addDevice(out); System.out.println("made connection"); byte input[] = new byte[FOUR_KB]; int numBytesRead = in.read(input, 0, FOUR_KB); String messageReceived = new String(input, 0, numBytesRead); nick = messageReceived; users.add(nick); userJSON = new JSONObject(); System.out.println("Added user: " + nick); while (true) { // System.out.println("inside while(true)"); // rebuild the json in case a user has quit and subsequently // removed from the user arraylist userJSON.remove("users"); userJSON.put("users", users); numBytesRead = in.read(input, 0, FOUR_KB); if (numBytesRead == -1) { break; } messageReceived = new String(input, 0, numBytesRead); if (messageReceived.equals(HIST_CONSTANT)) { out.write(getLog().getBytes(), 0, getLog().length()); in.read(input, 0, FOUR_KB); out.write(userJSON.toString().getBytes(), 0, userJSON.toString().length()); } else { addToLog(nick + ": " + messageReceived); out.write(getLog().getBytes(), 0, getLog().length()); System.out.println(messageReceived + " From Id: " + id); // updater.write(getLog()); if (messageReceived.equals("eXIT")) break; } } users.remove(nick); System.out.println("removed user: " + nick); userJSON = new JSONObject(); userJSON.put("users", users); out.close(); in.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } public synchronized String getLog() { return log.toString(); } public synchronized void addToLog(String message) { log.append("\n" + message); } public static void main(String[] args) { // DeviceUpdater du = new DeviceUpdater(); int id = 0; String host = "localhost"; int port = 11001; if (args.length == 2) { host = args[0]; port = Integer.valueOf(args[1]); } Socket sock; try { ServerSocket server = new ServerSocket(port); while (true) { // System.out.println("Server waiting for connects on port " + port); sock = server.accept(); id++; ChatServer threadOfServer = new ChatServer(sock, id); threadOfServer.start(); } } catch (Exception e) { e.printStackTrace(); } } }