8 Replies - 393 Views - Last Post: 30 November 2017 - 07:35 AM Rate Topic: -----

#1 Cartaphilus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-May 17

Encoding.cs Not Found Error

Posted 27 November 2017 - 02:38 PM

I'm trying to write a small application with VB Express 2015 that requires communicating with a remote server using the tcpclient class, which I am not at all familiar with. I picked up some examples from MSDN, but although I can get the code to compile, when I execute the following line, "Dim returndata As String = Encoding.ASCII.GetString(bytes)", I get a message that says "encoding.cs not found". I can't find any references on the web about what this error really means and how to correct it. Can anyone help me get past this?

Imports System.Net.Sockets
Imports System.Text.ASCIIEncoding
Public Class Form1
    Private server As String
    Private output As String
    Private newsgroup As ArrayList

    Private tcpClient As System.Net.Sockets.TcpClient
    Private networkStream As System.Net.Sockets.NetworkStream
    Private streamReader As System.IO.StreamReader

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        txtStatus.Text = "Connecting to server ... "
        tcpClient = New System.Net.Sockets.TcpClient
        tcpClient.Connect("news.supernews.com", 119)
        networkStream = tcpClient.GetStream()
        output = Read()
        TextBox1.Text = output
        txtStatus.Text = "finished."
    End Sub
    Private Function Read()

        If networkStream.CanRead Then
            ' Reads the NetworkStream into a byte buffer.
            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            ' Read can return anything from 0 to numBytesToRead. 
            ' This method blocks until at least one byte is read.
            networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))

            ' Returns the data received from the host to the console.
       ===> Dim returndata As String = ASCII.GetString(bytes) <===
            Console.WriteLine(("This is what the host returned to you: " + returndata))
        Else
            Console.WriteLine("You cannot read data from this stream.")
            tcpClient.Close()
        End If
        Return ("a")
    End Function
    Private Function write(message As String)
        'Dim myWriteBuffer As Byte() = Encoding.ASCII.GetBytes("Post")
        'networkStream.Write(myWriteBuffer, 0, myWriteBuffer.Length)
        Return ("a")
    End Function

End Class




TIA...

Is This A Good Question/Topic? 0
  • +

Replies To: Encoding.cs Not Found Error

#2 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 83
  • View blog
  • Posts: 364
  • Joined: 12-January 11

Re: Encoding.cs Not Found Error

Posted 28 November 2017 - 09:57 AM

Try replacing that line with this:

Dim returndata As String = System.Text.Encoding.ASCII.GetString(bytes)

I'm not familiar with the namespace System.Text.ASCIIEncoding, so that may be the issue. You can also try removing the ".ASCIIEncoding" and see if that gets you where you want to be.

This post has been edited by Psyguy: 28 November 2017 - 09:58 AM

Was This Post Helpful? 0
  • +
  • -

#3 Cartaphilus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-May 17

Re: Encoding.cs Not Found Error

Posted 28 November 2017 - 02:35 PM

View PostPsyguy, on 28 November 2017 - 09:57 AM, said:

Try replacing that line with this:

Dim returndata As String = System.Text.Encoding.ASCII.GetString(bytes)

I'm not familiar with the namespace System.Text.ASCIIEncoding, so that may be the issue. You can also try removing the ".ASCIIEncoding" and see if that gets you where you want to be.


Thanks, Psyguy, but no change...
Was This Post Helpful? 0
  • +
  • -

#4 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1491
  • View blog
  • Posts: 3,772
  • Joined: 01-February 13

Re: Encoding.cs Not Found Error

Posted 28 November 2017 - 02:49 PM

I agree with Psyguy, try using the System.Text.Encoding namespace instead of the System.Text.ASCIIEncoding namespace. I have never really used ASCIIEncoding and have never seen such an exception as that so, it may be something with the ASCIIEncoding class. It just seems strange it would tell you it can't find a C# file which is what the .cs file extension is used for.

Anyways, i tried your code after fixing the Functions and it seemed to work fine for me. After connecting, it received a string message saying "200 Supernews server ready".

I would suggest using the three lines i put at the top of the example below to stop you from making some of the beginner mistakes in your code. One such as not declaring the return type of your functions. If you do not need to return anything from a function, then it should be a Sub instead of a Function.

Option Strict On
Option Explicit On
Option Infer Off

Imports System.Net.Sockets
Imports System.Text.Encoding

Public Class Form1
    Private output As String

    Private tcpClient As TcpClient
    Private networkStream As NetworkStream

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        txtStatus.Text = "Connecting to server ... "
        tcpClient = New TcpClient
        tcpClient.Connect("news.supernews.com", 119)
        networkStream = tcpClient.GetStream()
        output = Read()
        TextBox1.Text = output
        txtStatus.Text = "finished."

        'don't forget to close/dispose these objects when you are done reading/writing to and from the connection
        tcpClient.Close()
        networkStream.Dispose()
    End Sub

    Private Function Read() As String
        Dim returndata As String = ""
        If networkStream.CanRead Then
            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, tcpClient.ReceiveBufferSize)
            returndata = ASCII.GetString(bytes)
        Else
            returndata = "You cannot read data from this stream."
            tcpClient.Close()
        End If
        Return returndata
    End Function
End Class



Quote

Thanks, Psyguy, but no change...


You are creating a Windows Form application, right?

What .Net framework are you targeting?

After looking real quick, there is a "Encoding.cs" file in the .Net Framework source code which i was thinking the exception might be talking about. Why the file can't be found is another story.
Was This Post Helpful? 1
  • +
  • -

#5 Cartaphilus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-May 17

Re: Encoding.cs Not Found Error

Posted 29 November 2017 - 01:16 PM

Thanks very much for the help so far. I'm not actually a beginner, I'm just a bit sloppy when I program for a hobby and also not that familiar with structuring code. I actually used to bang bits for a living but (really!) it was so long ago that I wrote in assembly language for the various machines I worked on.

Yes, it is a Windows form application, and my .net framework is 4.7.

If I could continue to depend on your expertise, could you show me how to add a routine to that code that would write a passed string to the network stream? Again, all the examples I can find use the dreaded 'encoding' word.

Many thanks

This post has been edited by andrewsw: 29 November 2017 - 01:23 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • blow up my boots
  • member icon

Reputation: 6541
  • View blog
  • Posts: 26,483
  • Joined: 12-December 12

Re: Encoding.cs Not Found Error

Posted 29 November 2017 - 01:23 PM

There is no need to quote the (large) previous post, there is a Reply button further down the page, or the Fast Reply box.
Was This Post Helpful? 0
  • +
  • -

#7 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1491
  • View blog
  • Posts: 3,772
  • Joined: 01-February 13

Re: Encoding.cs Not Found Error

Posted 29 November 2017 - 04:45 PM

So... Did my example work for you? If so, then your code for writing to the stream should work as is. Maybe just remove 'Encoding.' from the line below.
Dim myWriteBuffer As Byte() = Encoding.ASCII.GetBytes("Post")


A few other notes...

You may want to change your Function to a Sub if there is nothing that it should return, like maybe if you use the NetStream.CanWrite method in the Sub or Function and it returns True or False.

Perhaps if you need to call the Sub or Function more than once throughout your code and you want to send different String messages each time, then you could add a parameter or two to the Sub or Function for passing the String message and whatever else into it.

You will also want to move the Closing and Disposing methods i showed in the end of the form Load event to elsewhere so the connection is not closed/disposed until after you are done reading and writing to and from the stream. Maybe put those two lines in the Form Closing event for now.

As far as what kind of String messages you would need to Write to the stream or what format they should be in, is something i have no knowledge of.
Was This Post Helpful? 0
  • +
  • -

#8 Cartaphilus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-May 17

Re: Encoding.cs Not Found Error

Posted 30 November 2017 - 05:44 AM

But that gets me back to the original problem. Executing either

Dim myWriteBuffer As Byte() = Encoding.ASCII.GetBytes("Post")

or

Dim myWriteBuffer As Byte() = ASCII.GetBytes("Post")

returns the Encoding.cs not found"
Was This Post Helpful? 0
  • +
  • -

#9 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1491
  • View blog
  • Posts: 3,772
  • Joined: 01-February 13

Re: Encoding.cs Not Found Error

Posted 30 November 2017 - 07:35 AM

Again, did my example for reading from the stream work for you?

Have you changed your Import statements as is shown in my example?

If you have used my whole example code exactly as i showed it and the Read method works, then i see no good reason that the line you pointed out would give this exception.

I tried the Write below added into my example code and it wrote to the stream for me with no exceptions...

Imports System.Net.Sockets
Imports System.Text.Encoding

Public Class Form1
    Private output As String

    Private tcpClient As TcpClient
    Private networkStream As NetworkStream

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        txtStatus.Text = "Connecting to server ... "
        tcpClient = New TcpClient
        tcpClient.Connect("news.supernews.com", 119)
        networkStream = tcpClient.GetStream()
        output = Read()
        TextBox1.Text = output
        txtStatus.Text = "finished."
    End Sub

    Private Function Read() As String
        Dim returndata As String = ""
        If networkStream.CanRead Then
            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, tcpClient.ReceiveBufferSize)
            returndata = ASCII.GetString(bytes)
        Else
            returndata = "You cannot read data from this stream."
            tcpClient.Close()
        End If
        Return returndata
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Write("Post")
    End Sub

    Private Sub Write(message As String)
        If networkStream.CanWrite Then
            Try
                Dim myWriteBuffer As Byte() = ASCII.GetBytes(message)
                networkStream.Write(myWriteBuffer, 0, myWriteBuffer.Length)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        Else
            MessageBox.Show("Can Not Write To The Stream.")
        End If
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        'don't forget to close/dispose these objects when you are done reading/writing to and from the connection
        tcpClient.Close()
        networkStream.Dispose()
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1