linux上的TCP代理错误

本文关键字:错误 代理 TCP 上的 linux | 更新日期: 2023-09-27 17:58:13

我正试图在Linux服务器上运行一个内部代理,它是用C#编码的,我使用Mono来运行它。它在Windows7上运行得很好,但当我试图在Linux上运行它时,它会抛出一个异常;

Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
  at WorldSvrListener.Server.ListenForClients () [0x00000] in <filename unknown>:0
  at System.Threading.Thread.StartInternal () [0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
  at WorldSvrListener.Server.ListenForClients () [0x00000] in <filename unknown>:0
  at System.Threading.Thread.StartInternal () [0x00000] in <filename unknown>:0

我还没有完整地编写这个类,大部分都来自谷歌,我主要学习并编辑了它,但我对类和对象的经验还不够。

这是代码;

using System;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Net;
namespace SvrListener
{
    class Server
    {
        static void Main()
        { 
            Server server = new Server();
            server.tcpListener.Start();
        }
        private TcpListener tcpListener;
        private Thread listenThread;
        public Server()
        {
            this.tcpListener = new TcpListener(IPAddress.Any, 32666);
            this.listenThread = new Thread(new ThreadStart(ListenForClients));
            this.listenThread.Start();
        }
        private void ListenForClients()
        {
            try
            {
                Console.WriteLine("Try and listen to:" + tcpListener.Server.LocalEndPoint.ToString());
                this.tcpListener.Start();
                Console.WriteLine("Waiting for connection...");
            }
            catch (SocketException e) { Console.WriteLine("Error:" +e.ErrorCode + " "+ e.Message); }
            while (true)
            {
                //blocks until a client has connected to the server
                TcpClient client = this.tcpListener.AcceptTcpClient();
                //create a thread to handle communication 
                //with connected client
                Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                clientThread.Start(client);
            }
        }
        private void HandleClientComm(object client)
        {
            TcpClient tcpClient = (TcpClient)client;
            NetworkStream clientStream = tcpClient.GetStream();
            byte[] message = new byte[1024];
            int bytesRead;
            while (true)
            {
                bytesRead = 0;
                try
                {
                    //blocks until a client sends a message
                    bytesRead = clientStream.Read(message, 0, message.Length);
                }
                catch
                {
                    //a socket error has occured
                    break;
                }
                if (bytesRead == 0)
                {
                    //the client has disconnected from the server
                    break;
                }
                //message has successfully been received
                ASCIIEncoding encoder = new ASCIIEncoding();
                string s = message.Length.ToString();
                Console.WriteLine(s);
                Console.WriteLine(encoder.GetString(message, 0, bytesRead));

                Thread forwardThread = new Thread(new ParameterizedThreadStart(forwardPacket));
                forwardThread.Start(message);
            }
            tcpClient.Close();
         }
        private void forwardPacket(object message)
        {
            TcpClient client = new TcpClient();
            byte[] buffer = (byte[])message;
            IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.50"), 24585);
            client.Connect(serverEndPoint);
            NetworkStream clientStream = client.GetStream();
            ASCIIEncoding encoder = new ASCIIEncoding();
            string shit = encoder.GetString(buffer);
            byte[] nBuffer = encoder.GetBytes(shit);
            clientStream.Write(nBuffer, 0, nBuffer.Length);
            clientStream.Flush();
     }
}
}

更新:所以我尝试了--debug命令,它没有给我任何有用的东西;

Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
  at WorldSvrListener.Server.ListenForClients () [0x00000] in <filename unknown>:0
  at System.Threading.Thread.StartInternal () [0x00016] in /usr/src/mono-3.0.7/mcs/class/corlib/System.Threading/Thread.cs:734

linux上的TCP代理错误

嗯,您对Start()调用了两次,在Main()和ListenForClients()中,尝试删除Main()中的一个。

我在linux中使用mono创建了一些服务器,我必须告诉大家,我在TcpListener方面遇到了很多问题(但那至少是四年前的事了…),所以最终采用了一个完美工作的纯套接字实现。

根据可用信息,我的猜测是,由于没有打印其他内容,它将在这一行中消亡:

Console.WriteLine("Try and listen to:" + tcpListener.Server.LocalEndPoint.ToString());

MSDN上的文档并没有真正说明Socket.LocalEndPoint何时或是否可以返回null,但我认为它为未绑定的套接字返回null是有意义的。在Mono的实现中,套接字也可能在调用TcpListener.Start之后才绑定。

尝试在Start()调用之后移动该行,看看它是否会使内容不为空。