TCP/IP

Discussion in 'ASP .Net' started by id10t error, May 22, 2008.

  1. id10t error

    id10t error Guest

    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.IOException 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.Sockets
    Imports System.Text
    Public Class Form2


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
    As System.EventArgs) Handles Button1.Click
    Dim tcpClient As New System.Net.Sockets.TcpClient()
    'tcpClient.Connect(hostname, port)
    tcpClient.Connect("10.0.9.106", 515)
    Dim networkStream As NetworkStream = tcpClient.GetStream()
    If networkStream.CanWrite And networkStream.CanRead Then
    ' Do a simple write.
    Dim sendBytes As [Byte]() =
    Encoding.ASCII.GetBytes(" EZ{PRINT,STOP350:mad:20,35:ZP08A|003|")
    networkStream.Write(sendBytes, 0, sendBytes.Length)
    ' Read the NetworkStream into a byte buffer.
    Dim bytes(tcpClient.ReceiveBufferSize) As Byte
    networkStream.Read(bytes, 0,
    CInt(tcpClient.ReceiveBufferSize))' ---> 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.WriteLine(("Host returned: " + returndata))
    Else
    If Not networkStream.CanRead Then
    Console.WriteLine("cannot not write data to this
    stream")
    tcpClient.Close()
    Else
    If Not networkStream.CanWrite Then
    Console.WriteLine("cannot read data from this
    stream")
    tcpClient.Close()
    End If
    End If
    End If
    ' pause so user can view the console output
    Console.ReadLine()
    End Sub


    End Class
    id10t error, May 22, 2008
    #1
    1. Advertising

  2. You code and words contradict each other...
    You said you want to send something to printer.
    But you code says networkStream.Read as if you trying to read something from
    printer.


    Here is the code i am using


    public bool SendBytesToPrinter(byte[] p)
    {
    try
    {
    IPEndPoint ipEndPoint;
    ipEndPoint = new IPEndPoint(IPAddress.Parse(_sName), 9100);
    Socket socket;
    using (socket = new
    Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp))
    {
    socket.Connect(ipEndPoint);
    socket.Send(p);
    socket.Close();
    }
    return true;
    }
    catch (Exception e)
    {
    //TODO: do something with exception
    return false;
    }
    }


    George


    "id10t error" <> wrote in message
    news:...
    > 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.IOException 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.Sockets
    > Imports System.Text
    > Public Class Form2
    >
    >
    > Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
    > As System.EventArgs) Handles Button1.Click
    > Dim tcpClient As New System.Net.Sockets.TcpClient()
    > 'tcpClient.Connect(hostname, port)
    > tcpClient.Connect("10.0.9.106", 515)
    > Dim networkStream As NetworkStream = tcpClient.GetStream()
    > If networkStream.CanWrite And networkStream.CanRead Then
    > ' Do a simple write.
    > Dim sendBytes As [Byte]() =
    > Encoding.ASCII.GetBytes(" EZ{PRINT,STOP350:mad:20,35:ZP08A|003|")
    > networkStream.Write(sendBytes, 0, sendBytes.Length)
    > ' Read the NetworkStream into a byte buffer.
    > Dim bytes(tcpClient.ReceiveBufferSize) As Byte
    > networkStream.Read(bytes, 0,
    > CInt(tcpClient.ReceiveBufferSize))' ---> 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.WriteLine(("Host returned: " + returndata))
    > Else
    > If Not networkStream.CanRead Then
    > Console.WriteLine("cannot not write data to this
    > stream")
    > tcpClient.Close()
    > Else
    > If Not networkStream.CanWrite Then
    > Console.WriteLine("cannot read data from this
    > stream")
    > tcpClient.Close()
    > End If
    > End If
    > End If
    > ' pause so user can view the console output
    > Console.ReadLine()
    > End Sub
    >
    >
    > End Class
    >
    >
    George Ter-Saakov, May 22, 2008
    #2
    1. Advertising

  3. id10t error

    id10t error Guest

    On May 22, 2:00 pm, "George Ter-Saakov" <> wrote:
    > You code and words contradict each other...
    > You said you want to send something to printer.
    > But you code says networkStream.Read as if you trying to read something from
    > printer.
    >
    > Here is the code i am using
    >
    > public bool SendBytesToPrinter(byte[] p)
    > {
    >     try
    >     {
    >         IPEndPoint ipEndPoint;
    >         ipEndPoint = new IPEndPoint(IPAddress.Parse(_sName), 9100);
    >         Socket socket;
    >         using (socket = new
    > Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp))
    >         {
    >             socket.Connect(ipEndPoint);
    >             socket.Send(p);
    >             socket.Close();
    >         }
    >         return true;
    >     }
    >     catch (Exception e)
    >     {
    >         //TODO: do something with exception
    >         return false;
    >     }
    >
    > }
    >
    > George
    >
    > "id10t error" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > 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.IOException 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.Sockets
    > > Imports System.Text
    > > Public Class Form2

    >
    > >    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
    > > As System.EventArgs) Handles Button1.Click
    > >        Dim tcpClient As New System.Net.Sockets.TcpClient()
    > >        'tcpClient.Connect(hostname,  port)
    > >        tcpClient.Connect("10.0.9.106", 515)
    > >        Dim networkStream As NetworkStream = tcpClient.GetStream()
    > >        If networkStream.CanWrite And networkStream.CanRead Then
    > >            ' Do a simple write.
    > >            Dim sendBytes As [Byte]() =
    > > Encoding.ASCII.GetBytes(" EZ{PRINT,STOP350:mad:20,35:ZP08A|003|")
    > >            networkStream.Write(sendBytes, 0, sendBytes.Length)
    > >            ' Read the NetworkStream into a byte buffer.
    > >            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
    > >            networkStream.Read(bytes, 0,
    > > CInt(tcpClient.ReceiveBufferSize))' --->  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.WriteLine(("Host returned: " + returndata))
    > >        Else
    > >            If Not networkStream.CanRead Then
    > >                Console.WriteLine("cannot not write data to this
    > > stream")
    > >                tcpClient.Close()
    > >            Else
    > >                If Not networkStream.CanWrite Then
    > >                    Console.WriteLine("cannot read data from this
    > > stream")
    > >                    tcpClient.Close()
    > >                End If
    > >            End If
    > >        End If
    > >        ' pause so user can view the console output
    > >        Console.ReadLine()
    > >    End Sub

    >
    > > End Class- Hide quoted text -

    >
    > - Show quoted text -


    How can i convert this to VB?
    id10t error, May 22, 2008
    #3
  4. id10t error

    id10t error Guest

    On May 22, 2:14 pm, id10t error <> wrote:
    > On May 22, 2:00 pm, "George Ter-Saakov" <> wrote:
    >
    >
    >
    >
    >
    > > You code and words contradict each other...
    > > You said you want to send something to printer.
    > > But you code says networkStream.Read as if you trying to read something from
    > > printer.

    >
    > > Here is the code i am using

    >
    > > public bool SendBytesToPrinter(byte[] p)
    > > {
    > >     try
    > >     {
    > >         IPEndPoint ipEndPoint;
    > >         ipEndPoint = new IPEndPoint(IPAddress.Parse(_sName), 9100);
    > >         Socket socket;
    > >         using (socket = new
    > > Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp))
    > >         {
    > >             socket.Connect(ipEndPoint);
    > >             socket.Send(p);
    > >             socket.Close();
    > >         }
    > >         return true;
    > >     }
    > >     catch (Exception e)
    > >     {
    > >         //TODO: do something with exception
    > >         return false;
    > >     }

    >
    > > }

    >
    > > George

    >
    > > "id10t error" <> wrote in message

    >
    > >news:...

    >
    > > > 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.IOException 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.Sockets
    > > > Imports System.Text
    > > > Public Class Form2

    >
    > > >    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
    > > > As System.EventArgs) Handles Button1.Click
    > > >        Dim tcpClient As New System.Net.Sockets.TcpClient()
    > > >        'tcpClient.Connect(hostname,  port)
    > > >        tcpClient.Connect("10.0.9.106", 515)
    > > >        Dim networkStream As NetworkStream = tcpClient.GetStream()
    > > >        If networkStream.CanWrite And networkStream.CanRead Then
    > > >            ' Do a simple write.
    > > >            Dim sendBytes As [Byte]() =
    > > > Encoding.ASCII.GetBytes(" EZ{PRINT,STOP350:mad:20,35:ZP08A|003|")
    > > >            networkStream.Write(sendBytes, 0, sendBytes.Length)
    > > >            ' Read the NetworkStream into a byte buffer.
    > > >            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
    > > >            networkStream.Read(bytes, 0,
    > > > CInt(tcpClient.ReceiveBufferSize))' --->  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.WriteLine(("Host returned: " + returndata))
    > > >        Else
    > > >            If Not networkStream.CanRead Then
    > > >                Console.WriteLine("cannot not write data to this
    > > > stream")
    > > >                tcpClient.Close()
    > > >            Else
    > > >                If Not networkStream.CanWrite Then
    > > >                    Console.WriteLine("cannot read data from this
    > > > stream")
    > > >                    tcpClient.Close()
    > > >                End If
    > > >            End If
    > > >        End If
    > > >        ' pause so user can view the console output
    > > >        Console.ReadLine()
    > > >    End Sub

    >
    > > > End Class- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > How can i convert this to VB?- Hide quoted text -
    >
    > - Show quoted text -


    I figured it out. Just in case other people are having the same
    problem here is the code i used.

    Dim clsError As System.Net.Sockets.SocketError
    Dim bMessage As Byte() =
    System.Text.Encoding.ASCII.GetBytes("EZ{PRINT,STOP350:mad:20,35:ZP08A|
    003|}")
    Dim clsSocket As New Socket(AddressFamily.InterNetwork,
    SocketType.Stream, ProtocolType.Tcp)
    clsSocket.Connect("10.0.9.106", 515)
    clsSocket.Send(bMessage, 0, bMessage.Length, SocketFlags.None,
    clsError)
    id10t error, May 22, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nuno Fonseca

    ACCESS over TCP/IP

    Nuno Fonseca, Sep 5, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,044
    William Ryan
    Sep 6, 2003
  2. Danish Ahmed

    .net remoting and tcp listener socket

    Danish Ahmed, Apr 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    3,772
    Danish Ahmed
    Apr 21, 2005
  3. xchris
    Replies:
    5
    Views:
    4,125
  4. Guest

    tcp/ip events

    Guest, Nov 24, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    446
    Guest
    Nov 24, 2003
  5. Tiger
    Replies:
    5
    Views:
    968
    Dave Thompson
    May 1, 2006
Loading...

Share This Page