1

As per MSDN:

The TcpListener class provides simple methods that listen for and accept incoming connection requests in blocking synchronous mode.

My team lead is asking to send the data using TcpListener. Is there any way or feasible to receive as well as send the data using TCPListerner ?

I already know how to receive the data using TCPListener, don't have any idea about sending the data with the same. I know TCPClient can be used to send the data but our team lead wants to do it using TCPListerner.

Please let me know your comments and let me know if it is feasible with some sample code.

dev-masih
  • 4,188
  • 3
  • 33
  • 55
Gaurav123
  • 5,059
  • 6
  • 51
  • 81
  • Do you mean send reply to the server?? http://stackoverflow.com/questions/5589605/how-to-send-a-hello-to-server-and-reply-a-hi see if this helps – User2012384 Nov 17 '15 at 06:48
  • Please see this line from my lead `` It would use TCPListner so that it is server in the case of sending data.`` so you might be right, its reply – Gaurav123 Nov 17 '15 at 06:50
  • `TcpListener` accepts connections - once the connection is established, the data can flow both ways. You just write data to the stream... It's not really clear what you're asking. – Jon Skeet Nov 17 '15 at 06:51
  • @JonSkeet : Suppose I connected to a ``TCPListener`` using a ``TCPClient``, Now is there way send some data back to ``TCPClient`` using ``TCPListener`` ? – Gaurav123 Nov 17 '15 at 06:55
  • Yes. You write to the stream, and the client reads from the stream - it's as simple as that. – Jon Skeet Nov 17 '15 at 06:58
  • There is no direct method on TCPListener to write data to stream, but you can do this as a workaround: TcpClient client = TcpListener.AcceptTcpClient(); then you can send the stream back to the server. – User2012384 Nov 17 '15 at 07:01
  • @JonSkeet : yes I wrote some data to stream and tried to read from that stream on client side and it works. – Gaurav123 Nov 17 '15 at 07:13

1 Answers1

6

Not really. The purpose of TcpListener is to accept a new connection.

However, the stream object you get from your TcpListener (once the connection is established) can be used for reading and writing.

Look here for more details:

https://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener%28v=vs.110%29.aspx

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

class MyTcpListener
{
  public static void Main()
  { 
    TcpListener server=null;   
    try
    {
      // Set the TcpListener on port 13000.
      Int32 port = 13000;
      IPAddress localAddr = IPAddress.Parse("127.0.0.1");

      // TcpListener server = new TcpListener(port);
      server = new TcpListener(localAddr, port);

      // Start listening for client requests.
      server.Start();

      // Buffer for reading data
      Byte[] bytes = new Byte[256];
      String data = null;

      // Enter the listening loop.
      while(true) 
      {
        Console.Write("Waiting for a connection... ");

        // Perform a blocking call to accept requests.
        // You could also user server.AcceptSocket() here.
        TcpClient client = server.AcceptTcpClient();            
        Console.WriteLine("Connected!");

        data = null;

        // Get a stream object for reading and writing
        NetworkStream stream = client.GetStream();

        int i;

        // Loop to receive all the data sent by the client.
        while((i = stream.Read(bytes, 0, bytes.Length))!=0) 
        {   
          // Translate data bytes to a ASCII string.
          data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
          Console.WriteLine("Received: {0}", data);

          // Process the data sent by the client.
          data = data.ToUpper();

          byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

          // Send back a response.
          stream.Write(msg, 0, msg.Length);
          Console.WriteLine("Sent: {0}", data);            
        }

        // Shutdown and end connection
        client.Close();
      }
    }
    catch(SocketException e)
    {
      Console.WriteLine("SocketException: {0}", e);
    }
    finally
    {
       // Stop listening for new clients.
       server.Stop();
    }


    Console.WriteLine("\nHit enter to continue...");
    Console.Read();
  }   
}
paulsm4
  • 114,292
  • 17
  • 138
  • 190