単純な http サーバーを作成しましたが、残念ながら正しく動作していません。たとえば、Web ブラウザーを使用すると、サーバーが応答しません。しかし、私が Fiddler を使用すると、次のように答えます。
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-browser には表示されません。次のようになります。
\\ 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());
}
}