C# 用C编写WebSocket服务器#
我写的代码像。我的代码:C# 用C编写WebSocket服务器#,c#,websocket,C#,Websocket,我写的代码像。我的代码: static void Main() { TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 8080); server.Start(); Console.WriteLine("Server has started on 127.0.0.1:8080"); Console.WriteLine("Waiting for a connection...");
static void Main()
{
TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 8080);
server.Start();
Console.WriteLine("Server has started on 127.0.0.1:8080");
Console.WriteLine("Waiting for a connection...");
TcpClient client = server.AcceptTcpClient();
Console.Write("A client connected.");
NetworkStream stream = client.GetStream();
//enter to an infinite cycle to be able to handle every change in stream
while (true)
{
while (!stream.DataAvailable) ;
byte[] bytes = new byte[client.Available];
stream.Read(bytes, 0, bytes.Length);
//translate bytes of request to string
string request = Encoding.UTF8.GetString(bytes);
if (new Regex("^GET").IsMatch(request))
{
byte[] response = UTF8.GetBytes("HTTP/1.1 101 Switching Protocols" + Environment.NewLine
+ "Connection: Upgrade" + Environment.NewLine
+ "Upgrade: websocket" + Environment.NewLine
+ "Sec-WebSocket-Accept: " + Convert.ToBase64String(
SHA1.Create().ComputeHash(
Encoding.UTF8.GetBytes(
new Regex("Sec-WebSocket-Key: (.*)").Match(request).Groups[1].Value.Trim() + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
)
)
) + Environment.NewLine + Environment.NewLine);
stream.Write(response, 0, response.Length);
}
else
{
byte[] response = UTF8.GetBytes("Data received");
response[0] = 0x81; // denotes this is the final message and it is in text
response[1] = (byte)(response.Length - 2); // payload size = message - header size
stream.Write(response, 0, response.Length);
}
}
}
我试着调试它,但它运行正常
TcpClient client = server.AcceptTcpClient();
然后停下来。它显示等待连接…并停止。由于所提到的#Blorgbeard也应该有客户端,您可以检查此代码
或者像这样的简单代码:
var tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");
tcpclnt.Connect(IPAddress.Parse("127.0.0.1"), 8080);
// use the ipaddress as in the server program
Console.WriteLine("Connected");
Console.Write("Enter the string to be transmitted : ");
String str = Console.ReadLine();
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
Console.WriteLine("Transmitting.....");
stm.Write(ba, 0, ba.Length);
byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(bb[i]));
tcpclnt.Close();
var tcpclnt=new TcpClient();
Console.WriteLine(“连接…”);
tcpclnt.Connect(IPAddress.Parse(“127.0.0.1”),8080);
//使用服务器程序中的IP地址
控制台。写入线(“连接”);
Console.Write(“输入要传输的字符串:”);
String str=Console.ReadLine();
Stream stm=tcpclnt.GetStream();
ascienceoding asen=新的ascienceoding();
byte[]ba=asen.GetBytes(str);
Console.WriteLine(“传输…”);
stm.Write(ba,0,ba.Length);
字节[]bb=新字节[100];
intk=stm.Read(bb,0100);
for(int i=0;i
那么,您是否尝试连接到它acceptcpclient
阻塞线程,直到有东西真正连接。尝试导航到http://localhost:8281
在您的web浏览器中。哇!我试过了,成功了。多谢各位#Blorgbeard,我想发送一个字符串:“你好,word”,你能帮我吗?看来你对C#没有太多经验。我建议从一个比这更简单的任务开始。然后几个月后再来。#佩曼我试过运行代码。下图中出现错误:是否创建了控制台应用程序项目?#Peyman I运行代码:。下面显示错误。我运行代码follow“或像这样的简单代码:”,它显示错误行:tcpclnt.Connect(IPAddress);错误:“System.Net.IPAddress”是类型,但用作“变量”。如果我删除tcpclnt.Connect(IPAddress),它将显示错误:“System.dll中发生类型为'System.InvalidOperationException'的未处理异常。其他信息:不允许在未连接的套接字上执行该操作。”行内流stm=tcpclnt.GetStream()