|
-
March 24th, 2003, 08:17 AM
#1
Network receiving problem
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
PHP Code:
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 ?
WM.
What about weapons of mass construction?
-
March 25th, 2003, 04:04 PM
#2
what are you receiving in stringbuilder.
do you recieve something atleast ?
Paresh
- Software Architect
-
March 25th, 2003, 04:09 PM
#3
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
- Software Architect
-
March 26th, 2003, 08:10 AM
#4
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.
WM.
What about weapons of mass construction?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|