-1
我創建了一個簡單的http服務器,但不幸的是它工作錯了。例如,當我使用Web瀏覽器時,服務器不回覆。但是,當我使用提琴手它回答這個:Http服務器故障
HTTP/1.1 200 OK
content-type: text/html
content-length: 51
這是怎麼回事? PS。我使用套接字。
服務器
static void Main(string[] args)
{
// Устанавливаем для сокета локальную конечную точку
IPHostEntry ipHost = Dns.Resolve("192.168.1.103");
IPAddress ipAddr = ipHost.AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 8080); // Порт потом поменять 80
// Создаем сокет Tcp/Ip
Socket socketListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Назначаем сокет локальной конечной точке и слушаем входящие
try
{
socketListener.Bind(ipEndPoint);
socketListener.Listen(10);
//Начинаме слушать
while (true)
{
Console.WriteLine("Waiting for a connection on port {0}", ipEndPoint);
Socket handler = socketListener.Accept();
string data = null;
// Дождались клиента, пытающегося соединиться
while (!Console.KeyAvailable)
{
byte[] bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
if (data.IndexOf("\n") > -1)
{
break;
}
}
Console.WriteLine("Text Received: {0}", data);
string theReply = "HTTP/1.1 200 OK";
theReply += "\r\n";
theReply+="content-type: text/html";
theReply += "\r\n";
theReply += "content-length: 51";
theReply += "\r\n";
byte[] msg = Encoding.ASCII.GetBytes(theReply);
handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadLine();
}
客戶
static void Main(string[] args)
{
byte[] bytes = new byte[1024];
// Соединяемся с удаленным устройством
try
{
//Устанавиваем удаленную конечную точку для сокета
IPHostEntry ipHost = Dns.Resolve("192.168.1.103");
IPAddress ipAddr = ipAddr = ipHost.AddressList[0];
IPEndPoint ipEndpoint = new IPEndPoint(ipAddr, 8080);
Socket socketSender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Соединяем сокет с удаленной конечной точкой
socketSender.Connect(ipEndpoint);
Console.WriteLine("Socket connected to {0}", socketSender.RemoteEndPoint.ToString());
string theMessage = "GET /RService2 HTTP/1.0";
theMessage += "\r\n";
byte[] msg = Encoding.ASCII.GetBytes(theMessage);
//Отправляем данные через сокет
int byteSent = socketSender.Send(msg);
//Получаем ответ
int bytesRec = socketSender.Receive(bytes);
Console.WriteLine("The Server said: {0}", Encoding.ASCII.GetString(bytes, 0, bytesRec));
socketSender.Shutdown(SocketShutdown.Both);
socketSender.Close();
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine("Exception {0}", e.ToString());
}
這是C#項目。我改變了我的代碼,但沒有任何意義,它不再工作。我使用Fiddler gor調試,並有這個有趣的事情。在響應頭文件中,我看到了我的數據(在提琴手窗口中),但是在web-brouser中沒有。它是這樣的:
\\ Server
class Program
{
static void Main(string[] args)
{
byte[] bytes = new byte[1024];
// Соединяемся с удаленным устройством
try
{
//Устанавиваем удаленную конечную точку для сокета
IPHostEntry ipHost = Dns.Resolve("192.168.1.103");
IPAddress ipAddr = ipAddr = ipHost.AddressList[0];
IPEndPoint ipEndpoint = new IPEndPoint(ipAddr, 8080);
Socket socketSender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Соединяем сокет с удаленной конечной точкой
socketSender.Connect(ipEndpoint);
Console.WriteLine("Socket connected to {0}", socketSender.RemoteEndPoint.ToString());
string theMessage = "GET /RService2 HTTP/1.0";
theMessage += "\r\n\r\n";
byte[] request = Encoding.ASCII.GetBytes(theMessage);
//Отправляем данные через сокет
int byteSent = socketSender.Send(request);
//Получаем ответ
int bytesRec = socketSender.Receive(bytes);
Console.WriteLine("The Server said: {0}", Encoding.ASCII.GetString(bytes, 0, bytesRec));
socketSender.Shutdown(SocketShutdown.Both);
socketSender.Close();
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine("Exception {0}", e.ToString());
}
}
響應主體的52個字節在哪裏? – 2013-11-14 11:22:56
這看起來不像Java。方法以一個小寫的char i開頭。 Wat語言是這個? –
@JBNizet它似乎是C#... – neutrino