Tcp服务测试工具

服务端配置文件

 <system.runtime.remoting>
    <application>
      <service>
        <!--SingleCall activated wellknown-->
        <wellknown
           mode="Singleton" 
           type="Capinfo.BJMedicare.HospitalComponent.Service.CenterServer.CallProxy.RemotingService.ProxyObject,
HCS.CenterServer.CallProxy.RemotingService"
           objectUri="RemotingService.rem"
            />
      </service>
      <channels>
          <channel ref="tcp" port="9100" useIpAddress="true"/>
      </channels>
    </application>
    <customErrors mode="off"/>
  </system.runtime.remoting>

访问服务器地址: tcp://127.0.0.1:9100/RemotingService.rem  地址由两部分构成:IP端口 tcp://127.0.0.1:9100/  以及Uri:RemotingService.rem

客户端测试代码:

 private void btnTest_Click(object sender, EventArgs e)
    {
      try
      {
        string sFunctionCallWebServiceUrl = string.Format("tcp://{0}{1}", txtIP.Text, txtService.Text + "/RemotingService.rem");
        MarshalByRefObject obj = (MarshalByRefObject)Activator.GetObject(typeof(MarshalByRefObject), sFunctionCallWebServiceUrl);
        object o = obj.GetLifetimeService();
        MessageBox.Show("测试成功");
      }
      catch (System.Net.Sockets.SocketException ex)
      {
        MessageBox.Show(ex.Message);
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message);
      }
    }

由于MarshalByRefObject 是所有访问对象继承的基础,所以我想到用MarshalByRefObject来替代测试对象 实现测试效果。

由于只是测试TCP端口服务是否可以连通 而不是测试服务的具体实现效果 所以这几行简单的代码就可以实现.

 


posted on 2016-12-16 11:02  酒歌  阅读(1718)  评论(0编辑  收藏  举报