如何纠正此代码以多服务器方式在服务器中工作 [英] How to correct this code to work in a server multiple client fashion

查看:77
本文介绍了如何纠正此代码以多服务器方式在服务器中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个非常简单的服务器客户端,我在发送多个消息时遇到问题

我想要做的是使用Monitor.wait / pulse序列化进程,两个线程用于听众和一个客户



的想法是每个应用程序都有客户端和服务器,服务器获取消息解析它,如果锁等待释放它,否则DoSomething

它有点长但我必须给它所以你可以编译它,如果你喜欢



我的问题是我可以发送只有一条消息在输掉之前发送,我认为问题是客户端但我不能确认它,我绝对需要你的帮助!



I've created a pretty simple server client and I have a problem sending multiple messages
what I want to do is to serialize the process using Monitor.wait/pulse and two threads one for the listener and one the client

the idea is each application has client and server , server get message parse it and if lock is waiting release it else "DoSomething"
it is a bit long but I have to give it so you would be able to compile it if you like

my problem is that I can send only one message before a lose abillty to send , I think that the problem is the client but I just cant confirm it , I despertly need your help !

Server Side :

            public void socketListener()
            {
                byte[] StreamMessage = new byte[9632];
                Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, 
ProtocolType.Tcp);
                IPEndPoint localEndPoint =new IPEndPoint(IPAddress.Any , 
 ControlLayer.GlobalParam.PEER2PEER_PORT);
                try
                {
                    listener.Bind(localEndPoint);
                    listener.Listen(10);
                    while (true)
                    {
                        Socket Handler = listener.Accept();
                        int MessageLength;
                        MessageLength = Handler.Receive(StreamMessage, 0, StreamMessage.Length, SocketFlags.None);
                        //return MessageLength;

                       // string message = System.Text.Encoding.Default.GetString(StreamMessage);
                        string message = System.Text.Encoding.UTF8.GetString(StreamMessage);

                        OnDataRecievedFromRemotePeer(this, message, "TcpServer");//send data to screen
                        ParseMessage(message , Handler);
                    }
                }
                catch
                {

                }
            }







OnDataRecievedFromRemotePeer

将数据发送到richtextbox的事件

event to send data to richtextbox

ParseMessage

接收消息并处理它

释放锁定这种时尚的东西

takes the message and process it
to release locks something of this fashion

//...more conditions
else if (message.Equals("HelloAck"))//relaese lock for Hello message
{
    peerSessionList.Find(x => x.remoteIP == remoteIpEndPoint.Address.ToString()).CommandReleaseLocker();
}
//.. more conditions





发布锁密码



release lock code

public void CommandReleaseLocker()
{
    lock (locker)
    {
        Monitor.PulseAll(locker);
    }

}





客户端代码:



每个客户都有自己的类:



Client Side Code :

each client has its own class:

public class PeerSession
{
    public readonly object locker = new object();
    public PeerSession(string RemoteIP)
    {
        remoteIP = RemoteIP;
        _infrastructure_TcpClientHandler = new TcpClientHandler();
        DomainSetPortIP(ControlLayer.GlobalParam.PEER2PEER_PORT, RemoteIP);
    }
    public TcpClientHandler _infrastructure_TcpClientHandler;

    public bool IsConnetcted = false;
    public string remoteIP = string.Empty;
    internal void DomainSetPortIP(int port, string IpAddressClient)
    {
        _infrastructure_TcpClientHandler.ComSettings.Port = port;
        _infrastructure_TcpClientHandler.ComSettings.IpAddress = IpAddressClient;
    }
    /// <summary>
    /// Write command to remote peer
    /// </summary>
    public void Write(string message)
    {
        Task task = new Task(() => CreateClient(message));
        task.Start();
    }
    private void CreateClient(object message)
    {
        _infrastructure_TcpClientHandler.SendMessage(message);
_infrastructure_TcpClientHandler.SendMessageViaStreamWriter(message+"\n");
    }
    public void CommandLocker (string Message)//TODO: add timeOut
    {
        lock (locker)
        {
            Thread.Sleep(1000);
            Write(Message);
            Monitor.Wait(locker);
        }
    }
    public void CommandReleaseLocker()
    {
        lock (locker)
        {
            Monitor.PulseAll(locker);
        }
    }
}



谢谢


thanks

推荐答案

服务器端代码,您将需要为每个客户端创建一个新线程 - 或使用异步调用来读取数据。



作为单个客户端的快速修复你可以移动Socket Handler = listener.Accept();在while(true)循环旁边,因为在每个读取数据块之后总是尝试接受新连接。
For the server side code you are going to need to create a new thread for each client - or use asynchronous calls to read the data.

As a quick fix for a single client you could move the Socket Handler = listener.Accept(); out side the while (true) loop, as you are always trying to accept a new connection after each block of data read.


这篇关于如何纠正此代码以多服务器方式在服务器中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆