Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#1.0 C#2.0 C#3.0 C#4.0
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);
        }
 
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();
        }
 
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
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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

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



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