Guest User

Untitled

a guest
Aug 19th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. public class Server {
  2. int port;
  3. ServerSocket server=null;
  4. Socket socket=null;
  5. ExecutorService exec = null;
  6. ArrayList clients = new ArrayList();
  7. DataOutputStream dos=null;
  8.  
  9. public static void main(String[] args) throws IOException {
  10. Server serverobj=new Server(2000);
  11. serverobj.startServer();
  12. }
  13.  
  14. Server(int port){
  15. this.port=port;
  16. exec = Executors.newFixedThreadPool(3);
  17. }
  18.  
  19. public void startServer() throws IOException {
  20.  
  21. server=new ServerSocket(2000);
  22. System.out.println("Server running");
  23.  
  24. while(true){
  25.  
  26. socket=server.accept();
  27. dos = new DataOutputStream(socket.getOutputStream());
  28. clients.add(dos);
  29. ServerThread runnable= new ServerThread(socket,new ArrayList<>(clients),this);
  30. exec.execute(runnable);
  31. }
  32.  
  33. }
  34.  
  35. private static class ServerThread implements Runnable {
  36.  
  37. Server server=null;
  38. Socket socket=null;
  39. BufferedReader brin;
  40. Iterator it=null;
  41. Scanner sc=new Scanner(System.in);
  42. String str;
  43.  
  44. ServerThread(Socket socket, ArrayList clients ,Server server ) throws IOException {
  45.  
  46. this.socket=socket;
  47. this.server=server;
  48. System.out.println("Connection successful with "+socket);
  49.  
  50. brin=new BufferedReader(new InputStreamReader(socket.getInputStream()));
  51. it = clients.iterator();
  52.  
  53. }
  54.  
  55. @Override
  56. public void run() {
  57.  
  58. try{
  59. while ((str = brin.readLine()) != null) {
  60. while (it.hasNext()) {
  61. try{
  62.  
  63. DataOutputStream dost=(DataOutputStream) it.next();
  64. dost.writeChars(str);
  65. dost.flush();
  66.  
  67. }
  68. catch(IOException ex){
  69. System.out.println("Error 1 "+ex);
  70. }
  71. }
  72. }
  73.  
  74. brin.close();
  75. socket.close();
  76.  
  77. }
  78. catch(IOException ex){
  79. System.out.println("Error 2 "+ex);
  80. }
  81.  
  82. }
  83. }
  84.  
  85. }
  86.  
  87. public class Client1 {
  88. public static void main(String args[]) throws IOException{
  89.  
  90. String str;
  91. Socket socket=new Socket("127.0.0.1",2000);
  92.  
  93. PrintStream prout=new PrintStream(socket.getOutputStream());
  94. BufferedReader bread=new BufferedReader(new InputStreamReader(System.in));
  95. BufferedReader dis=new BufferedReader(new InputStreamReader(socket.getInputStream()));
  96.  
  97. while(true){
  98. System.out.println("Send to others:");
  99. str=bread.readLine();
  100. prout.println(str);
  101.  
  102. }
  103. }
  104.  
  105. }
  106.  
  107. public class Client2 {
  108. public static void main(String args[]) throws IOException{
  109.  
  110. String str;
  111. Socket socket=new Socket("127.0.0.1",2000);
  112.  
  113. BufferedReader dis=new BufferedReader(new InputStreamReader(socket.getInputStream()));
  114.  
  115. while(true){
  116. str=dis.readLine();
  117. System.out.print("Message: "+str+"n");
  118.  
  119. }
  120. }
  121.  
  122. }
Add Comment
Please, Sign In to add comment