WillemM
March 24th, 2003, 07:17 AM
Help, I got a very difficult problem.... I don't see the error anymore in this code. It connects fine, it sends fine. But when it comes to receiving it's useless :P
public event OnTextReceived TextReceived;
public event OnConnected Connected;
public delegate void OnTextReceived(string text);
public delegate void OnConnected();
public void Connect()
{
try
{
client = new TcpClient(server,port);
client.GetStream().BeginRead(marData,0,1024,new AsyncCallback(ResponseCallBack),null);
pingSender = new Thread(new ThreadStart(PingRun));
pingSender.Start();
Connected();
}
catch(Exception ex)
{
throw new Exception("Can't connect (" + ex.Message + " " + ex.Source + ")");
}
}
private void ResponseCallBack(IAsyncResult ar)
{
int count;
try
{
count = client.GetStream().EndRead(ar);
if(count < 1)
{
pingSender.Abort();
Disconnected();
throw new Exception("Connection lost");
}
BuildString(marData,0,count);
client.GetStream().BeginRead(marData,0,1024,new AsyncCallback(ResponseCallBack),null);
}
catch(Exception ex)
{
throw ex;
}
}
public void BuildString(byte[] data, int offset, int count)
{
int index;
for(index = offset; index < offset + count; index++)
{
if(data[index] != 10 && data[index] != 13)
{
builder.Append(data[index]);
}
else
{
TextReceived(builder.ToString());
builder = new StringBuilder();
}
}
}
Paresh or anyone else, can you see the problem ?
public event OnTextReceived TextReceived;
public event OnConnected Connected;
public delegate void OnTextReceived(string text);
public delegate void OnConnected();
public void Connect()
{
try
{
client = new TcpClient(server,port);
client.GetStream().BeginRead(marData,0,1024,new AsyncCallback(ResponseCallBack),null);
pingSender = new Thread(new ThreadStart(PingRun));
pingSender.Start();
Connected();
}
catch(Exception ex)
{
throw new Exception("Can't connect (" + ex.Message + " " + ex.Source + ")");
}
}
private void ResponseCallBack(IAsyncResult ar)
{
int count;
try
{
count = client.GetStream().EndRead(ar);
if(count < 1)
{
pingSender.Abort();
Disconnected();
throw new Exception("Connection lost");
}
BuildString(marData,0,count);
client.GetStream().BeginRead(marData,0,1024,new AsyncCallback(ResponseCallBack),null);
}
catch(Exception ex)
{
throw ex;
}
}
public void BuildString(byte[] data, int offset, int count)
{
int index;
for(index = offset; index < offset + count; index++)
{
if(data[index] != 10 && data[index] != 13)
{
builder.Append(data[index]);
}
else
{
TextReceived(builder.ToString());
builder = new StringBuilder();
}
}
}
Paresh or anyone else, can you see the problem ?