点击或拖拽改变大小

NetworkBaseCreateSocketAndConnect 方法 (IPEndPoint, Int32, IPEndPoint)

创建一个新的socket对象并连接到远程的地址,需要指定远程终结点,超时时间(单位是毫秒),如果需要绑定本地的IP或是端口,传入 local对象
To create a new socket object and connect to the remote address, you need to specify the remote endpoint, the timeout period (in milliseconds), if you need to bind the local IP or port, pass in the local object

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

参数

endPoint
类型:System.NetIPEndPoint
连接的目标终结点
timeOut
类型:SystemInt32
连接的超时时间
local (Optional)
类型:System.NetIPEndPoint
如果需要绑定本地的IP地址,就需要设置当前的对象

返回值

类型: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
        }
    }
}
参见