Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a TCP client that I need to Listen and send responses. What’s the best way to achieve this with c#?
I’ve done something in the past but I was the client, in this situation I need to be the server. I’ve been searching a way to connect to the client through a given port and listen/send data….but with no results….

Here's my code so far....

C#
private void ListeningThread() // let's listen in another thread instead!!
       {
           int port = 12345; // change as required

           this.listener = new TcpListener(IPAddress.Any, port);

           try
           {
               this.listener.Start();
           }
           catch (Exception e) { MessageBox.Show("couldn't bind to port " + port + " -> " + e.Message); return; }



           while (true)
           {





               Thread.Sleep(30);
           }
       }




Now I need, in the while cicle, to receive data and convert it to a string...
Posted
Updated 6-Jun-13 1:29am
v2
Comments
Volynsky Alex 6-Jun-13 6:54am    
Hi marco.constantino

This article:
http://www.codeproject.com/Articles/488668/Csharp-NET-TCP-Server describes a library that provides an easy to use a TCP server component for the .NET environment that does not have the limitations ...

Regards,
Alex

1 solution

I guess you are talking about TcpListener Class[^].

Also refer - A TCP/IP Server written in C#[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900