我正在尝试使用套接字在 C# 中编写一个简单的代理服务器(仅在 GET 上工作),下面的代码“有效”,它确实通过我的代理运行显示网页,但它大部分时间都很慢并且总是以我不知道如何处理的套接字异常...参考
class ProxyServer {
static void Main(string[] args) {
const int DEFAULT_PORT_NO = 27015;
int port = -1;
if(args.Length > 0 && args[0] != null) {
port = Int32.Parse(args[0]);
} else if(port == -1) {
port = DEFAULT_PORT_NO;
}
const int BACKLOG = 10;
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ipAddress = IPAddress.Loopback;
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);
listener.Bind(localEndPoint);
listener.Listen(BACKLOG);
Console.WriteLine(localEndPoint.ToString() + " Listening...\n");
while(true) {
Socket listenSocket = listener.Accept();
RequestHandler requestHandler = new RequestHandler(listenSocket);
Thread requestThread = new Thread(new ThreadStart(requestHandler.handle));
requestThread.Start();
}
}
}
class RequestHandler {
private Socket serverSocket;
public RequestHandler(Socket s) {
serverSocket = s;
}
public void handle() {
string header = getHeader(serverSocket);
string host = getHost(header);
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress[] addresslist = Dns.GetHostAddresses(new Uri(host).Host);
IPEndPoint remoteEndPoint = new IPEndPoint(addresslist[0], new Uri(host).Port);
clientSocket.Connect(remoteEndPoint);
sendRequest(clientSocket, header);
header = getHeader(clientSocket);
sendRequest(serverSocket, header);
const int BUFFER_SIZE = 1024;
byte[] buffer = new byte[BUFFER_SIZE];
int receivedBytes = clientSocket.Receive(buffer);
while(receivedBytes > 0) {
serverSocket.Send(buffer, receivedBytes, SocketFlags.None);
receivedBytes = clientSocket.Receive(buffer);
}
serverSocket.Shutdown(SocketShutdown.Both);
serverSocket.Close();
}
string getHeader(Socket s) {
string header = "";
while(true){
byte[] bytes = new byte[1];
int bytesRec = s.Receive(bytes);
header += System.Text.Encoding.ASCII.GetString(bytes, 0, bytesRec);
if (header.IndexOf("\r\n\r\n") > -1 || header.IndexOf("\n\n") > -1){
break;
}
}
return header;
}
string getHost(String h) {
return h.Split(new char[] {'\n'})[0].Split(new char[] {' '})[1];
}
void sendRequest(Socket s, string h) {
byte[] bytesSent = System.Text.Encoding.ASCII.GetBytes(h);
s.Send(bytesSent, bytesSent.Length, SocketFlags.None);
}
}
指针会很棒...