开发者

how to get the socket type?

i've a class, that expects to get either an udp or tcp socket.

Now, i want to check, what kind of socket has been given to the class.

class NetClientWriter {
    public __construct($socket=null) {
    // do we have a socket?
    if(!is_null($socket)) {
        if(!is_resource($socket) ||
           strtolower(get_resource_type($socket))!='socket') 
            throw new InvalidSocketTypeExeption("This is not a resource of type socket.");
    }
    // socket type
    if(socket_get_option($socket, SOL_SOCKET, SO_TYPE)==SOCK_STREAM) {
        echo("TCP!!!!!");
    }
    elseif(socket_get_option($soc开发者_开发问答ket, SOL_SOCKET, SO_TYPE)==SOCK_DGRAM) {
        echo("UDP!!!!!");
    }
    else throw new InvalidSocketTypeExeption("Invalid socket type. Just UDP and TCP sockets supported.");
    }
}

thanks a lot


Use socket_get_option and check for SOCK_STREAM (TCP) vs SOCK_DGRAM (UDP).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜