如何将消息发送回浏览器或localhost,例如,如果我想显示名为的消息....这是在浏览器中测试。
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MyTcpListener
{
public static void Main()
{
try
{
// Set the TcpListener on port 13000.
Int32 port = 80;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
// TcpListener server = new TcpListener(port);
TcpListener server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
// Enter the listening loop.
while (true)
{
Console.Write("Waiting for a connection... ");
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
data = null;
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
int i;
// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine(String.Format("Received: {0}", data));
// Process the data sent by the client.
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
// Send back a response.
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Sending message..");
}
// Shutdown and end connection
client.Close();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
}
答案 0 :(得分:1)
using(StreamWriter sw = new StreamWriter(stream))
{
sw.Write("<html><body>Hello There!</body></html>");
}
这应该将HTML写入输出流。确保在此之后关闭流。并关闭TcpClient。
答案 1 :(得分:0)
这似乎是一个控制台应用程序而不是一个网站,因此Console.WriteLine
语句中生成的输出将在Visual Studio中运行时进入调试控制台,或者如果你在屏幕上显示它们从命令提示符处将其作为已编译的应用程序运行。
这是什么类型的项目?
答案 2 :(得分:0)
在检索0字节之前,您应该阅读,直到找到空白行。在打开的网络连接的最后一位调用Read()只会阻塞,直到浏览器发送更多数据(这不会因为它发送了所有内容而正在等待你)。
class MyTcpListener
{
public static void Main()
{
try
{
// Set the TcpListener on port 13000.
Int32 port = 80;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
// TcpListener server = new TcpListener(port);
TcpListener server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
// Enter the listening loop.
while (true)
{
Console.Write("Waiting for a connection... ");
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
// Get a stream object for reading and writing
using(NetworkStream stream = client.GetStream())
using (StreamReader sr = new StreamReader(stream))
{
List<byte> msg = new List<byte>();
// Loop to receive all the data sent by the client.
string data;
while ((data = sr.ReadLine()) != "")
{
Console.WriteLine(String.Format("Received: {0}", data));
// Process the data sent by the client.
data = data.ToUpper();
msg.AddRange(System.Text.Encoding.ASCII.GetBytes(data));
}
// Send back a response.
stream.Write(msg.ToArray(), 0, msg.Count);
Console.WriteLine("Sending message..");
}
client.Close();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
}