Sockets Question

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

    #1

    Sockets Question

    Hello All,

    My question is about the proper way to setup a listening socket. I have the
    following code that works fine:
    IPAddress localAddr = Dns.GetHostEntr y(strHostName). AddressList[0];

    Int32 port = 25001;
    TcpListener tcpServerListen er = new TcpListener(por t);
    tcpServerListen er.Start();
    Socket serverSocket = tcpServerListen er.AcceptSocket ();

    However, I've seen other code out there that looks like this:
    this.listenerSo cket = new Socket(AddressF amily.InterNetw ork,
    SocketType.Stre am, ProtocolType.Tc p);
    this.listenerSo cket.Bind( new IPEndPoint(this .serverIP , this.serverPort ));
    this.listenerSo cket.Listen(200 );

    Is it necessary to specify the AddressFamily and SocketType or is my way ok?

    Thanks,
    John
  • Dave Sexton

    #2
    Re: Sockets Question

    Hi John,

    TcpListener wraps a listening, streaming Tcp Socket and it aggregates some of
    the more common operations into single method calls, so it's much easier to
    code against.

    The code you have supplied that uses TcpListener does something different than
    the code you have supplied that uses Socket directly:
    Socket serverSocket = tcpServerListen er.AcceptSocket ();
    This line blocks until it can acquire the first client connection, returned as
    a different Socket than the one TcpListener uses internally

    And the Socket code has something different as well:
    this.listenerSo cket.Bind( new IPEndPoint(this .serverIP , this.serverPort ));
    Binds the Socket to a specific IP as well as a specific port


    Check out the TcpListener.Acc eptTcpClient() method. Instead of AcceptSocket()
    returning a Socket, it returns TcpClient, which wraps the client Socket
    connection making it easier to work with.

    --
    Dave Sexton

    "John F" <jf@rt.comwro te in message
    news:CCE6A78A-83C5-41F1-A765-9B78C40ABEF1@mi crosoft.com...
    Hello All,
    >
    My question is about the proper way to setup a listening socket. I have the
    following code that works fine:
    IPAddress localAddr = Dns.GetHostEntr y(strHostName). AddressList[0];
    >
    Int32 port = 25001;
    TcpListener tcpServerListen er = new TcpListener(por t);
    tcpServerListen er.Start();
    Socket serverSocket = tcpServerListen er.AcceptSocket ();
    >
    However, I've seen other code out there that looks like this:
    this.listenerSo cket = new Socket(AddressF amily.InterNetw ork,
    SocketType.Stre am, ProtocolType.Tc p);
    this.listenerSo cket.Bind( new IPEndPoint(this .serverIP ,
    this.serverPort ));
    this.listenerSo cket.Listen(200 );
    >
    Is it necessary to specify the AddressFamily and SocketType or is my way ok?
    >
    Thanks,
    John

    Comment

    • John F

      #3
      Re: Sockets Question

      Thanks Dave. I will check out the AcceptTcpClient method.
      --
      John F


      "Dave Sexton" wrote:
      Hi John,
      >
      TcpListener wraps a listening, streaming Tcp Socket and it aggregates some of
      the more common operations into single method calls, so it's much easier to
      code against.
      >
      The code you have supplied that uses TcpListener does something different than
      the code you have supplied that uses Socket directly:
      >
      Socket serverSocket = tcpServerListen er.AcceptSocket ();
      >
      This line blocks until it can acquire the first client connection, returned as
      a different Socket than the one TcpListener uses internally
      >
      And the Socket code has something different as well:
      >
      this.listenerSo cket.Bind( new IPEndPoint(this .serverIP , this.serverPort ));
      >
      Binds the Socket to a specific IP as well as a specific port
      >
      >
      Check out the TcpListener.Acc eptTcpClient() method. Instead of AcceptSocket()
      returning a Socket, it returns TcpClient, which wraps the client Socket
      connection making it easier to work with.
      >
      --
      Dave Sexton
      >
      "John F" <jf@rt.comwro te in message
      news:CCE6A78A-83C5-41F1-A765-9B78C40ABEF1@mi crosoft.com...
      Hello All,

      My question is about the proper way to setup a listening socket. I have the
      following code that works fine:
      IPAddress localAddr = Dns.GetHostEntr y(strHostName). AddressList[0];

      Int32 port = 25001;
      TcpListener tcpServerListen er = new TcpListener(por t);
      tcpServerListen er.Start();
      Socket serverSocket = tcpServerListen er.AcceptSocket ();

      However, I've seen other code out there that looks like this:
      this.listenerSo cket = new Socket(AddressF amily.InterNetw ork,
      SocketType.Stre am, ProtocolType.Tc p);
      this.listenerSo cket.Bind( new IPEndPoint(this .serverIP ,
      this.serverPort ));
      this.listenerSo cket.Listen(200 );

      Is it necessary to specify the AddressFamily and SocketType or is my way ok?

      Thanks,
      John
      >
      >
      >

      Comment

      Working...