如何从我的系统上的TCP端口读取 [英] How to read from TCP port on my system

查看:98
本文介绍了如何从我的系统上的TCP端口读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我必须编写服务,服务必须从部署系统的TCP端口连续监听。



我试过几个控制台示例,但不知道他们没有工作的错误。



提前感谢。



我的尝试:



我尝试使用以下代码:

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();

推荐答案

TcpListener.AcceptTcpClient 是一种阻止方法。在连接完成之前它不会返回...在您的情况下,这意味着该地址和端口没有连接...

正如您所说的某些测试软件确实识别该设备,您可能已经错误的设置(IP /端口)......
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)...


这篇关于如何从我的系统上的TCP端口读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆