Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class Client implements Runnable
- {
- static Collection<Socket> users;
- Socket clientSocket = null;
- BufferedReader netInput;
- PrintWriter netOutput;
- public Client(Socket ClientSocket)
- {
- clientSocket = ClientSocket;
- }
- public void run()
- {
- try
- {
- netInput = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
- System.out.println("Input stream initialized.");
- netOutput = new PrintWriter(clientSocket.getOutputStream());
- System.out.println("Output stream initialized.");
- netOutput.println("Welcome to the Nexo Relay Server!\nType things and have them echoed back at you.");
- netOutput.flush();
- users.add(clientSocket);
- String cData = "";
- while((cData = netInput.readLine()) != null)
- {
- System.out.println(cData);
- //netOutput.println("ECHO: " + cData);
- sendToAll(cData);
- netOutput.flush();
- }
- users.remove(clientSocket);
- System.out.println("Client disconnected.");
- }
- catch(Exception e){e.printStackTrace();}
- }
- public static void sendToAll(String message) throws IOException
- {
- Iterator<Socket> clientIterator = users.iterator();
- PrintWriter tempWriter;
- Socket tempSocket;
- while(clientIterator.hasNext())
- {
- tempSocket = clientIterator.next();
- tempWriter = new PrintWriter(tempSocket.getOutputStream());
- tempWriter.println("DATA FROM " + tempSocket.getRemoteSocketAddress() + ": " + message);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement