Hello!

I have a problem with my C# Client/Server network program. This problem only occurs when connecting via an IP over the internet, whereas everything works fine using a local address (127.0.0.1).

What happens is while transferring a file (10kb or more maybe) using the below code, my receiving program (Client if downloading from server) freezes, on closer inspection I have narrowed it down to the below methods. One of the symptoms of this is that the hash code of byte being sent is mismatched.

Here is the base code:

Writing method:

        public void WriteFrame112_GUI(FileStream f)
        {
            try
            {
                SC.WriteByte(112);

                SC.WriteInteger((int)f.Length);

                for (int i = 0; i < f.Length; i++)
                {
                    if (i % 10 == 0)
                        Application.DoEvents();

                    SC.WriteByte((byte)f.ReadByte());
                }
            }
            catch (Exception e) { MessageBox.Show(e.Message); }
        }

Reading method:
Please note: The first byte (In this case, 112) has already been read before the reading method is called.

        public byte[] ReadFrame112_GUI()
        {
            try
            {
                int filelen = SC.ReadInteger();

                byte[] data = new byte[filelen];
                for (int i = 0; i < filelen; i++)
                {
                    if (i % 10 == 0)
                        Application.DoEvents();

                    data[i] = SC.ReadByte();
                }

                return data;
            }
            catch (Exception e) { MessageBox.Show(e.Message); return null; }
        }

These both in turn call:

    public void WriteHash(object Obj)
    {
        int hInt = Obj.GetHashCode();

        byte[] Buffer = new byte[4];
        int Offset = 0;

        Buffer[Offset++] = (byte)(hInt >> 24);
        Buffer[Offset++] = (byte)(hInt >> 16);
        Buffer[Offset++] = (byte)(hInt >> 8);
        Buffer[Offset++] = (byte)(hInt);

        NS.Write(Buffer, 0, 4);
    }

    public int ReadHash()
    {
        while (!NS.DataAvailable) ; // Wait for data

        byte[] Buffer = new byte[4];
        int Offset = 0;

        NS.Read(Buffer, Offset, Buffer.Length);

        int Val = (int)(Buffer[0] << 24) + (Buffer[1] << 16) + (Buffer[2] << 8) + (Buffer[3]);

        return Val;
    }

    public void WriteByte(byte b)
    {
        WriteHash(b);
        NS.WriteByte(b);
    }

    public byte ReadByte()
    {
        int Hash = ReadHash();

        while (!NS.DataAvailable) ; // Wait for data
        byte Val = (byte)NS.ReadByte();

        if (Hash != Val.GetHashCode())
        {
            throw new Exception("Hash Code Mismatch Exception (byte): " + Hash + ":" + Val.GetHashCode());
        }

        return Val;
    }

I suspect your problem is in ReadHash. While you check if there is data, you don't check if there is enough data. You should make sure there are 4 bytes available for reading before you attempt to read 4 bytes.

Thank you very much. I should have checked that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.