文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>[ZZ].NET中通过TCP信道通信

[ZZ].NET中通过TCP信道通信

时间:2011-04-01  来源:wk23415

原文出处http://lmarsy.bokee.com/1225863.html

最近碰到一个问题,朋友说要用FIFO解决.晕了,看了看SDK帮助,这个和FIFO有些接近.

先调试通过了,看了看原理,也明白的.呵呵 贴出来,免得以后忘记了.

等两天把它改为非阻塞的/


建一个windows项目
建一个server.cs文件

server.cs

View Code
//-----------------------------------------------------------------------
// This file is part of the Microsoft .NET SDK Code Samples.
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation. See these other
//materials for detailed information regarding Microsoft code samples.
//
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//-----------------------------------------------------------------------

////////////////
//注意:在项目引用中得引用System.Runtime.Remoting
////////////////
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace RemotingSamples {

public class Sample
{

public static int Main(string [] args)
{

TcpChannel chan
= new TcpChannel(8085);
ChannelServices.RegisterChannel(chan);
RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType(
"RemotingSamples.HelloServer"), "SayHello", WellKnownObjectMode.SingleCall);
System.Console.WriteLine(
"Hit to exit...");
System.Console.ReadLine();
return 0;
}

}
public class HelloServer : MarshalByRefObject
{

public HelloServer()
{
Console.WriteLine(
"HelloServer activated");
}

public String HelloMethod(String name)
{
Console.WriteLine(
"Server111 : {0}", name);
return String.Format("Server222 {0}", name);
}
}
}
/////////////////////////////////////////

客户端 用windows程序或者aspx 都行
我测试是用的aspx.
在页面cs文件中添加一个类

View Code
public class HelloServer : MarshalByRefObject 
{

public HelloServer()
{
// Console.WriteLine("HelloServer activated");
}

public String HelloMethod(String name)
{

return "";//String.Format("Client222 {0}", name);
}
}
让button1的click事件响应,发送信号192.
168.0.33是我的IP
private void Button1_Click(object sender, System.EventArgs e)
{
HelloServer obj
= (HelloServer)Activator.GetObject(typeof(RemotingSamples.HelloServer), "tcp://192.168.0.33:8085/SayHello");
if (obj == null)
{
System.Console.WriteLine(
"Could not locate server");
}
else
{
for(int i=0;i<100;i++)
{
//发100次
//Console.WriteLine("Client00:"+obj.HelloMethod(i.ToString())+"\n\r");

Response.Write(
"Client00:"+obj.HelloMethod(i.ToString())+"
"
);
}
}
//System.Console.ReadLine();
//return 0;
}

 当运行 server的时候,点击button1按钮,server的控制台就会打印出
 HelloServer activated
        Server111 : 88
 也面会打印出:
 Client00:Server222 60

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载