Remoting访问的问题,请高手不吝赐教

wzkqqq 2007-03-12 04:39:20
如何在服务器端获取客户端访问的端口(服务器端开启了多个端口供客户端访问)
...全文
418 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
withcsharp 2007-03-14
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Security.Permissions;
using System.Runtime.Serialization.Formatters;
using System.Collections;
using r;
namespace WindowsApplication12
{

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private System.Runtime.Remoting.Channels.Tcp.TcpServerChannel serverChannel;
private BinaryServerFormatterSinkProvider serverProvider;
private ClientIPServerSinkProvider IPProvider;
private cmyLogin mylogin;
private void Form1_Load(object sender, EventArgs e)
{
mylogin=new cmyLogin();
serverProvider = new BinaryServerFormatterSinkProvider();
IPProvider = new ClientIPServerSinkProvider();

IPProvider.Next = serverProvider;//链接上下一个provider
serverProvider.TypeFilterLevel = TypeFilterLevel.Full;

IDictionary props = new Hashtable();
props["port"] = 9090;
props["id"] = string.Empty;



serverChannel =
new TcpServerChannel(props, IPProvider,mylogin);

RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Remotable), "Remotable.rem", WellKnownObjectMode.Singleton
);

// Show the name and priority of the channel.
Console.WriteLine("Channel Name: {0}", serverChannel.ChannelName);
Console.WriteLine("Channel Priority: {0}", serverChannel.ChannelPriority);

// Show the URIs associated with the channel.
ChannelDataStore data = (ChannelDataStore)serverChannel.ChannelData;
foreach (string uri in data.ChannelUris)
{
Console.WriteLine(uri);
}

// Wait for method calls.
Console.WriteLine("Listening...");


}
}
}
withcsharp 2007-03-14
  • 打赏
  • 举报
回复
using System;
using System.Collections;
using System.IO;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Channels;
using System.Threading;
using System.Net;
using System.Security.Principal;

namespace r
{
public class Remotable : MarshalByRefObject
{

private int callCount = 0;

public int GetCount()
{

callCount++;
return (callCount);
}

}
public class cmyLogin : IAuthorizeRemotingConnection
{
public bool IsConnectingEndPointAuthorized(EndPoint endPoint)
{
if (endPoint.AddressFamily.ToString() != "")
return true;
else
return true;
}
public bool IsConnectingIdentityAuthorized(IIdentity identity)
{
return true;
}

}
public class ClientIPServerSinkProvider : IServerChannelSinkProvider
{
private IServerChannelSinkProvider next = null;
public ClientIPServerSinkProvider()
{

}

public void GetChannelData(IChannelDataStore channelData)
{

}

public IServerChannelSink CreateSink(IChannelReceiver channel)
{
IServerChannelSink nextSink = null;
if (next != null)
{
nextSink = next.CreateSink(channel);
}
return new ClientIPServerSink(nextSink);
}

public IServerChannelSinkProvider Next
{
get { return next; }
set { next = value; }
}
}

public class ClientIPServerSink : BaseChannelObjectWithProperties, IServerChannelSink, IChannelSinkBase
{

private IServerChannelSink _next;

public ClientIPServerSink(IServerChannelSink next)
{
_next = next;
}

public void AsyncProcessResponse(System.Runtime.Remoting.Channels.IServerResponseChannelSinkStack sinkStack, System.Object state, System.Runtime.Remoting.Messaging.IMessage msg, System.Runtime.Remoting.Channels.ITransportHeaders headers, System.IO.Stream stream)
{

}

public Stream GetResponseStream(System.Runtime.Remoting.Channels.IServerResponseChannelSinkStack sinkStack, System.Object state, System.Runtime.Remoting.Messaging.IMessage msg, System.Runtime.Remoting.Channels.ITransportHeaders headers)
{
return null;
}

public System.Runtime.Remoting.Channels.ServerProcessing ProcessMessage(System.Runtime.Remoting.Channels.IServerChannelSinkStack sinkStack, System.Runtime.Remoting.Messaging.IMessage requestMsg, System.Runtime.Remoting.Channels.ITransportHeaders requestHeaders, System.IO.Stream requestStream, out System.Runtime.Remoting.Messaging.IMessage responseMsg, out System.Runtime.Remoting.Channels.ITransportHeaders responseHeaders, out System.IO.Stream responseStream)
{

if (_next != null)
{


IPAddress ip = requestHeaders[CommonTransportKeys.IPAddress] as IPAddress;

Console.WriteLine(ip.ToString());

CallContext.SetData("ClientIPAddress", ip);

ServerProcessing spres = _next.ProcessMessage(sinkStack, requestMsg, requestHeaders, requestStream, out responseMsg, out responseHeaders, out responseStream);

return spres;

}

else
{

responseMsg = null;

responseHeaders = null;

responseStream = null;

return new ServerProcessing();

}

}

public IServerChannelSink NextChannelSink
{

get { return _next; }

set { _next = value; }

}

}

}
withcsharp 2007-03-14
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/5054/5054452.xml?temp=.7828333
wzkqqq 2007-03-14
  • 打赏
  • 举报
回复
还是先结了吧,
很感谢,withcsharp()
最后的结果 我做好后再贴出来给大家.
wzkqqq 2007-03-14
  • 打赏
  • 举报
回复
谢谢 withcsharp()
我正在看...
haiwangstar 2007-03-13
  • 打赏
  • 举报
回复
用不同的端口來區分不同用戶這不是一個好辦法。
wzkqqq 2007-03-13
  • 打赏
  • 举报
回复
顶,大家帮帮忙,一定有人知道的
wzkqqq 2007-03-13
  • 打赏
  • 举报
回复
我不想用传值的方法,从服务端直接获得的更好些吧
lcg200x 2007-03-13
  • 打赏
  • 举报
回复
把用户id传过来
wzkqqq 2007-03-13
  • 打赏
  • 举报
回复
有很多信息是根据端口设置的
我想肯定是有办法获得的,有没有人知道啊,
问题还没有解决,大家多帮忙顶下,
wzkqqq 2007-03-13
  • 打赏
  • 举报
回复
这已经定下来了 我只能做了
我也认为不好,不过没事 我们这是内部使用的 用户不会很多

我想应该可以实现的,大家帮忙顶啊 谢了
huangyj 2007-03-13
  • 打赏
  • 举报
回复
用不同的端口方法不好,这样如果用户多岂不是要开N个端口?
wzkqqq 2007-03-13
  • 打赏
  • 举报
回复
我怎么获得呢 愿闻其详
withcsharp 2007-03-13
  • 打赏
  • 举报
回复
IAuthorizeRemotingConnection 接口提供了一些方法,可根据客户端的网络地址和用户标识来指示客户端是否被授权连接至当前信道。


public TcpServerChannel (
IDictionary properties,
IServerChannelSinkProvider sinkProvider,
IAuthorizeRemotingConnection authorizeCallback
)
wzkqqq 2007-03-13
  • 打赏
  • 举报
回复
withcsharp()
能说的具体点吗
withcsharp 2007-03-13
  • 打赏
  • 举报
回复
IAuthorizeRemotingConnection 可以吗?
wzkqqq 2007-03-13
  • 打赏
  • 举报
回复
我是因为别的原因要区分,总之是想要区分,想要获取对象是通过多个端口中的哪一个传入的
withcsharp 2007-03-13
  • 打赏
  • 举报
回复
// Summary:
// Defines how well-known objects are activated.
[Serializable]
[ComVisible(true)]
public enum WellKnownObjectMode
{
// Summary:
// Every incoming message is serviced by the same object instance.
Singleton = 1,
//
// Summary:
// Every incoming message is serviced by a new object instance.
SingleCall = 2,
}

你用的 SingleCall?
要区分吗
wzkqqq 2007-03-13
  • 打赏
  • 举报
回复
我想 在客户端向服务器发送的远程对象里面应该包含类似的如IP,端口,对象类型等信息吧
应该有方法可以把这个信息取出来,只是我没有找到
wzkqqq 2007-03-13
  • 打赏
  • 举报
回复
我的意思是说:不需要客户端发送类似的信息,直接通过服务器自行检测得到访问的哪个端口
加载更多回复(7)

111,119

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧