Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- public class MultiEchoServer {
- public static void main(String[] args) {
- try {
- ServerSocket s = new ServerSocket(4444);
- while (true) {
- Socket incoming = s.accept();
- new ClientHandler(incoming).start();
- }
- } catch (Exception e) {}
- }
- }
- class ClientHandler extends Thread {
- protected Socket incoming;
- public ClientHandler(Socket incoming) {
- this.incoming = incoming;
- }
- public void run() {
- try {
- BufferedReader in
- = new BufferedReader(
- new InputStreamReader(
- incoming.getInputStream()));
- PrintWriter out
- = new PrintWriter(
- new OutputStreamWriter(
- incoming.getOutputStream()));
- out.println("Hello! ...");
- out.println("Enter BYE to exit.");
- out.flush();
- while (true) {
- String str = in.readLine();
- if (str == null) {
- break;
- } else {
- out.println("Echo: " + str);
- out.flush();
- if (str.trim().equals("BYE"))
- break;
- }
- }
- incoming.close();
- } catch (IOException e) {}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment