         }
        return nil
     }
+    
+    public func read(inout buffer buffer:[UInt8], expectlen:Int? = nil, timeout:Int = -1) throws -> Int {
+        if let fd:Int32 = self.fd{
+            let len = expectlen ?? buffer.count
+            let readLen:Int32=c_ytcpsocket_pull(fd, buff: &buffer, len: Int32(len), timeout: Int32(timeout))
+            return Int(readLen)
+        } else {
+            throw Error.SocketNotOpen
+        }
+    }
+    
+    public enum Error : ErrorType {
+        case SocketNotOpen
+        case SocketFunction
+        case Send
+        case Timeout
+        case ConnectFail
+        case ConnectionClosed
+        case Unknown
+    }
 }
+
 
 public class TCPServer:YSocket{
 

    * send data
    * return success or fail with message
    */
    public func send(data d:[UInt8])->(Bool,String){
        if let fd:Int32=self.fd{
            let sendsize:Int32=c_ytcpsocket_send(fd, buff: d, len: Int32(d.count))
            if Int(sendsize)==d.count{
               return (true,"send success")
            }else{
                return (false,"send error")
            }
        }else{
            return (false,"socket not open")
        }
    }
    /*
    * send string
    * return success or fail with message
    */
    public func send(str s:String)->(Bool,String){
        if let fd:Int32=self.fd{
            let sendsize:Int32=c_ytcpsocket_send(fd, buff: s, len: Int32(strlen(s)))
            if sendsize==Int32(strlen(s)){
                return (true,"send success")
            }else{
                return (false,"send error")
            }
        }else{
            return (false,"socket not open")
        }
    }
    /*
    *
    * send nsdata
    */
    public func send(data d:NSData)->(Bool,String){
        if let fd:Int32=self.fd{
            var buff:[UInt8] = [UInt8](count:d.length,repeatedValue:0x0)
            d.getBytes(&buff, length: d.length)
            let sendsize:Int32=c_ytcpsocket_send(fd, buff: buff, len: Int32(d.length))
            if sendsize==Int32(d.length){
                return (true,"send success")
            }else{
                return (false,"send error")
            }
        }else{
            return (false,"socket not open")
        }
    }
    /*
    * read data with expect length
    * return success or fail with message
    */
    public func read(expectlen:Int, timeout:Int = -1)->[UInt8]?{
        if let fd:Int32 = self.fd{
            var buff:[UInt8] = [UInt8](count:expectlen,repeatedValue:0x0)
            let readLen:Int32=c_ytcpsocket_pull(fd, buff: &buff, len: Int32(expectlen), timeout: Int32(timeout))

     * connect to server
     * return success or fail with message
     */
    public func connect(timeout t:Int) throws {
        let rs:Int32=c_ytcpsocket_connect(self.addr, port: Int32(self.port), timeout: Int32(t))
        if rs>0{
            self.fd=rs
            return
        }else{
            switch rs{
            case -1:
                throw Error.ConnectFail
            case -2:
                throw Error.ConnectionClosed
            case -3:
                throw Error.Timeout
            default:
                throw Error.Unknown
            }
        }
    }
