TCP/IP connection Error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • id10t error

    TCP/IP connection Error

    Hello,

    I am trying to write soem code that will send data to an Oneil printer
    over our network. I know the printer works and is setup correctly
    because I can print to it from there software. In my program i am
    getting this error.

    System.IO.IOExc eption was unhandled
    Message="Unable to read data from the transport connection: An
    existing connection was forcibly closed by the remote host."

    I will post my code below. Any help would be great.

    Imports System.Net.Sock ets
    Imports System.Text
    Public Class Form2

    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
    As System.EventArg s) Handles Button1.Click
    Dim tcpClient As New System.Net.Sock ets.TcpClient()
    'tcpClient.Conn ect(hostname, port)
    tcpClient.Conne ct("10.0.9.106" , 515)
    Dim networkStream As NetworkStream = tcpClient.GetSt ream()
    If networkStream.C anWrite And networkStream.C anRead Then
    ' Do a simple write.
    Dim sendBytes As [Byte]() =
    Encoding.ASCII. GetBytes("EZ{P RINT,STOP350:@2 0,35:ZP08A|003| ")
    networkStream.W rite(sendBytes, 0, sendBytes.Lengt h)
    ' Read the NetworkStream into a byte buffer.
    Dim bytes(tcpClient .ReceiveBufferS ize) As Byte
    networkStream.R ead(bytes, 0,
    CInt(tcpClient. ReceiveBufferSi ze))' --- this is the line giving me
    the error
    ' Output the data received from the host to the console.
    Dim returndata As String = Encoding.ASCII. GetString(bytes )
    Console.WriteLi ne(("Host returned: " + returndata))
    Else
    If Not networkStream.C anRead Then
    Console.WriteLi ne("cannot not write data to this
    stream")
    tcpClient.Close ()
    Else
    If Not networkStream.C anWrite Then
    Console.WriteLi ne("cannot read data from this
    stream")
    tcpClient.Close ()
    End If
    End If
    End If
    ' pause so user can view the console output
    Console.ReadLin e()
    End Sub

    End Class
Working...