TcpClient.Client 属性获取或设置基础 Socket。
[C#]
protected Socket Client {get; set;}属性值
基础网络 Socket。备注
TcpClient 创建一个 Socket 以通过网络发送和接收数据。从 TcpClient 派生的类可以使用该属性获取或设置此 Socket。如果您需要的访问权限超出了 TcpClient 所提供的访问权限,请使用从 Client 返回的基础 Socket。还可以使用 Client 将基础 Socket 设置为现有的 Socket。如果要通过预先存在 Socket 来利用 TcpClient 的简易性,则可能会使用该属性。示例
下面的示例说明一个使用受保护属性 Client 的派生类。在此示例中,MyTcpClientDerivedClass 获取基础 Socket 以启用广播。[C#] 
// This derived class demonstrates the use of three protected methods belonging to the TcpClient class
public class MyTcpClientDerivedClass : TcpClient{// Constructor for the derived class.
public MyTcpClientDerivedClass() : base(){
}
public void UsingProtectedMethods(){  // Uses the protected 'Active' property belonging to the TcpClient base class 
  // to determine if a connection is established. 
  if (this.Active){
      // Calls the protected 'Client' property belonging to the TcpClient base class.
      Socket s = this.Client;
      // Uses the Socket returned by Client to set an option that is not available using TcpClient.
      s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  }
  // To free all resources, calls the protected virtual method Dispose belonging to the TcpClient base class.
 this.Dispose(true);
 GC.SuppressFinalize(this);
}
}参考上面的例子,得到socket后,用s.AddressFamily.ToString()得到IP地址

解决方案 »

  1.   

    先谢谢楼上!
    但是在AcceptTcpClient()方法中怎么得到socket,要是能得到socket那问题就解决了!
    请教在AcceptTcpClient()状态下怎么得到socket
      

  2.   

    直接AcceptSocket()就可以了。
      

  3.   

    wangsaokui(无间道III(终极无间)) 
    你的意思是说在“AcceptTcpClient()”里socket受到保护,要用到派生类才能访问到,是吗?
    但是我现在的c#水平很差啊,可不可以先帮帮我啊!
    Thank you!
      

  4.   

    this.Active这个为假,怎么办?