public ChatServerThread(Socket socket, ChatServer ChatServer, int id) { // super("ChatServerThread"); this.socket = socket; // this.server = ChatServer; this.clientID = id; p.nl("Connection from: " + socket.toString()); }
public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(PORT); } catch (IOException e) { System.err.println("Could not listen on port: " + PORT + "."); System.exit(1); } while (true) { Socket clientSocket = null; try { clientSocket = serverSocket.accept(); startTime = System.currentTimeMillis(); } catch (IOException e) { System.err.println("Accept failed."); System.exit(1); } PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String inputLine, outputLine; while ((inputLine = in.readLine()) != null) { if (messageCount.get(clientSocket) == null) { messageCount.put(clientSocket, 1); } else { messageCount.put(clientSocket, messageCount.get(clientSocket) + 1); } overallMessageCount += 1; System.out.println("---------------------------------------"); System.out.println("Client: " + clientSocket.toString()); System.out.println( "aktive since " + ((System.currentTimeMillis() - startTime) / 1000) + " seconds"); System.out.println(new Date(System.currentTimeMillis())); System.out.println("Overall Message Count: " + overallMessageCount); System.out.println("Message Count from Client: " + messageCount.get(clientSocket)); System.out.println("Message " + inputLine); System.out.println("---------------------------------------"); outputLine = inputLine.toUpperCase(); if (outputLine.equals("END")) { out.println("abort"); break; } out.println(outputLine); } out.close(); in.close(); clientSocket.close(); System.out.println("All Closed"); } // serverSocket.close(); }
@Override public String toString() { return _socket.toString(); }
public String toString() { return mSocket.toString(); }
public FlickrFtpd(Socket incoming) { super(tg, null, incoming.toString()); // ~~ not a real good name.... this.incoming = incoming; }
public void run() { try { // Benutzt für die aktive Zeit vom Client startTime = System.currentTimeMillis(); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); // ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); String inputLine; String name = "", ip = ""; boolean loggedIn = false; while ((inputLine = in.readLine()) != null) { overallMessageCount += 1; messageCount += 1; System.out.println("---------------------------------------"); System.out.println("Client: " + socket.toString()); System.out.println( "aktive since " + ((System.currentTimeMillis() - startTime) / 1000) + " seconds"); System.out.println(new Date(System.currentTimeMillis())); System.out.println("Overall Message Count: " + overallMessageCount); System.out.println("Message Count from Client: " + messageCount); System.out.println("---------------------------------------"); if (inputLine.startsWith("NEW")) { try { if (!loggedIn) { userCount += 1; name = inputLine.split(" ")[1]; ip = socket.getInetAddress().toString().substring(1); if (userMap.containsKey(name)) { out.println("ERROR chatname already assigned"); break; } else { userMap.put(name, ip); } out.println("OK"); out.flush(); loggedIn = true; } else { out.println("ERROR user already logged in"); break; } } catch (RuntimeException e) { out.println("ERROR cannot Add User"); break; } } else if (inputLine.equals("INFO")) { try { String list; list = "LIST " + MyMultiServer.userCount; for (Entry<String, String> e : userMap.entrySet()) { list = list + " " + e.getValue() + " " + e.getKey(); } System.out.println(list); out.println(list); } catch (RuntimeException e) { out.println("ERROR Incorrect UserList"); break; } } else if (inputLine.equals("BYE")) { out.println("BYE"); userCount--; deleteUser(name, ip); loggedIn = false; break; } else { out.println("ERROR Incorrect Argument"); deleteUser(name, ip); userCount--; loggedIn = false; break; } } out.close(); in.close(); socket.close(); System.out.println("All Closed"); } catch (IOException e) { System.out.println("Something went wrong!"); // e.printStackTrace(); } }
/** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // nextMonitor = new Object(); monitor = new Object(); try { myIP = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } ss = new ServerSocket(serverPort); if (args.length == 0) { next = new Socket(myIP, serverPort); } else if (args.length == 1) { next = new Socket(InetAddress.getByName(args[0]), serverPort); } else { System.out.println("Usage: peer [ip address]"); System.exit(0); } prev = ss.accept(); nextIn = new BufferedReader(new InputStreamReader(next.getInputStream())); nextOut = new PrintStream(next.getOutputStream(), true); prevIn = new BufferedReader(new InputStreamReader(prev.getInputStream())); prevOut = new PrintStream(prev.getOutputStream(), true); nextInput = new Thread(new NextInput()); nextInput.start(); nextOutput = new Thread(new NextOutput()); nextOutput.start(); prevOutput = new Thread(new PrevOutput()); prevOutput.start(); connectionHandler = new Thread(new ConnectionHandler()); connectionHandler.start(); prevInput = new Thread(new PrevInput()); prevInput.start(); BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); String userInput; try { mainloop: while (!quit) { if ((userInput = stdIn.readLine()) != null) { if (userInput.toLowerCase().equals("quit")) { quit = true; break mainloop; } else if (Peer.debug && userInput.toLowerCase().equals("debug")) { System.out.println("PREV: " + prev.toString()); System.out.println("NEXT: " + next.toString()); System.out.println("SocketQueue empty? " + socketQueue.isEmpty()); System.out.println("ChatQueue empty? " + chatQueue.isEmpty()); System.out.println("reconnectQueue empty? " + reconnectQueue.isEmpty()); System.out.println("Hold = " + hold + ", prevDone = " + prevDone + ", quit = " + quit); } else if (Peer.debug && userInput.toLowerCase().equals("test")) { Peer.prevOut.println("test"); } else { Peer.chatQueue.add(Peer.myIP + ": " + userInput); } } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } while (!socketQueue.isEmpty()) { Socket s = socketQueue.remove(); PrevOutput.sendReconnect(s); } try { prevOut.println("Hold"); Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } while (!chatQueue.isEmpty()) { nextOut.println(chatQueue.remove()); } prevOut.println(next.getInetAddress().getHostAddress()); prevOut.close(); prevIn.close(); prev.close(); nextOut.close(); nextIn.close(); next.close(); ss.close(); }