Click here to Skip to main content
12,948,566 members (62,247 online)
Rate this:
Please Sign up or sign in to vote.
See more:

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.
                        _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.

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("", 1337, 10025);
            _Client.Connected += new EventHandler<TCPClient.IncomingConnectionEvent>(_Client_Connected);
        void _Client_Connected(object sender, TCPClient.IncomingConnectionEvent e)
            MessageBox.Show("I do have connection !");
        void _Server_IncomingPackage(object sender, TCPServer.IncomingPackageEvent e)
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)

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);

I can't seem to find it. Anyone please help. :L
Posted 14-Dec-12 9:24am
Yvar Birx2.4K
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


_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.

Rate this: bad
Please Sign up or sign in to vote.

Solution 2


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

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 5,419
CHill60 3,275
Maciej Los 2,778
Jochen Arndt 1,935
ppolymorphe 1,795

Advertise | Privacy | Mobile
Web02 | 2.8.170524.1 | Last Updated 15 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100