Posted on

Java System

  1. import java.io.*;
  2. import java.net.*;
  3. class ClientHandler extends Thread {
  4.   protected Socket incoming;
  5.   public ClientHandler(Socket incoming) {
  6.     this.incoming = incoming;
  7.   }
  8.   public void run() {
  9.     try {
  10.         DataOutputStream out = new DataOutputStream(incoming.getOutputStream());
  11.         DataInputStream in = new DataInputStream(incoming.getInputStream());
  12. //      byte b;
  13. //
  14. //      while ((b = in.readByte()) != -1) {
  15. //      out.write(b);
  16.       out.writeBytes(“Hello! This is Java MultiEchoServer.”);
  17.       out.writeBytes(“Enter BYE to exit.”);
  18. //      out.flush();
  19.       for (;;) {
  20.         String str = in.readLine();
  21.         if (str == null) {
  22.           break;
  23.         } else {
  24.           out.writeBytes(“Echo: “);
  25.           out.flush();
  26.           System.out.println(“Received: ” + str);
  27.           if (str.trim().equals(“BYE”))
  28.             break;
  29.         }
  30.       }
  31.       incoming.close();
  32.     } catch (Exception e) {
  33.       System.out.println(“Error: ” + e);
  34.     }
  35.   }
  36. }
  37. public class MultiEchoServer {
  38.   public static void main(String[] args) {
  39.     System.out.println(“MultiEchoServer started.”);
  40.     try {
  41.       ServerSocket s = new ServerSocket(4445);
  42.       for (;;) {
  43.         Socket incoming = s.accept();
  44.         new ClientHandler(incoming).start();
  45.       }
  46.     } catch (Exception e) {
  47.       System.out.println(“Error: ” + e);
  48.     }
  49.     System.out.println(“MultiEchoServer stopped.”);
  50.   }
  51. }
===================================================
import java.io.*;
import java.net.*;

class ClientHandler extends Thread {

protected Socket incoming;

public ClientHandler(Socket incoming) {
this.incoming = incoming;
}

public void run() {
try {
DataOutputStream out = new DataOutputStream(incoming.getOutputStream());
DataInputStream in = new DataInputStream(incoming.getInputStream());

//        byte b;
//
//        while ((b = in.readByte()) != -1) {
//        out.write(b);

out.writeBytes(“Hello! This is Java MultiEchoServer.”);
out.writeBytes(“Enter BYE to exit.”);
//      out.flush();

for (;;) {
String str = in.readLine();
if (str == null) {
break;
} else {
out.writeBytes(“Echo: “);
out.flush();
System.out.println(“Received: ” + str);

if (str.trim().equals(“BYE”))
break;
}
}
incoming.close();

} catch (Exception e) {
System.out.println(“Error: ” + e);
}
}

}

public class MultiEchoServer {

public static void main(String[] args) {
System.out.println(“MultiEchoServer started.”);

try {
ServerSocket s = new ServerSocket(4445);
for (;;) {
Socket incoming = s.accept();
new ClientHandler(incoming).start();
}
} catch (Exception e) {
System.out.println(“Error: ” + e);
}

System.out.println(“MultiEchoServer stopped.”);
}
}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s