点击或拖拽改变大小

NetworkBaseCreateSocketAndConnect 方法 (String, Int32)

创建一个新的socket对象并连接到远程的地址,默认超时时间为10秒钟,需要指定ip地址以及端口号信息
Create a new socket object and connect to the remote address. The default timeout is 10 seconds. You need to specify the IP address and port number.

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

参数

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