1. mud study Socket
package driver; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class GMain { public GMain() { // TODO Auto-generated constructor stub new ConnectionListener(); } public static void main(String[] arge){ new GMain(); } } class ConnectionListener extends Thread{ private ServerSocket serverSocket; public ConnectionListener() { try { serverSocket = new ServerSocket(8888); start(); } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } } @Override public void run() { try { while(true){ Socket socket = serverSocket.accept(); if(socket !=null){ new ConnectionBuilder(socket); } sleep(500); } } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class ConnectionBuilder extends Thread{ private Socket socket; private PrintWriter output; private BufferedReader input; public ConnectionBuilder(Socket s) { try { socket = s; output = new PrintWriter(socket.getOutputStream(),true); input = new BufferedReader(new InputStreamReader(socket.getInputStream())); } catch (IOException e) { // TODO: handle exception } start(); } @Override public void run() { // TODO Auto-generated method stub output.println("Welcome!!! HelloWorld!"); String userInput = ""; try { while(true){ userInput = input.readLine(); if(!"".equals(userInput)){ userInput = "broadcast : "+userInput; output.println(userInput); } sleep(500); } } catch (IOException e) { // TODO: handle exception } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
=================================================================
result :