Java Code Examples for org.apache.thrift.transport.TTransport#write()

The following examples show how to use org.apache.thrift.transport.TTransport#write() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServerTrans.java    From ThriftBook with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) 
       throws TTransportException, UnsupportedEncodingException {
    final String msg = "Hello Thrift!\n";
    final String stop_cmd = "STOP";
    final int buf_size = 1024*8;
    byte[] buf = new byte[buf_size];
    final int port = 9090;

    TServerTransport acceptor = new TServerSocket(9090);
    acceptor.listen();
    System.out.println("[Server] listening on port: " + port);
    
    String input;
    do {
        TTransport trans = acceptor.accept();
        int len = trans.read(buf, 0, buf_size);
        input = new String(buf, 0, len,"UTF-8");
        System.out.println("[Server] handling request: " + input);
        trans.write(msg.getBytes());
        trans.flush();
        trans.close();
    } while (! stop_cmd.regionMatches(0, input, 0, 4)); 

    System.out.println("[Server] exiting");
    acceptor.close();
}
 
Example 2
Source File: TransExcep.java    From ThriftBook with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
     try {
         TTransport trans = new TSimpleFileTransport("data", false, true);
         Trade trade = new Trade();
         trade.symbol = "F";
         trade.price = 13.10;
         trade.size = 2500;
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         ObjectOutputStream oos = new ObjectOutputStream(baos);
         oos.writeObject(trade);
         trans.write(baos.toByteArray());
trans.close();

         trans = new TSimpleFileTransport("data",
                                      ((args.length==0) ? true : false), 
                                      true);			
         byte[] buf = new byte[128];
         int iBytesRead = trans.read(buf, 0, buf.length);
         ByteArrayInputStream bais = new ByteArrayInputStream(buf);
         ObjectInputStream ois = new ObjectInputStream(bais);
         trade = (Trade) ois.readObject();
         System.out.println("Trade(" + iBytesRead + "): " + trade.symbol + " " + 
                            trade.size + " @ " + trade.price);
     } catch (TTransportException tte) {				
         System.out.println("TTransportException(" + tte.getType() + 
                            "): " + tte);
     } catch (TException te) {						
         System.out.println("TException: " + te);
     } catch (Exception e) {						
         System.out.println("Exception: " + e);
     } catch (Throwable t) {						
         System.out.println("Throwable: " + t);			
     }
 }
 
Example 3
Source File: ServerFrame.java    From ThriftBook with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) 
         throws TTransportException, UnsupportedEncodingException, SocketException {
     final String msg = "Hello Thrift!\n";
     final String stop_cmd = "STOP";
     final int buf_size = 1024*8;
     byte[] buf = new byte[buf_size];
     final int port = 9090;

     TServerTransport acceptor = new TServerSocket(port);
     acceptor.listen();
     System.out.println("[Server] listening on port " + port);

     while (true) {
         TTransport trans_ep = acceptor.accept();
TTransport trans = new TFramedTransport(trans_ep);
         int len = trans.read(buf, 0, buf_size);
         String input = new String(buf, 0, len, "UTF-8");
         System.out.println("[Server] handling request: " + input);
         trans.write(msg.getBytes());
         trans.flush();
         trans.close();
         if (stop_cmd.regionMatches(0, input, 0, 4)) {
            break;
         }
     }
     System.out.println("[Server] exiting");
     acceptor.close();
 }
 
Example 4
Source File: SockTrans.java    From ThriftBook with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws TTransportException {
    //Display web page
    TTransport trans = new TSocket("thrift.apache.org", 80);
    final String msg = "GET / \n";
    trans.open();
    trans.write(msg.getBytes());
    trans.flush();							
    read_trans(trans);
    trans.close();
    //Display file
    trans = new TSimpleFileTransport("SockTrans.java");
    trans.open();
    read_trans(trans);
    trans.close();
}