Click here to Skip to main content
15,938,446 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have to write a service, where the service has to listen continuosly from the TCP port of the system where it is deployed.

I tried few Console Examples but dont know what the mistake they are not working.

thanks in advance.

What I have tried:

I tried using the following code:
TcpListener server = null;
           try
           {
               // Set the TcpListener on port 13000.
               Int32 port = 8080;
               IPAddress localAddr = IPAddress.Parse("10.0.0.5");

               // TcpListener server = new TcpListener(port);
               server = new TcpListener(IPAddress.Any, 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();
Posted
Updated 16-Oct-18 7:41am
Comments
F-ES Sitecore 16-Oct-18 4:42am    
"Not working" doesn't give anyone enough information to help you. Explain any error messages you get, or any behaviour that doesn't happen that should, or behaviour that shouldn't happen but does.
sirisha.basavaraju 16-Oct-18 4:55am    
The console displays "Waiting for a connection... " and does not move beyond that
I am getting data on the port when i checked with "Socket Test" tool but not getting data from my C# program
F-ES Sitecore 16-Oct-18 5:06am    
What is connecting to that port on your machine? What data are you expecting to see?

1 solution

TcpListener.AcceptTcpClient is a blocking method. It will not return until a connection made... In your case it means that no connection on that address and port...
As you state that some test software does recognize the device, you probably have the wrong settings (IP/port)...
 
Share this answer
 
v2

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