CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2002
    Location
    .NET 2.0/.NET 3.0/.NET 3.5 VS2005/VS2008
    Posts
    284

    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[] dataint offsetint count)
    {
        
    int index;

        for(
    index offsetindex offset countindex++)
        {
            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?

  2. #2
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    what are you receiving in stringbuilder.
    do you recieve something atleast ?

    Paresh
    - Software Architect

  3. #3
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    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

  4. #4
    Join Date
    Jul 2002
    Location
    .NET 2.0/.NET 3.0/.NET 3.5 VS2005/VS2008
    Posts
    284
    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
  •  





Click Here to Expand Forum to Full Width

Featured