Results 1 to 11 of 11

Thread: TCP - Handling Data

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    TCP - Handling Data

    Hello, I am attempting to write a Remote Assistance program.

    I am trying to debug my program to see what the image is not being displayed on the 'server'.

    Server = Sends Screen Shots of User's Screen
    Client = Receives Screen Shots and Displays them in a Picture Box

    I am trying to place a break point on the ProcessData() line but it is not breaking on it. I don't believe any data is being passed as the paremeter for ProcessData (hence the empty image).

    Here is the code to read data.
    Code:
    private void DoListen()
            {
                // Buffer for reading data
                Byte[] bytes = new Byte[256];
                List<Byte> byteList = new List<byte>();
                String data = null;
    
                // Enter the listening loop.
                while (remainActive && client == null)
                {
                    Debug.WriteLine("Waiting for a connection... ");
    
                    // Perform a blocking call to accept requests.
                    // You could also user server.AcceptSocket() here.
                    client = server.AcceptTcpClient();
                    Debug.WriteLine("Connected!");
    
                    data = null;
    
                    // Get a stream object for reading and writing
                    stream = client.GetStream();
    
                    int i = 0;
    
                    // Loop to receive all the data sent by the client.
                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        // Translate data bytes to a ASCII string.
                        //data += System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                        byteList.AddRange(bytes);
                        Debug.WriteLine("Received: {0}", data);
                    }
                    ProcessData(byteList.ToArray());
                    // Shutdown and end connection
                    client.Close();
                    //remainActive = false;
                }
            }
    I believe byteList is empty when passed to ProcessData after the second internal while loop.

    What's wrong?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,335

    Re: TCP - Handling Data

    I can see one issue straight away. This:
    Code:
    byteList.AddRange(bytes);
    is going to add the entire array, but you may not have filled the entire array. You only want the first 'i' elements. Assuming .NET 3.5 or later, you could change it to this:
    Code:
    byteList.AddRange(bytes.Take(i));
    The fact that a breakpoint is not being hit is worrisome though. Have you tried putting a Debug.WriteLine call in there?

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: TCP - Handling Data

    When I check Properties then Target Framework it says .NET v3.5 however 'Take' does not exist.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,335

    Re: TCP - Handling Data

    Take is an extension method declared in the Enumerable class. As such, you will need to have referenced System.Core.dll and imported System.Linq.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: TCP - Handling Data

    I have modified my code replacing the byteList.AddRange line as you said.

    I have placed a Debug.WriteLine after the while loop and it is not being executed. The program is simply skipping all the code after the end of the interior loop.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,335

    Re: TCP - Handling Data

    Ah, didn;t notice those Debug.WriteLine calls. Have you tried setting a breakpoint at the top of the code and stepping through it? There are only two explanations that I can think of that execution would jump from that line:

    1. Your compiled code doesn't match your source.

    2. An exception is being thrown.

    Have you tried adding an exception handler?

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: TCP - Handling Data

    Here is my latest code, the issue is still present.
    Code:
    private void DoListen()
            {
                try
                {
                    // Buffer for reading data
                    Byte[] bytes = new Byte[256];
                    List<Byte> byteList = new List<byte>();
                   
                    // Enter the listening loop.
                    while (remainActive && client == null)
                    {
                        Debug.WriteLine("Waiting for a connection... ");
    
                        client = server.AcceptTcpClient();
                        Debug.WriteLine("Connected!");
    
                      
                        // Get a stream object for reading and writing
                        stream = client.GetStream();
    
                        int i = 0;
                        // Loop to receive all the data sent by the client.
                        while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            byteList.AddRange(bytes.Take(i));
                            Debug.WriteLine(byteList.Count.ToString());
                        }
                        Debug.WriteLine("You got here!!");
                        ProcessData(byteList.ToArray());
                        // Shutdown and end connection
                        client.Close();
                        //remainActive = false;
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    throw ex;
                }
            }
    Debug.WriteLine("You got here!!"); isn't being executed. The Debug.WriteLine(byteList.Count.ToString()) is working. Once I see the last byte I see this message in the Output window:
    The thread 0x1578 has exited with code 0 (0x0).
    Instead of seeing the Debug.WriteLine("You got here!!"); like I should.

    I've tried everything you've said so far, without luck.

    EDIT: No exception is being caught.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,335

    Re: TCP - Handling Data

    I'm afraid that's a bit of a mystery. Have you tried stepping through the code form the top? If you add a breakpoint below that loop, does the IDE indicate that it won't be hit? Have you tried running this on a different machine? Have you tried deleting the contents of your 'bin' folder and rebuilding?

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: TCP - Handling Data

    Is it possible that something is wrong on the Server side? Is there something I am supposed to do to terminate the connection? - Perhaps the connections is not being terminated once all the data has been read.

    Here is the server side that sends the data:
    Code:
     public void SendMessage(byte[] data)
            {
                   
                stream = client.GetStream();
    
                // Send the message to the connected TcpServer. 
                stream.Write(data, 0, data.Length);
    
                Debug.WriteLine(String.Format("Sent: {0}", data.ToString()));
    
                // Receive the TcpServer.response.
    
                // Buffer to store the response bytes.
                data = new Byte[256];
    
                // String to store the response ASCII representation.
                String responseData = String.Empty;
    
                // Read the first batch of the TcpServer response bytes.
                Int32 bytes = stream.Read(data, 0, data.Length);
                responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                Debug.WriteLine(responseData);
    
                // Close everything.
                stream.Close();
                client.Close();
            }
    Also - as an unrelated question - Why isn't myByte.ToString("XX"); working? Doesn't that return the text in hexadecimal format?

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,335

    Re: TCP - Handling Data

    If there's an issue on the server side then that might lead to issues on the client side, but those issues should be diagnosable using the usual techniques. The fact that your code appears to simply be ignored shouldn't be the case regardless, so there's something seriously not right going on.

    If you want a 2-digit, upper-case, hexadecimal representation of a Byte then the format string is "X2".

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: TCP - Handling Data

    I create two new projects in two new separate solutions, one for the client and one for the server. Before both projects were in the same Solution. The program still does not work.

    I thought I may be able to diagnose the error by seeing what information is being communicated between the two programs. I have a couple packet sniffers on my computer but they don't pickup programs communicating through the localhost. Do you know of a means to see what if any information is being passed between the programs?

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