miceZipper

SimpleClientServer

Apr 27th, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. package test;
  2.  
  3. import java.io.IOException;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. import java.net.UnknownHostException;
  7. import java.util.concurrent.ExecutorService;
  8. import java.util.concurrent.Executors;
  9.  
  10. public class Test {
  11.  
  12.     String address = "127.0.0.1";
  13.     int port = 15561;
  14.     String msg = "Hello There";
  15.  
  16.     private Test() {
  17.         ExecutorService exec = Executors.newCachedThreadPool();
  18.         exec.execute(new Runnable() {
  19.  
  20.             @Override
  21.             public void run() {
  22.                 try {
  23.                     new Socket(address, port).getOutputStream().write(msg.getBytes());
  24.                 } catch (UnknownHostException ex) {
  25.                     System.out.println("[Sender] Error: " + ex);
  26.                 } catch (IOException ex) {
  27.                     System.out.println("[Sender] Error: " + ex);
  28.                 }
  29.             }
  30.         });
  31.         exec.execute(new Runnable() {
  32.  
  33.             @Override
  34.             public void run() {
  35.                 try {
  36.                     byte[] buf = new byte[64 * 1024];
  37.                     new ServerSocket(port).accept().getInputStream().read(buf);
  38.                     System.out.println(new String(buf));
  39.                 } catch (IOException ex) {
  40.                     System.out.println("[Reciever] Error: " + ex);
  41.                 }
  42.             }
  43.         });
  44.         exec.shutdown();
  45.     }
  46.  
  47.     public static void main(String[] args) {
  48.         new Test();
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment