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:
private void HandleClient(object _Client)
{
TcpClient _TcpClient = (TcpClient)_Client;
NetworkStream _ClientStream = _TcpClient.GetStream();
byte[] _Package = new byte[BufferSize];
int _BytesRead;
while (true)
{
_BytesRead = 0;
try
{
_BytesRead = _ClientStream.Read(_Package, 0, BufferSize);
}
catch (SocketException ex) { System.Windows.Forms.MessageBox.Show("Sorry !" + ex.Message); break; }
if (_BytesRead == 0)
{ break; }
if (IncomingPackage != null)
IncomingPackage(this, new IncomingPackageEvent(_TcpClient, _TcpClient.Client.RemoteEndPoint.ToString(), _Package));
}
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.
public void SendMessage( byte[] _Message)
{
if (this._TcpClient.Connected)
{
this._TcpClient.GetStream().Write(_Message, 0, _Message.Length);
}
}
I can't seem to find it. Anyone please help. :L