Ident服务器未在端口113上返回标识

时间:2015-01-02 04:05:42

标签: c# identity server

我正在尝试编写一个ident服务器来处理来自我正在为irc客户端编程的irc服务器的身份协议请求。问题是我尝试在屏幕上打印我收到但没有打印的内容。我没有收到错误代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace ConnectIRC
{
    class IdentityClass
    {
        private const int bufSize = 32;
        public void IdentityRequest() {
            TcpListener listener = null;
            int port = 113;

            IPEndPoint hostInfo = new IPEndPoint(IPAddress.Any, 113);
            listener = new TcpListener(hostInfo);
            listener.Start();

            byte[] rcvBuffer = new byte[bufSize];
            int rec;

            for (; ; )
            {
                TcpClient client = null;
                NetworkStream netStream = null;

                client = listener.AcceptTcpClient();
                if (listener.Pending())
                {
                    Console.WriteLine("Connection was made");
                }
                netStream = client.GetStream();
                //byte[] rcvBuffer = new byte[bufSize];
                rec = netStream.Read(rcvBuffer, 0, rcvBuffer.Length);
                Array.Resize(ref rcvBuffer, rec);
                Console.WriteLine(Encoding.ASCII.GetString(rcvBuffer));
                netStream.Close();
                client.Close();
            }



        }

    }
}

1 个答案:

答案 0 :(得分:0)

这是一个非常基本的身份服务器实现

显然它只接受一个连接并关闭

请注意,您需要通过路由器映射端口才能使其正常工作

public class Ident
{
   private readonly TcpListener _listener;
   private readonly string _userId;

   public Ident(string userId)
   {
      _userId = userId;
      _listener = new TcpListener(IPAddress.Any, 113);
   }

   public void Start()
   {
      Console.WriteLine("Ident started");
      _listener.Start();
      var client = _listener.AcceptTcpClient();
      _listener.Stop();
      Console.WriteLine("Ident got a connection");
      using (var s = client.GetStream())
      {
         var reader = new StreamReader(s);
         var str = reader.ReadLine();
         var writer = new StreamWriter(s);
         Console.WriteLine("Ident got: " + str + ", sending reply");
         writer.WriteLine(str + " : USERID : UNIX : " + _userId);
         writer.Flush();
         Console.WriteLine("Ident sent reply");
      }
      Console.WriteLine("Ident server exiting");

   }
}