C#(TCP)套接字异常还是? [英] C# (TCP) Socket Exception or ?

查看:64
本文介绍了C#(TCP)套接字异常还是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,



过去一小时我一直试图解决这个问题。我希望有人可以指出我的错误。基本上,我有一个简单的TCPClient处理程序:



Alright,

I have been trying to fix this issue for the past hour. I hope someone could point me out the error. Basically, I have a simple TCPClient handler:

// -- Handle a client.
/// <summary>
/// Handles a client, should be called by a thread.
/// </summary>
/// <param name="_Client">The client to be handled.</param>
private void HandleClient(object _Client)
{
    // -- Convert the object client to a TCPClient.
    TcpClient _TcpClient = (TcpClient)_Client;

    // -- Create a stream.
    NetworkStream _ClientStream = _TcpClient.GetStream();

    // -- The message.
    byte[] _Package = new byte[BufferSize];

    // -- Bytes read.
    int _BytesRead;

    // -- Loop.
    while (true)
    {
            // -- Clear the buffer.
            _BytesRead = 0;

            // -- Read the message.
            try
            {
                _BytesRead = _ClientStream.Read(_Package, 0, BufferSize);
            }
            catch (SocketException ex) { System.Windows.Forms.MessageBox.Show("Sorry !" + ex.Message); break; }

            // -- Disconnected.
            if (_BytesRead == 0)
            { break; }

            // -- Message Succesfully sent.
            if (IncomingPackage != null)
                IncomingPackage(this, new IncomingPackageEvent(_TcpClient, _TcpClient.Client.RemoteEndPoint.ToString(), _Package));


    }

    // -- Close the client.
    StopClient(_TcpClient);
}





然后,这是有效的。

我的意思是,它确实在try {_BytesRead ....之前显示一个消息框。

但是当我尝试在阅读后显示一个消息框时,它没有显示消息框。消息框也没有抱歉。所以,我的想法是没有错误。那么,它可能是由客户引起的?不,我的表格中有这个代码:





Then, this works.
I mean, it does show a message box before "try { _BytesRead ....".
But when I try to show a message box after the read, it doesn''t show the message box. Neither does the message box "sorry appear". So, my idea is that there is no error. So, It might be caused by the client ? No, I have this code in my Form:

TCPServer _Server;
        TCPClient _Client;
        private void Form1_Load(object sender, EventArgs e)
        {
            _Server = new TCPServer(1337, 10025);
            _Server.IncomingPackage += new EventHandler<TCPServer.IncomingPackageEvent>(_Server_IncomingPackage);

            _Client = new TCPClient("127.0.0.1", 1337, 10025);
            _Client.Connected += new EventHandler<TCPClient.IncomingConnectionEvent>(_Client_Connected);

            _Client.SendMessage(Encoding.ASCII.GetBytes("LOLDERPHERPMERP"));
            
           
        }

        void _Client_Connected(object sender, TCPClient.IncomingConnectionEvent e)
        {
            MessageBox.Show("I do have connection !");
        }

        void _Server_IncomingPackage(object sender, TCPServer.IncomingPackageEvent e)
        {
            MessageBox.Show("");
            MessageBox.Show(Encoding.ASCII.GetString(e.Package));
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            _Client.StopClient();
            _Server.StopServer();
        }





弹出我有连接的消息框,但是空消息框或解码消息那并不。这意味着事件不会被触发,错误发生在try {_BytesRead和SocketException之间。



如果你想知道我发送的方式消息,这是无效的。





The message box saying "I do have connection" pops up, but the empty message box or the decoded message doesn''t. So that means the event doesn''t get fired and the error is between "try{ _BytesRead" and "SocketException".

If you want to know how I send the message, this is the void.

// -- Send a message to the server.
/// <summary>
/// Sends a message to the server.
/// </summary>
/// <param name="_Message">The message to be sent..</param>
public void SendMessage( byte[] _Message)
{
    // -- Send the message.
    if (this._TcpClient.Connected)
    {
        this._TcpClient.GetStream().Write(_Message, 0, _Message.Length);
    }
}





我似乎无法找到它。有人请帮忙。 :L



I can''t seem to find it. Anyone please help. :L

推荐答案





首先,
Hi,

Firstly,
_BytesRead = _ClientStream.Read(_Package, 0, BufferSize);

是一个阻塞函数。这意味着一旦到达那里,它将等到客户端发送数据。因此,您需要检查是否达到

is a blocking function. This means once you reached there it will wait till a client sends the data. So you need to check whether you reach

if (_BytesRead == 0)

。如果没有那么你必须检查

or not. If not then you have to check whether

this._TcpClient.GetStream().Write(_Message, 0, _Message.Length);

是否正确执行。还尝试在具有适当关键部分的单独线程中处理阻塞函数。



问候。

executes correctly or not. Also try to handle blocking functions in a separate thread with proper critical sections.

Regards.


已修复。



由于某些奇怪的原因,它没有发送没有Thread.Sleep的消息。
Fixed.

For some odd reason it does not send a message without a Thread.Sleep.


这篇关于C#(TCP)套接字异常还是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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