我创建了一个服务器,只要收到新请求就打印出一行:
var server = new AppServer();
if (!server.Setup(2012) || !server.Start()) {
return;
}
server.NewSessionConnected += (session) => Console.WriteLine("new connection");
server.NewRequestReceived += (session, requestInfo) => Console.WriteLine("new request");
我从netcat连接到它:
C:\Users\sashoalm>nc localhost 2012 test test ^C C:\Users\sashoalm>
它打印new connection
但从不打印new request
,尽管从netcat键入2行。我知道netcat每次输入一个完整的行时都会将文本发送到服务器(这就是netcat的工作方式)。
编辑:我发现了另一个(措辞不太好)未回答的问题可能会问同样的事情 - How to send data to server using supersocket library,但我不完全确定这是否是同一个问题。那个人试图从代码中发送数据。
另一个相关问题 - C# SuperSocket without protocol。他们谈论那里的协议。 SuperSocket中没有指定默认协议吗?是HTTP还是别的什么?
答案 0 :(得分:1)
使用下面的简单客户端,基于此MSDN Sample
static void Main(string[] args)
{
IPHostEntry ipHostInfo = Dns.Resolve("localhost");
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 2012);
Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sender.Connect(remoteEP);
Console.WriteLine("Socket connected to {0}", sender.RemoteEndPoint.ToString());
List<string> list = new List<string>
{
"Test1\r\n",
"Test2",
"Test3\r\n",
"Test4\r\n",
"Test5\r\n",
"Test6\r\n",
"Test7",
"Test8\r\n",
"Test9\r\n",
};
foreach (string s in list)
sender.Send(Encoding.ASCII.GetBytes(s));
sender.Shutdown(SocketShutdown.Both);
sender.Close();
}
并使用稍微修改过的服务器版本:
static void Main(string[] args)
{
var server = new AppServer();
if (!server.Setup(2012) || !server.Start())
{
return;
}
server.NewSessionConnected += (session) => Console.WriteLine("new connection");
server.NewRequestReceived += (session, requestInfo) => Console.WriteLine("new request: Key={0}", requestInfo.Key);
Console.WriteLine("Press ENTER to exit....");
Console.ReadLine();
}
显示使用或不使用\r\n
作为请求的终止符的效果:
Press ENTER to exit.... new connection new request: Key=Test1 new request: Key=Test2Test3 new request: Key=Test4 new request: Key=Test5 new request: Key=Test6 new request: Key=Test7Test8 new request: Key=Test9
没有终结符的请求与下一个请求连接,直到找到终结符。