我创建了一个服务器,每当收到新请求时,它只打印一行:
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:Userssashoalm>nc localhost 2012测试测试C ^C:Users sashoalm> 之前它打印
new connection
,但从不打印new request
,尽管从netcat中输入了2行。我知道netcat会在每次输入整行时将文本发送到服务器(这就是netcat的工作原理)。编辑:我发现另一个(不是很好措辞)未回答的问题,可能是问同样的事情-如何发送数据到服务器使用超级套接字库,但我不完全确定如果它是相同的问题。那个家伙似乎试图从代码中发送数据。
另一个相关的问题-没有协议的c# SuperSocket。他们谈到了协议。当你没有指定一个时,在SuperSocket中是否有一个默认协议?是HTTP还是别的什么?
使用下面这个基于MSDN示例的简单客户端
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>
{
"Test1rn",
"Test2",
"Test3rn",
"Test4rn",
"Test5rn",
"Test6rn",
"Test7",
"Test8rn",
"Test9rn",
};
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();
}
显示了使用或不使用rn
作为请求终止符的效果:
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
没有结束符的请求将与下一个连接,直到找到结束符。