Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
// -- 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);
}


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:

C#
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();
        }


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.

C#
// -- 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);
    }
}


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

Fixed.

For some odd reason it does not send a message without a Thread.Sleep.
 
Share this answer
 
v2
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900