-2

So I am writing a C# program in which the server will encode a selected file into bytes and then send it to a client computer. However, for a reason I can't find, I am getting a "An object reference is required for the non-static field, method, or property 'Encoding.GetBytes(string)' " error, which is very odd as seen in the code below, nothing in the entire code is set to public or static so not sure why it would be generating this error? The line where Visual Studio throws the error is marked below. Any thoughts are appreciated. Thank you.

void SendFile_Click(object sender, EventArgs e)
        {

            try
            {
                FileSendProgress.Maximum = 100;
                IPAddress ipAd = IPAddress.Parse(ClientIP.Text); //use local m/c IP address, and use the same in the client

                /* Initializes the Listener */
                TcpListener myList = new TcpListener(ipAd, 8001);

                /* Start Listeneting at the specified port */
                myList.Start();

                MessageBox.Show("The server is running at port 8001...");
                MessageBox.Show("The local End point is  :" + myList.LocalEndpoint);
                MessageBox.Show("Looking for other computer");

                Socket s = myList.AcceptSocket();
                FSStatus.Text = "Connnected to client computer......Sending data now, hold on to your hats. " + FileSendProgress.Value.ToString();
                FileSendProgress.Value += 5;
                ASCIIEncoding asen = new ASCIIEncoding();
                s.Send(asen.GetBytes(DURL.Text)); // This is the line where the error is thrown.
                MessageBox.Show("The selected file : " + FileToSend.Text + "is now on it's way to the client computer. It may take a while if a large file is being sent.");

                byte[] b = new byte[100];
                int k = s.Receive(b);
                for (int i = 0; i < k; i++)
                    Console.Write(Convert.ToChar(b[i]));
                FileSendProgress.Value += 45;

                FSStatus.Text = "Encoding........ " + FileSendProgress.Value.ToString();
                /* clean up */



                byte[] fileContent = null;
                System.IO.FileStream fs = new System.IO.FileStream(FileToSend.Text, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(fs);
                long byteLength = new System.IO.FileInfo(FileToSend.Text).Length;
                fileContent = binaryReader.ReadBytes((Int32)byteLength);
                byte[] bb = new byte[1024];

                FSStatus.Text = "Transmitting now...... " + FileSendProgress.Value.ToString();
                string file = Encoding.UTF8.GetString(bb.AsSpan(0, k));
                FileSendProgress.Value += 15;
                s.Send(ASCIIEncoding.GetBytes(file));
                    fs.Close();
                    fs.Dispose();

                    binaryReader.Close();
                    s.Close();
                    myList.Stop(); 
                FileSendProgress.Value += 35;
             
                FSStatus.Text = "File Sent " + FileSendProgress.Value.ToString();


            }

                catch (Exception n)
                {
                    MessageBox.Show("Please make sure the file is selected, or the file is not supported. " + n);
                }
            
          
            }
Theoneguy
  • 13
  • 4
  • The error, "An object reference is required for the non-static field", is saying you are trying to call a property as if it is static but it's not defined as static. Basically, you need to reference an instance of the object. Based on your code and your comment on the error, my best guess is the way you are calling the `DURL.Text` property is the problem. Do you have an instance of the `DURL` object or are you trying to call the `Text` property without and instance? – quaabaam Dec 06 '22 at 18:32
  • If `DURL` is a type (a `class` or a `struct`) then you will need an object of this type: `DURL durl = new DURL();`. Then you can write `s.Send(asen.GetBytes(durl.Text));` – Olivier Jacot-Descombes Dec 06 '22 at 18:37
  • When you get an error it's really important to read the error message. The error message says you have a non-static something, but then you complain nothing is marked `static`. That is exactly the problem; you can't use a non-static thing as if it was static. As the other commenters have pointed out, it's probably `DURL`. You need to show us where that comes from as it's not defined anywhere in your code. – Dour High Arch Dec 07 '22 at 15:02

2 Answers2

0

GetBytes() is not a static method. You probably want to have:

asen.GetBytes(file)

instead of

ASCIIEncoding.GetBytes(file)
pawelfelcyn
  • 156
  • 5
0

Try Encoding.ASCII.GetBytes(file) instead of ASCIIEncoding.GetBytes(file).

CER
  • 224
  • 1
  • 3
  • However, I am attempting to encode the actual file itself, would this do that? – Theoneguy Dec 06 '22 at 20:33
  • It should work fine. I prefer `Encoding.ASCII` but instantiating `ASCIIEncoding` is fine too. See https://stackoverflow.com/questions/20880246/encoding-ascii-vs-new-asciiencoding and https://stackoverflow.com/questions/5636372/difference-between-asciiencoding-and-encoding for more info. – CER Dec 06 '22 at 21:22