Click to See Complete Forum and Search --> : Network receiving problem


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 ?

pareshgh
March 25th, 2003, 03:04 PM
what are you receiving in stringbuilder.
do you recieve something atleast ?

Paresh

pareshgh
March 25th, 2003, 03:09 PM
addition to previous post

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

}

}

}

don't know what you are doing in TextReceived,
but on each character why are you creating new stringbuilder.


and further
it will only go in side if it found return or enter char and do
builder.Append(data[index]);

-Paresh

WillemM
March 26th, 2003, 07:10 AM
LOL, if the character is not 13 or 10 then it should be appended to the stringbuilder. If it is 13 or 10 then the stringbuilder is returned to the event TextReceived and a new stringbuilder is being made.

If i'm right this is the correct code....

if(data[index] != 10.....

That means it adds each character not equal to 13 or 10.