Listen to Port

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

    Listen to Port

    I am looking for code to listen to a tcp port. Another (not .net) process
    will be writing to that port on the same computer and I need c# code to be
    able to get data, which is a simple string, from that port. I am not a c#
    coder but can easy adapt working sample.
    Thanks alot for help.

  • John Timney \(MVP\)

    #2
    Re: Listen to Port

    read up on creating a socket listener


    Regards

    John Timney (MVP)




    "markgoldin " <markgoldin_200 0@yahoo.comwrot e in message
    news:03D6DDD8-FFD4-4974-AA54-3229ABE710A0@mi crosoft.com...
    >I am looking for code to listen to a tcp port. Another (not .net) process
    >will be writing to that port on the same computer and I need c# code to be
    >able to get data, which is a simple string, from that port. I am not a c#
    >coder but can easy adapt working sample.
    Thanks alot for help.

    Comment

    • Kerem Gümrükcü

      #3
      Re: Listen to Port

      Hi,

      thats really easy to do in .NET. Thats all you need:

      //inside a console Main

      byte[] byteReadStream = null; // holds the data in byte buffer
      IPEndPoint ipe = new IPEndPoint(IPAd dress.Parse("0. 0.0.0"),
      8888);//listen on all local addresses and 8888 port
      TcpListener tcpl = new TcpListener(ipe );

      while(true){ //infinite loop


      tcpl.Start(); // block application until data and connection
      is requested
      TcpClient tcpc = tcpl.AcceptTcpC lient(); //accept connection

      byteReadStream = new byte[tcpc.Available]; //allocate space
      for data
      tcpc.GetStream( ).Read(byteRead Stream, 0, tcpc.Available) ;
      //read data into byte array
      Console.WriteLi ne(Encoding.Def ault.GetString( byteReadStream)
      + "\n"); Write data to console buffer



      }


      But see the MSDN Library for more information
      about the used Classes.


      Regards

      Kerem


      --
      --
      -----------------------
      Beste Grüsse / Best regards / Votre bien devoue
      Kerem Gümrükcü
      Microsoft Live Space: http://kerem-g.spaces.live.com/
      Latest Open-Source Projects: http://entwicklung.junetz.de
      -----------------------
      "This reply is provided as is, without warranty express or implied."

      "markgoldin " <markgoldin_200 0@yahoo.comschr ieb im Newsbeitrag
      news:03D6DDD8-FFD4-4974-AA54-3229ABE710A0@mi crosoft.com...
      I am looking for code to listen to a tcp port. Another (not .net) process
      will be writing to that port on the same computer and I need c# code to
      be
      able to get data, which is a simple string, from that port. I am not a c#
      coder but can easy adapt working sample.
      Thanks alot for help.
      >

      Comment

      • markgoldin

        #4
        Re: Listen to Port

        What is a difference between listening to a socket or to a TCP port? Or it's
        the same?

        Thanks
        "John Timney (MVP)" <xyz_john@timne y.eclipse.co.uk wrote in message
        news:QdydnRJya9 YL6NzVnZ2dnUVZ8 s3inZ2d@eclipse .net.uk...
        read up on creating a socket listener

        >
        Regards
        >
        John Timney (MVP)


        >
        >
        "markgoldin " <markgoldin_200 0@yahoo.comwrot e in message
        news:03D6DDD8-FFD4-4974-AA54-3229ABE710A0@mi crosoft.com...
        >>I am looking for code to listen to a tcp port. Another (not .net) process
        >>will be writing to that port on the same computer and I need c# code to
        >>be able to get data, which is a simple string, from that port. I am not a
        >>c# coder but can easy adapt working sample.
        >Thanks alot for help.
        >
        >

        Comment

        • Joe Fawcett

          #5
          Re: Listen to Port



          "markgoldin " <markgoldin_200 0@yahoo.comwrot e in message
          news:050F74B1-50E4-46F1-8471-FF4AB033B2B0@mi crosoft.com...
          What is a difference between listening to a socket or to a TCP port? Or
          it's the same?
          >
          Thanks
          "John Timney (MVP)" <xyz_john@timne y.eclipse.co.uk wrote in message
          news:QdydnRJya9 YL6NzVnZ2dnUVZ8 s3inZ2d@eclipse .net.uk...
          >read up on creating a socket listener
          >http://www.devarticles.com/c/a/C-Sha...g-in-C-Part-I/
          >>
          >Regards
          >>
          >John Timney (MVP)
          >http://www.johntimney.com
          >http://www.johntimney.com/blog
          >>
          >>
          >"markgoldin " <markgoldin_200 0@yahoo.comwrot e in message
          >news:03D6DDD 8-FFD4-4974-AA54-3229ABE710A0@mi crosoft.com...
          >>>I am looking for code to listen to a tcp port. Another (not .net) process
          >>>will be writing to that port on the same computer and I need c# code to
          >>>be able to get data, which is a simple string, from that port. I am not a
          >>>c# coder but can easy adapt working sample.
          >>Thanks alot for help.
          >>
          >>
          >
          A socket is basically a combination of an IP address and a port.

          --

          Joe Fawcett (MVP - XML)


          Comment

          • Kerem Gümrükcü

            #6
            Re: Listen to Port

            >A socket is basically a combination of an IP address and a port.

            ACK.

            But i would describe a Socket more an connection endpoint
            that is bound to an ip address and a corresponding port.

            Regards

            K.

            --
            --
            -----------------------
            Beste Grüsse / Best regards / Votre bien devoue
            Kerem Gümrükcü
            Microsoft Live Space: http://kerem-g.spaces.live.com/
            Latest Open-Source Projects: http://entwicklung.junetz.de
            -----------------------
            "This reply is provided as is, without warranty express or implied."

            "Joe Fawcett" <joefawcett@new sgroup.nospamsc hrieb im Newsbeitrag
            news:12A3D188-3BB7-46B5-946F-590C84F9B635@mi crosoft.com...
            >
            >
            "markgoldin " <markgoldin_200 0@yahoo.comwrot e in message
            news:050F74B1-50E4-46F1-8471-FF4AB033B2B0@mi crosoft.com...
            What is a difference between listening to a socket or to a TCP port? Or
            it's the same?

            Thanks
            "John Timney (MVP)" <xyz_john@timne y.eclipse.co.uk wrote in message
            news:QdydnRJya9 YL6NzVnZ2dnUVZ8 s3inZ2d@eclipse .net.uk...
            read up on creating a socket listener

            >
            Regards
            >
            John Timney (MVP)


            >
            >
            "markgoldin " <markgoldin_200 0@yahoo.comwrot e in message
            news:03D6DDD8-FFD4-4974-AA54-3229ABE710A0@mi crosoft.com...
            >>I am looking for code to listen to a tcp port. Another (not .net)
            process
            >>will be writing to that port on the same computer and I need c# code
            to
            >>be able to get data, which is a simple string, from that port. I am not
            a
            >>c# coder but can easy adapt working sample.
            >Thanks alot for help.
            >
            >
            A socket is basically a combination of an IP address and a port.
            >
            --
            >
            Joe Fawcett (MVP - XML)

            >

            Comment

            • Peter Duniho

              #7
              Re: Listen to Port

              On Sat, 31 May 2008 09:27:39 -0700, Kerem Gümrükcü <kareem114@hotm ail.com>
              wrote:
              >A socket is basically a combination of an IP address and a port.
              >
              ACK.
              >
              But i would describe a Socket more an connection endpoint
              that is bound to an ip address and a corresponding port.
              Well, as long as we're nitpicking... :)

              For TCP over TCP/IP, a connected socket represents _two_ endpoints -- the
              local endpoint and the remote endpoint -- each of which is defined by an
              IP address and a port, and of course the fact that the connection is the
              TCP protocol is represented as well. In other words, the connected socket
              encapsulates five different pieces of information, and is uniquely
              identified by those pieces of information.

              Different sockets vary. A listening socket of course has only the
              protocol (TCP), local IP and port, and the fact that it's listening. Just
              local IP and port wouldn't be sufficient to distinguish it from connected
              sockets on the same port. And of course UDP sockets are connectionless,
              so the remote endpoint only affects individual datagrams, not the socket
              itself.

              And then, of course, the Socket class in .NET is a managed object that
              provides a managed interface to all of the above. :)

              Pete

              Comment

              • Peter Duniho

                #8
                Re: Listen to Port

                On Sat, 31 May 2008 08:50:24 -0700, Kerem Gümrükcü <kareem114@hotm ail.com>
                wrote:
                [...]
                byteReadStream = new byte[tcpc.Available]; //allocate
                space
                for data
                tcpc.GetStream( ).Read(byteRead Stream, 0, tcpc.Available) ;
                //read data into byte array
                Console.WriteLi ne(Encoding.Def ault.GetString( byteReadStream)
                + "\n"); Write data to console buffer
                This is wrong, for at least a couple of reasons.

                The most significant one is that TCP does not guarantee that all of the
                data sent will be read in a single call to Read(). You _must_ iterate the
                read in some way (loop, async i/o, whatever) until you know for sure that
                you've read all of the data. This could be as simple as reading
                everything until the end of the stream (connection is closed), or more
                complicated as in defining a fixed-length for the data, sending a length
                before the string, or using some sort of delimiter/terminator (e.g. null
                character).

                The other issue is that the Available property may change between the time
                one allocates the buffer and the time one calls Read(). If it increases,
                then the buffer length passed to Read() is larger than the buffer itself
                actually is, which will produce an error. At the very least, one should
                be passing "byteReadStream .Length" as the buffer length, not some other
                value. Preferable would be to allocate the buffer as some fixed size,
                rather than anticipating the size of the read. What size is appropriate
                depends on your application protocol, but for larger transfers, a 4K or 8K
                buffer would be appropriate.

                IMHO it would be better for the OP to simply read the docs for
                TcpListener, etc. and take advantage of the code samples there. If he
                still has questions after looking at the documentation, then we can answer
                those in a more specific, directed way.

                Pete

                Comment

                • Kerem Gümrükcü

                  #9
                  Re: Listen to Port

                  Hi Pete,

                  i even didnt compile the code. I wrote it on the
                  fly inside #develop. It was just a first hint for the OP.
                  As statet he should read the docs in my reply,...


                  Regards

                  Kerem

                  --
                  -----------------------
                  Beste Grüsse / Best regards / Votre bien devoue
                  Kerem Gümrükcü
                  Microsoft Live Space: http://kerem-g.spaces.live.com/
                  Latest Open-Source Projects: http://entwicklung.junetz.de
                  -----------------------
                  "This reply is provided as is, without warranty express or implied."
                  "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.coms chrieb im Newsbeitrag
                  news:op.ub03m6n 18jd0ej@petes-computer.local. ..
                  On Sat, 31 May 2008 08:50:24 -0700, Kerem Gümrükcü <kareem114@hotm ail.com>
                  wrote:
                  >
                  >[...]
                  > byteReadStream = new byte[tcpc.Available]; //allocate
                  >space
                  >for data
                  > tcpc.GetStream( ).Read(byteRead Stream, 0, tcpc.Available) ;
                  >//read data into byte array
                  >>
                  >Console.WriteL ine(Encoding.De fault.GetString (byteReadStream )
                  >+ "\n"); Write data to console buffer
                  >
                  This is wrong, for at least a couple of reasons.
                  >
                  The most significant one is that TCP does not guarantee that all of the
                  data sent will be read in a single call to Read(). You _must_ iterate the
                  read in some way (loop, async i/o, whatever) until you know for sure that
                  you've read all of the data. This could be as simple as reading
                  everything until the end of the stream (connection is closed), or more
                  complicated as in defining a fixed-length for the data, sending a length
                  before the string, or using some sort of delimiter/terminator (e.g. null
                  character).
                  >
                  The other issue is that the Available property may change between the time
                  one allocates the buffer and the time one calls Read(). If it increases,
                  then the buffer length passed to Read() is larger than the buffer itself
                  actually is, which will produce an error. At the very least, one should
                  be passing "byteReadStream .Length" as the buffer length, not some other
                  value. Preferable would be to allocate the buffer as some fixed size,
                  rather than anticipating the size of the read. What size is appropriate
                  depends on your application protocol, but for larger transfers, a 4K or 8K
                  buffer would be appropriate.
                  >
                  IMHO it would be better for the OP to simply read the docs for
                  TcpListener, etc. and take advantage of the code samples there. If he
                  still has questions after looking at the documentation, then we can answer
                  those in a more specific, directed way.
                  >
                  Pete

                  Comment

                  • Kerem Gümrükcü

                    #10
                    Re: Listen to Port

                    ACK.


                    Cheers

                    K.

                    --
                    -----------------------
                    Beste Grüsse / Best regards / Votre bien devoue
                    Kerem Gümrükcü
                    Microsoft Live Space: http://kerem-g.spaces.live.com/
                    Latest Open-Source Projects: http://entwicklung.junetz.de
                    -----------------------
                    "This reply is provided as is, without warranty express or implied."
                    "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.coms chrieb im Newsbeitrag
                    news:op.ub0250f v8jd0ej@petes-computer.local. ..
                    On Sat, 31 May 2008 09:27:39 -0700, Kerem Gümrükcü <kareem114@hotm ail.com>
                    wrote:
                    >
                    >>A socket is basically a combination of an IP address and a port.
                    >>
                    >ACK.
                    >>
                    >But i would describe a Socket more an connection endpoint
                    >that is bound to an ip address and a corresponding port.
                    >
                    Well, as long as we're nitpicking... :)
                    >
                    For TCP over TCP/IP, a connected socket represents _two_ endpoints -- the
                    local endpoint and the remote endpoint -- each of which is defined by an
                    IP address and a port, and of course the fact that the connection is the
                    TCP protocol is represented as well. In other words, the connected socket
                    encapsulates five different pieces of information, and is uniquely
                    identified by those pieces of information.
                    >
                    Different sockets vary. A listening socket of course has only the
                    protocol (TCP), local IP and port, and the fact that it's listening. Just
                    local IP and port wouldn't be sufficient to distinguish it from connected
                    sockets on the same port. And of course UDP sockets are connectionless,
                    so the remote endpoint only affects individual datagrams, not the socket
                    itself.
                    >
                    And then, of course, the Socket class in .NET is a managed object that
                    provides a managed interface to all of the above. :)
                    >
                    Pete

                    Comment

                    • Markgoldin

                      #11
                      Re: Listen to Port

                      Thanks for your code.
                      I am running the code, but it only shows data being sent to TCP port if I
                      debug program firstly stopping it at the line:
                      byteReadStream = new byte[tcpc.Available];

                      otherwise, if I just run it I dont see data shown in the Console.



                      "Kerem Gümrükcü" <kareem114@hotm ail.comwrote in message
                      news:u5DOhazwIH A.4376@TK2MSFTN GP06.phx.gbl...
                      Hi,
                      >
                      thats really easy to do in .NET. Thats all you need:
                      >
                      //inside a console Main
                      >
                      byte[] byteReadStream = null; // holds the data in byte buffer
                      IPEndPoint ipe = new IPEndPoint(IPAd dress.Parse("0. 0.0.0"),
                      8888);//listen on all local addresses and 8888 port
                      TcpListener tcpl = new TcpListener(ipe );
                      >
                      while(true){ //infinite loop
                      >
                      >
                      tcpl.Start(); // block application until data and
                      connection
                      is requested
                      TcpClient tcpc = tcpl.AcceptTcpC lient(); //accept
                      connection
                      >
                      byteReadStream = new byte[tcpc.Available]; //allocate space
                      for data
                      tcpc.GetStream( ).Read(byteRead Stream, 0, tcpc.Available) ;
                      //read data into byte array
                      >
                      Console.WriteLi ne(Encoding.Def ault.GetString( byteReadStream)
                      + "\n"); Write data to console buffer
                      >
                      >
                      >
                      }
                      >
                      >
                      But see the MSDN Library for more information
                      about the used Classes.
                      >
                      >
                      Regards
                      >
                      Kerem
                      >
                      >
                      --
                      --
                      -----------------------
                      Beste Grüsse / Best regards / Votre bien devoue
                      Kerem Gümrükcü
                      Microsoft Live Space: http://kerem-g.spaces.live.com/
                      Latest Open-Source Projects: http://entwicklung.junetz.de
                      -----------------------
                      "This reply is provided as is, without warranty express or implied."
                      >
                      "markgoldin " <markgoldin_200 0@yahoo.comschr ieb im Newsbeitrag
                      news:03D6DDD8-FFD4-4974-AA54-3229ABE710A0@mi crosoft.com...
                      >I am looking for code to listen to a tcp port. Another (not .net) process
                      >will be writing to that port on the same computer and I need c# code to
                      be
                      >able to get data, which is a simple string, from that port. I am not a c#
                      >coder but can easy adapt working sample.
                      >Thanks alot for help.
                      >>
                      >
                      >

                      Comment

                      Working...