Tcp侦听器与Http侦听器

时间:2018-09-20 10:53:26

标签: c#

我只需要发送一个简单的字符串。哪种方法更合适,还是性能更好。

HttpListener

$scope.$on('broadcastedEvent', function() {
  doThings(); 
});

TcpListener

HttpListener listener = new HttpListener();

    foreach (string s in prefixes)
    {
        listener.Prefixes.Add(s);
    }
    listener.Start();
    Console.WriteLine("Listening..");

    HttpListenerContext context = listener.GetContext();
    HttpListenerRequest request = context.Request;
    HttpListenerResponse response = context.Response;

    string responseString = "<HTML><BODY> Test </BODY></HTML>";
    byte[] buffer = Encoding.UTF8.GetBytes(responseString);

    response.ContentLength64 = buffer.Length;
    Stream output = response.OutputStream;
    output.Write(buffer, 0, buffer.Length);

    output.Close();
    listener.Stop();

我需要从客户端发送带有一些值的字符串,例如Name = xxx,Age = xxx ,然后服务器获取该字符串并回答“确定”

0 个答案:

没有答案