如何解决试图创建一种可以写入控制台或Form应用程序的方法
| 我正在使用通用服务器和客户端程序。我面临的问题是当我在客户端和服务器中的OnDataReceived中时,我不知道如何处理数据。理想情况下,它应该将接收到的数据输出到一个窗口中,但是我不知道它将是Form还是Console应用程序。因此,问题是如何创建可以同时处理这两种方法的通用方法,或者如果无法处理,该怎么办? 我正在使用的代码: SocketPacket theSockId = (SocketPacket)asyn.AsyncState;
int iRx = theSockId.m_currentSocket.EndReceive(asyn);
char[] chars = new char[iRx + 1];
Decoder decode = Encoding.Default.GetDecoder();
int charLength = decode.GetChars(theSockId.dataBuffer,iRx,chars,0);
String szData = new String(chars);
//Handle Message here
WaitForData();
和套接字包类:
class SocketPacket
{
public Socket m_currentSocket;
public byte[] dataBuffer = new byte[1024];//Buffer to store the data by the client
public SocketPacket(Socket socket)
{
m_currentSocket = socket;
}
}
PS。
我不知道是否需要知道,但是我正在做异步客户端/服务器。
解决方法
您的通信班级不必理会接收到的数据如何处理。相反,他们应该使数据可供需要它的类使用。一种方法是提供一个getData()方法,该方法接收数据,然后将其返回给调用方。更好的方法是提供一个DataArrived事件,该事件在您收到数据时就触发。这样,任何数量的使用者都可以侦听数据,但是您的通信代码不必知道正在侦听哪些类或他们打算如何处理数据。
编辑:
一个简单的例子:
public class MyClassWithEvent
{
public delegate void DataArrivedDelegate(string data);
public event DataArrivedDelegate DataArrived;
public void GetSomeData()
{
// Communication code goes here; stringData has the data
DataArrivedDelegate handler = DataArrived;
if (handler != null)
{
// If you want to raise the event on this thread,this is fine
handler(stringData);
}
}
}
在您的侦听器类中:
public MyListener
{
public MyListener(MyServer server)
{
// Sets MyListenerMethod to be called when DataArrived is raised
server.DataArrived += MyListenerMethod;
}
public void MyListenerMethod(string data)
{
// Do something with the data
Console.WriteLine(data);
}
}
, 用字符串触发事件?如果数据缓冲区字符串是套接字对象的成员,我将提供帮助-那么您可以仅使用套接字对象和事件处理程序来触发事件,无论结果如何,它都具有决定所需内容的一切处理数据。
Rgds,
马丁
, 我将创建一个接口,可以在其中编写事件信息,例如IEventSink。因此,OnDataReceived与IEventSink的实例一起使用,并在其上调用Write方法。
然后,我将有2个接口实现:一个在控制台上写,一个在表单上写。
, 您可以尝试实施策略
interface IOutputStrategy
{
void Output(string message);
}
class ConsoleOutput:IOutputStrategy
{
public void Output(string message)
{
Console.Writeline(message);
}
}
class FormOutput:IOutputStrategy
{
public void Output(string message)
{
// output where you want
}
}
在服务器/客户端上,您将拥有类型“ѭ5”
//Server
IOutputStrategy instance = new ConsoleOutput();
//Client
IOutputStrategy instance = new FormOutput();
然后在OnDataReceived回调中,您可以使用当前的IOutputStrategy实例输出消息
instance.Output(szData);
希望能帮助到你
, 这是依赖注入的良好位置。
创建一个具有字符串属性\“ ShowTheResult \”的接口(我们现在将其称为ISiemsen)
现在,在任何您希望能够显示结果的对象(窗体,控制台应用程序,Web窗体...)中,都可以实现ISiemsen界面
在\“ ShowTheResult \”的属性集内,将值分配给满足该对象需要的任何值。 (如果是控制台,则执行console.WriteLine(value),如果它是表单,则将值分配给文本框)
更改您的工作程序进程以接受对您的工作程序函数的引用,并使其将响应发送到需要的任何地方。
protected void getResults(ISiemsen siemens ... )
{
SocketPacket theSockId = (SocketPacket)asyn.AsyncState;
int iRx = theSockId.m_currentSocket.EndReceive(asyn);
char[] chars = new char[iRx + 1];
Decoder decode = Encoding.Default.GetDecoder();
int charLength = decode.GetChars(theSockId.dataBuffer,iRx,chars,0);
String szData = new String(chars);
WaitForData();
//Handle Message here
siemsen.ShowTheResult(theResult);
}
使用这种技术,UI元素不知道或不在乎数据的来源,而工作进程不知道或不在乎数据的去向……“松耦合”。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。