C4Grey

SocketConnection

May 29th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.13 KB | None | 0 0
  1. package FlashServer;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.EOFException;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.io.InterruptedIOException;
  8. import java.io.PrintWriter;
  9. import java.net.Socket;
  10.  
  11. class SocketConnection extends Thread {
  12.     private Socket _socket;
  13.     private BufferedReader _socketIn;
  14.     private PrintWriter _socketOut;
  15.  
  16.     public SocketConnection(Socket socket_) {
  17.         _socket = socket_;
  18.     }
  19.  
  20.     public void run() {
  21.         try {
  22.             _socket.setSoTimeout(10000);
  23.             _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
  24.             _socketOut = new PrintWriter(_socket.getOutputStream(), true);
  25.         } catch (IOException e) {
  26.             if (MainServer.DEBUG)
  27.                 System.out.println("IO Exception " + e.getMessage());
  28.             return;
  29.         }
  30.  
  31.         readPolicyRequest();
  32.     }
  33.  
  34.     private void readPolicyRequest() {
  35.         try {
  36.             if (MainServer.POLICY_REQUEST.equals(read())) {
  37.                 write(MainServer.PolicyXML());
  38.             }
  39.         } catch (Exception e) {
  40.             if (MainServer.DEBUG)
  41.                 System.out.println("Exception " + e.getMessage());
  42.         }
  43.         close();
  44.     }
  45.  
  46.     private String read() throws IOException, EOFException,
  47.             InterruptedIOException {
  48.         StringBuffer buffer = new StringBuffer();
  49.         int codePoint;
  50.         boolean zeroByteRead = false;
  51.  
  52.         if (MainServer.DEBUG) {
  53.             System.out.println("Reading...");
  54.         }
  55.         do {
  56.             codePoint = _socketIn.read();
  57.             if (codePoint == 0)
  58.                 zeroByteRead = true;
  59.             else
  60.                 buffer.appendCodePoint(codePoint);
  61.         } while (!zeroByteRead && buffer.length() < 100);
  62.         if (MainServer.DEBUG)
  63.             System.out.println("Read: " + buffer.toString());
  64.  
  65.         return buffer.toString();
  66.     }
  67.  
  68.     public void write(String msg) {
  69.         _socketOut.println(msg + "\u0000");
  70.         _socketOut.flush();
  71.         if (MainServer.DEBUG) {
  72.             System.out.println("Wrote: " + msg);
  73.         }
  74.     }
  75.  
  76.     public void close() {
  77.         try {
  78.             if (_socket != null)
  79.                 _socket.close();
  80.             if (_socketOut != null)
  81.                 _socketOut.close();
  82.             if (_socketIn != null)
  83.                 _socketIn.close();
  84.             System.out.println("Port close.");
  85.         } catch (IOException e) {
  86.         }
  87.  
  88.         _socketIn = null;
  89.         _socketOut = null;
  90.         _socket = null;
  91.     }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment