1 7 package org.jboss.test.remoting.transport.socket.raw; 8 9 12 import java.io.BufferedInputStream ; 13 import java.io.BufferedOutputStream ; 14 import java.io.IOException ; 15 import java.io.ObjectInputStream ; 16 import java.io.ObjectOutputStream ; 17 import java.net.InetAddress ; 18 import java.net.ServerSocket ; 19 import java.net.Socket ; 20 21 24 public class RawServer implements Runnable 25 { 26 protected int serverBindPort = 6700; 27 protected int backlog = 200; 28 protected ServerSocket serverSocket; 29 protected InetAddress bindAddress; 30 protected String serverBindAddress = "localhost"; 31 protected int timeout = 60000; 33 public RawServer() 34 { 35 try 36 { 37 bindAddress = InetAddress.getByName(serverBindAddress); 38 serverSocket = new ServerSocket (serverBindPort, backlog, bindAddress); 39 } 40 catch(IOException e) 41 { 42 e.printStackTrace(); 43 } 44 } 45 46 public void startServer() 47 { 48 new Thread (this).start(); 49 } 50 51 62 public void run() 63 { 64 try 65 { 66 Socket socket = serverSocket.accept(); 67 socket.setSoTimeout(timeout); 68 69 BufferedOutputStream bos = new BufferedOutputStream (socket.getOutputStream()); 70 BufferedInputStream bis = new BufferedInputStream (socket.getInputStream()); 73 75 ObjectOutputStream oos = new ObjectOutputStream (bos); 76 oos.flush(); 77 ObjectInputStream objInputStream = new ObjectInputStream (bis); 78 79 80 processRequest(objInputStream, oos); 81 82 while(true) 83 { 84 acknowledge(objInputStream, oos); 85 processRequest(objInputStream, oos); 86 } 87 88 89 } 90 catch(IOException e) 91 { 92 e.printStackTrace(); 93 } 94 catch(ClassNotFoundException e) 95 { 96 e.printStackTrace(); 97 } 98 } 99 100 private void processRequest(ObjectInputStream objInputStream, ObjectOutputStream oos) 101 throws IOException , ClassNotFoundException 102 { 103 Object obj = objInputStream.readObject(); 104 objInputStream.readObject(); 105 106 109 System.out.println("Object read: " + obj); 110 111 Object resp = null; 112 try 113 { 114 boolean interrupted = Thread.interrupted(); 116 resp = "This is response."; 118 } 119 catch(Exception ex) 120 { 121 resp = ex; 122 } 123 124 Thread.interrupted(); 126 127 oos.writeObject(resp); 128 129 131 oos.reset(); 132 oos.writeObject(Boolean.TRUE); 136 oos.flush(); 137 oos.reset(); 138 } 139 140 private void acknowledge(ObjectInputStream objInputStream, ObjectOutputStream oos) throws IOException 141 { 142 System.out.println("waiting for ack"); 144 byte ACK = objInputStream.readByte(); 145 System.out.println("got ack"); 146 oos.writeByte(ACK); 147 oos.flush(); 148 oos.reset(); 149 150 } 151 152 public static void main(String [] args) 153 { 154 RawServer server = new RawServer(); 155 server.startServer(); 156 } 157 } | Popular Tags |