点击或拖拽改变大小

NetworkBaseCreateSocketAndConnect 方法 (String, Int32, Int32)

创建一个新的socket对象并连接到远程的地址,需要指定ip地址以及端口号信息,还有超时时间,单位是毫秒
To create a new socket object and connect to a remote address, you need to specify the IP address and port number information, and the timeout period in milliseconds

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

参数

ipAddress
类型:SystemString
Ip地址
port
类型:SystemInt32
端口号
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
        }
    }
}
参见