`
默默的小熊
  • 浏览: 232742 次
社区版块
存档分类
最新评论

TSocket

 
阅读更多

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;

/**
 * Socket implementation of the TTransport interface. To be commented soon!
 *
 */
public class TSocket extends TIOStreamTransport {

  private static final Logger LOGGER = LoggerFactory.getLogger(TSocket.class.getName());

  /**
   * Wrapped Socket object
   */
  private Socket socket_ = null;

  /**
   * Remote host
   */
  private String host_  = null;

  /**
   * Remote port
   */
  private int port_ = 0;

  /**
   * Socket timeout
   */
  private int timeout_ = 0;

  /**
   * Constructor that takes an already created socket.
   *
   * @param socket Already created socket object
   * @throws TTransportException if there is an error setting up the streams
   */
  public TSocket(Socket socket) throws TTransportException {
    socket_ = socket;
    try {
      socket_.setSoLinger(false, 0);
      socket_.setTcpNoDelay(true);
    } catch (SocketException sx) {
      LOGGER.warn("Could not configure socket.", sx);
    }

    if (isOpen()) {
      try {
        inputStream_ = new BufferedInputStream(socket_.getInputStream(), 1024);
        outputStream_ = new BufferedOutputStream(socket_.getOutputStream(), 1024);
      } catch (IOException iox) {
        close();
        throw new TTransportException(TTransportException.NOT_OPEN, iox);
      }
    }
  }

  /**
   * Creates a new unconnected socket that will connect to the given host
   * on the given port.
   *
   * @param host Remote host
   * @param port Remote port
   */
  public TSocket(String host, int port) {
    this(host, port, 0);
  }

  /**
   * Creates a new unconnected socket that will connect to the given host
   * on the given port.
   *
   * @param host    Remote host
   * @param port    Remote port
   * @param timeout Socket timeout
   */
  public TSocket(String host, int port, int timeout) {
    host_ = host;
    port_ = port;
    timeout_ = timeout;
    initSocket();
  }

  /**
   * Initializes the socket object
   */
  private void initSocket() {
    socket_ = new Socket();
    try {
      socket_.setSoLinger(false, 0);
      socket_.setTcpNoDelay(true);
      socket_.setSoTimeout(timeout_);
    } catch (SocketException sx) {
      LOGGER.error("Could not configure socket.", sx);
    }
  }

  /**
   * Sets the socket timeout
   *
   * @param timeout Milliseconds timeout
   */
  public void setTimeout(int timeout) {
    timeout_ = timeout;
    try {
      socket_.setSoTimeout(timeout);
    } catch (SocketException sx) {
      LOGGER.warn("Could not set socket timeout.", sx);
    }
  }

  /**
   * Returns a reference to the underlying socket.
   */
  public Socket getSocket() {
    if (socket_ == null) {
      initSocket();
    }
    return socket_;
  }

  /**
   * Checks whether the socket is connected.
   */
  public boolean isOpen() {
    if (socket_ == null) {
      return false;
    }
    return socket_.isConnected();
  }

  /**
   * Connects the socket, creating a new socket object if necessary.
   */
  public void open() throws TTransportException {
    if (isOpen()) {
      throw new TTransportException(TTransportException.ALREADY_OPEN, "Socket already connected.");
    }

    if (host_.length() == 0) {
      throw new TTransportException(TTransportException.NOT_OPEN, "Cannot open null host.");
    }
    if (port_ <= 0) {
      throw new TTransportException(TTransportException.NOT_OPEN, "Cannot open without port.");
    }

    if (socket_ == null) {
      initSocket();
    }

    try {
      socket_.connect(new InetSocketAddress(host_, port_), timeout_);
      inputStream_ = new BufferedInputStream(socket_.getInputStream(), 1024);
      outputStream_ = new BufferedOutputStream(socket_.getOutputStream(), 1024);
    } catch (IOException iox) {
      close();
      throw new TTransportException(TTransportException.NOT_OPEN, iox);
    }
  }

  /**
   * Closes the socket.
   */
  public void close() {
    // Close the underlying streams
    super.close();

    // Close the socket
    if (socket_ != null) {
      try {
        socket_.close();
      } catch (IOException iox) {
        LOGGER.warn("Could not close socket.", iox);
      }
      socket_ = null;
    }
  }

}
 
分享到:
评论

相关推荐

    TTSocket_V1_1_4.zip_TTSocket_V1_1_4_delphi tsocket

    "delphi tsocket"标签进一步确认了这是与Delphi编程语言和TCP/IP套接字(Sockets)技术相关的组件。 Delphi是Borland公司开发的一种集成开发环境(IDE),广泛用于创建Windows和Web应用程序。它基于Object Pascal...

    TCP Socket类

    #if defined AFX TSOCKET H ECFF7A02 DCAF 455D 97C3 0C1D465D977B INCLUDED #define AFX TSOCKET H ECFF7A02 DCAF 455D 97C3 0C1D465D977B INCLUDED #if MSC VER &gt; 1000 #pragma once #endif MSC VER &gt; ...

    qt tcpsocket 传送结构体信息

    在IT领域,网络通信是不可或缺的一部分,而QT作为一个强大的跨平台开发框架,提供了丰富的网络通信功能,其中包括TCP(传输控制协议)套接字。本文将深入探讨如何在QT环境中使用TCP套接字来传递结构体信息,这在很多...

    IOCP网络通信框架

    `TSocket.cpp`包含了`TSocket`类的实现,这是一个封装了套接字操作的类。这个类可能会有打开、关闭套接字、绑定、监听、接受连接、发送和接收数据等方法。由于IOCP模型是基于套接字的,所以`TSocket`类是整个框架与...

    C#通过thrift连接hbase操作步骤

    它创建了一个`TSocket`对象,指定HBase服务器的IP地址和端口号,然后创建`TBinaryProtocol`实例和`Hbase.Client`对象,打开连接并调用`getRow`方法来获取指定表和行的数据。 5. **启动服务**:在运行C#客户端之前,...

    从Thrift服务框架思考服务器框架-真的很有收获

    TTransport(负则传输的模块,就是底层I/O的实现):每一种传输方式都对应一个该模块,比如TSocket负则Socket通信,负责传输的对象就是Message。 TProtocol:这个就是协议模块,因为对Message的传输需要统一,否则就...

    python操作hbase

    transport = TSocket.TSocket('localhost', 9090) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Hbase.Client(protocol) try: ...

    C++实现Thrift双向通信实例V1.0

    TSocket* socket = new TSocket("localhost", 9090); TBufferedTransport* transport = new TBufferedTransport(socket); TBinaryProtocol* protocol = new TBinaryProtocol(transport); MyServiceClient client...

    介绍当前Windows支持的各种Socket I/O模型

    ASock, MainSock: TSocket; len, i: Integer; begin MainSock := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); addr.sin_family := AF_INET; addr.sin_port := htons(5678); addr.sin_addr.S_addr := htonl...

    php_thrift_python安装测试记录

    $transport = new TSocket('localhost', 9090); $protocol = new TBinaryProtocol($transport); $client = new MyServiceClient($protocol); $transport-&gt;open(); $result = $client-&gt;hello('World'); $transport-&gt;...

    python thrift2 connect hbase

    transport = TSocket.TSocket('localhost', 9090) transport = TTransport.TBufferedTransport(transport) # 创建协议实例 protocol = TBinaryProtocol.TBinaryProtocol(transport) # 创建HBase客户端 client = ...

    藏经阁-HBase多语言访问.pdf

    1. Transport:Thrift提供了多种Transport方式,包括TSocket、TFileTransport、TZlibTransport、TBufferedTransport和TFramedTransport。这些Transport方式可以根据需要选择,例如TSocket用于 socket 通信,...

    thrift的使用介绍

    socket = TSocket.TSocket('localhost', 9090) transport = TTransport.TBufferedTransport(socket) protocol = TBinaryProtocol.TBinaryProtocol(transport) handler = HelloServiceImpl() processor = processor...

    【Thrift之C++远程调用helloworld菜鸟教程】

    在服务器端,我们需要创建一个`TProcessor`实例(这里是`helloworld::HelloWorldProcessor`),然后绑定到一个`TSocket`或更高级的传输层,如`TFramedTransport`。接着,我们可以启动一个`TServer`(如`...

    解决python线程卡死的问题

    transport = TSocket.TSocket('10.134.113.75', 1234) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Client(protocol) transport....

    python实现的RPC例子

    transport = TSocket.TSocket('localhost', 9090) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Client(protocol) transport.open...

    btpeer P2P 仿真平台

    在C++中,标准库中没有提供多线程安全的字符串操作,所以"cstring.cpp"可能是为了提供线程安全的字符串操作,而"tSocket.cpp"可能是一个封装了线程安全特性的套接字类,确保在网络通信中的并发操作不会引发问题。...

    thriftTest java案例代码

    客户端则创建一个`TTransport`对象(如`TSocket`)连接到服务器,然后构建一个`TProtocol`,并通过`TestService$Client`与服务交互: ```java public class Client { public static void main(String[] args) ...

Global site tag (gtag.js) - Google Analytics