Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Socket listening

Socket listening

Scheduled Pinned Locked Moved C#
tutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    choopie
    wrote on last edited by
    #1

    I want to listen ip host with a specified port number. But not local ip address. I've tried to do it using three classes IPEndPoint, Socket, NetworkStream, but I couldn't see any data (in a MessageBox for example). I use: private static string connectSocket(string address, int port) { //address = "10.0.105.4"; //port = 4000; String data = null; try { Byte[] bytes = new Byte[256]; IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(address), port); Socket socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); socket.Connect(endPoint); NetworkStream ns = new NetworkStream(socket); Int32 i; while((i = ns.Read(bytes, 0, bytes.Length)) != 0) { data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); } return data } Does anyone know how to solve this?

    E 1 Reply Last reply
    0
    • C choopie

      I want to listen ip host with a specified port number. But not local ip address. I've tried to do it using three classes IPEndPoint, Socket, NetworkStream, but I couldn't see any data (in a MessageBox for example). I use: private static string connectSocket(string address, int port) { //address = "10.0.105.4"; //port = 4000; String data = null; try { Byte[] bytes = new Byte[256]; IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(address), port); Socket socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); socket.Connect(endPoint); NetworkStream ns = new NetworkStream(socket); Int32 i; while((i = ns.Read(bytes, 0, bytes.Length)) != 0) { data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); } return data } Does anyone know how to solve this?

      E Offline
      E Offline
      Eitsop
      wrote on last edited by
      #2

      Err... The line that goes:

      data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);

      Should you appending? i.e.

      data += System.Text.Encoding.ASCII.GetString(bytes, 0, i);

      It is that simple, just adding the data += instead of data = ?

      - Eitsop What we do not understand we do not possess. - Goethe.

      C 1 Reply Last reply
      0
      • E Eitsop

        Err... The line that goes:

        data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);

        Should you appending? i.e.

        data += System.Text.Encoding.ASCII.GetString(bytes, 0, i);

        It is that simple, just adding the data += instead of data = ?

        - Eitsop What we do not understand we do not possess. - Goethe.

        C Offline
        C Offline
        choopie
        wrote on last edited by
        #3

        Thanks but still it should return to me any data without appending.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups