点击或拖拽改变大小

NetworkBaseCreateSocketAndConnect 方法 (IPEndPoint, Int32)

创建一个新的socket对象并连接到远程的地址,需要指定远程终结点,超时时间(单位是毫秒)
To create a new socket object and connect to a remote address, you need to specify the remote endpoint and the timeout period (in milliseconds)

命名空间:  HslCommunication.Core.Net
程序集:  HslCommunication (在 HslCommunication.dll 中) 版本:9.6.4.0 (9.6.4.0)
语法
protected OperateResult<Socket> CreateSocketAndConnect(
	IPEndPoint endPoint,
	int timeOut
)

参数

endPoint
类型:System.NetIPEndPoint
连接的目标终结点
timeOut
类型:SystemInt32
连接的超时时间

返回值

类型:OperateResultSocket
返回套接字的封装结果对象
示例
创建连接示例
public class NetworkMy : NetworkBase
{
    public void CreateSocketAndConnectExample1( )
    {
        // 连接远程的端口
        OperateResult<Socket> socketResult = CreateSocketAndConnect( "192.168.0.100", 12345 );
        if (socketResult.IsSuccess)
        {
            // connect success
        }
        else
        {
            // failed
        }
    }

    public void CreateSocketAndConnectExample2( )
    {
        // 连接远程的端口,允许设置超时时间,比如1秒
        OperateResult<Socket> socketResult = CreateSocketAndConnect( "192.168.0.100", 12345, 1000);
        if (socketResult.IsSuccess)
        {
            // connect success
        }
        else
        {
            // failed
        }
    }

    public void CreateSocketAndConnectExample3( )
    {
        // 连接远程的端口,允许设置超时时间,比如1秒
        OperateResult<Socket> socketResult = CreateSocketAndConnect( new System.Net.IPEndPoint( System.Net.IPAddress.Parse( "192.168.0.100" ), 12345 ), 1000 );
        if (socketResult.IsSuccess)
        {
            // connect success
        }
        else
        {
            // failed
        }
    }
}
参见