本文介绍了为什么这段代码不起作用(简单代理)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我使用了以下代码示例,但它不起作用。我不知道什么是错的。它没有做任何事情,我需要它将端口1205上传入的通信重新发送到另一个端口,即。 1203在同一台机器上。我真的很感激任何帮助。谢谢你 namespace ConsoleApplication1Hi,I have used following code example but it doesnt work. I dont know whats wrong. It does nothing, I would need it to resend communication incoming on port 1205 to another port,i.e. 1203 on the same machine. I would be really grateful for any help.Thank younamespace ConsoleApplication1 { class 程序class Program { static void Main( string [] args)static void Main(string[] args) { string Address = " google.com" ;string Address="google.com"; int UDPPort = 80;int UDPPort = 80; int TCPPort = 80;int TCPPort = 80; UdpRedirect _UdpRedirect = new UdpRedirect (){_ address = Address,_Port = UDPPort};UdpRedirect _UdpRedirect = new UdpRedirect() { _address = Address, _Port = UDPPort}; 线程 _Thread = new Thread (_ UdpRedirect.Connect);Thread _Thread = new Thread(_UdpRedirect.Connect); _Thread.Name = " UDP" ; ;_Thread.Name = "UDP"; _Thread.Start();_Thread.Start(); TcpRedirect _TcpRedirect = new TcpRedirect (地址,TCPPort);TcpRedirect _TcpRedirect = new TcpRedirect(Address, TCPPort);}} class UdpRedirectclass UdpRedirect { public string _address;public string _address; public int _Port;public int _Port; public UdpRedirect()public UdpRedirect() {} public void Connect()public void Connect() { UdpClient _UdpClient = new UdpClient (_ Port);UdpClient _UdpClient = new UdpClient(_Port); int ? LocalPort = null ;int? LocalPort = null; while ( true )while (true) { IPEndPoint _IPEndPoint = null ;IPEndPoint _IPEndPoint = null; byte [] _bytes = _UdpClient.Receive( ref _IPEndPoint);byte[] _bytes = _UdpClient.Receive(ref _IPEndPoint); if (LocalPort == null )LocalPort = _IPEndPoint.Port;if (LocalPort == null) LocalPort = _IPEndPoint.Port; bool Local = IPAddress .IsLoopback(_IPEndPoint .Address);bool Local = IPAddress.IsLoopback(_IPEndPoint.Address); string AddressToSe nd = null ;string AddressToSend = null; int PortToSend = 0;int PortToSend = 0; if (本地)if (Local) { AddressToSend = _address;AddressToSend = _address; PortToSend = _Port;PortToSend = _Port;} elseelse { AddressToSend = " 127.0。 0.1" ;AddressToSend = "127.0.0.1"; PortToSend = LocalPort.Value;PortToSend = LocalPort.Value;} _UdpClient.Send(_bytes,_bytes.Length,AddressToSend,PortToSend);_UdpClient.Send(_bytes, _bytes.Length, AddressToSend, PortToSend);}}} class TcpRedirectclass TcpRedirect { public TcpRedirect( string _address, int _Port)public TcpRedirect(string _address, int _Port) { TcpListener _TcpListener = new TcpListener ( IPAddress .Any,_Port);TcpListener _TcpListener = new TcpListener(IPAddress.Any, _Port); _TcpListener.Start();_TcpListener.Start(); int i = 0;int i = 0; while ( true )while (true) { i ++; TcpClient _LocalSocket = _TcpListener .AcceptTcpClient();TcpClient _LocalSocket = _TcpListener.AcceptTcpClient(); NetworkStream _NetworkStreamLocal = _LocalSocket.GetStream() ;NetworkStream _NetworkStreamLocal = _LocalSocket.GetStream(); TcpClient _RemoteSocket = new TcpClient (_ address,_Port);TcpClient _RemoteSocket = new TcpClient(_address, _Port); NetworkStream _NetworkStreamRemote = _RemoteSocket.GetStream();NetworkStream _NetworkStreamRemote = _RemoteSocket.GetStream(); 控制台 .WriteLine( " \\\<<<<<<<<<连接>>>>>>>>>>>>>" );Console.WriteLine("\n<<<<<<<<<connected>>>>>>>>>>>>>"); 客户端 _RemoteClient = new 客户 ( " remote" + i)Client _RemoteClient = new Client("remote" + i) { _SendingNetworkStream = _NetworkStreamLocal,_SendingNetworkStream = _NetworkStreamLocal, _ListenNetworkStream = _NetworkStreamRemote,_ListenNetworkStream = _NetworkStreamRemote, _ListenSocket = _RemoteSocket_ListenSocket = _RemoteSocket}; 客户 _LocalClient = new 客户 ( " local" + i)Client _LocalClient = new Client("local" + i) { _SendingNetworkStream = _NetworkStreamRemote,_SendingNetworkStream = _NetworkStreamRemote, _ListenNetworkStream = _NetworkStreamLocal,_ListenNetworkStream = _NetworkStreamLocal, _ListenSocket = _LocalSocket_ListenSocket = _LocalSocket};}} public class 客户public class Client { public TcpClient _ListenSocket;public TcpClient _ListenSocket; public NetworkStream _SendingNetworkStream;public NetworkStream _SendingNetworkStream; public NetworkStream _ListenNetworkStream;public NetworkStream _ListenNetworkStream; Thread _Thread;Thread _Thread; public 客户端( string 姓名)public Client(string Name) { _Thread = new Thread ( new ThreadStart (ThreadStartHander));_Thread = new Thread(new ThreadStart(ThreadStartHander)); _Thread.Name =姓名;_Thread.Name = Name; _Thread.Start();_Thread.Start();} public void ThreadStartHander()public void ThreadStartHander() { 字节 [] data = new byte [99999];Byte[] data = new byte[99999]; while ( true )while (true) { if (_ListenSocket.Available> 0)if (_ListenSocket.Available > 0) { int _bytesReaded = _ListenNetworkStream.Read(data,0,_ListenSocket.Available);int _bytesReaded = _ListenNetworkStream.Read(data, 0, _ListenSocket.Available); _SendingNetworkStream.Write(data,0,_ bytesReaded);_SendingNetworkStream.Write(data, 0, _bytesReaded); 控制台 .WriteLine( "(( (((( + _bytesReaded + "))))))))))" + _Thread.Name + " \ n" + ASCIIEncoding .ASCII.GetString(data,0,_bytesReaded).Replace(( char )7, '?' ) );Console.WriteLine("(((((((" + _bytesReaded + "))))))))))" + _Thread.Name + "\n" + ASCIIEncoding.ASCII.GetString(data, 0, _bytesReaded).Replace((char)7, '?'));} 线程 。睡眠(10);Thread.Sleep(10);}}}}}推荐答案您好, 确保没有其他东西正在侦听端口80,就像IIS一样。在我的本地机器上停止IIS后,我能够使用它。 HTHHi,Make sure nothing else is listening on port 80, like IIS. I was able to get this to work after stopping IIS on my local machine.HTH 这篇关于为什么这段代码不起作用(简单代理)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-03 13:53