1

I've created a server that just prints out a line whenever a new request is received:

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");

I connect to it from netcat:

C:\Users\sashoalm>nc localhost 2012
test
test
^C
C:\Users\sashoalm>

It prints new connection but never prints new request, despite typing 2 lines from netcat. I know that netcat sends the text to the server each time a full line has been typed (that's how netcat works).

Edit: I found another (not very well worded) unanswered question that might be asking the same thing - How to send data to server using supersocket library, but I'm not entirely sure if it's the same problem. The guy there tries to send data from code it seems.

Another related question - C# SuperSocket without protocol. They talk about protocols there. Is there a default protocol in SuperSocket when you don't specify one? Is it HTTP or something else?

sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • It prints both `new connection` and `new request` by using **telnet localhost 2012**. – jsanalytics Aug 16 '16 at 12:35
  • @jstreet You're right. It works with telnet but not with netcat. I don't understand what the difference would be. Maybe it requires \r\n line endings and netcat uses only \n, I don't know. – sashoalm Aug 16 '16 at 12:46
  • If you build a simple client and send a list of strings, some with `\r\n` and some without it, you will see the difference: the strings in the list that do not have `\r\n` are concatenated with the next one that does have it, forming one single request. – jsanalytics Aug 16 '16 at 17:15
  • 1
    @jstreet You are correct, I tested it with a simple Qt program and it works exactly as you described. I assume it's because HTTP uses `\r\n`, and SuperSocket is based on HTTP maybe, not just TCP. If you post your comments as an answer I'll accept it and award the bounty. – sashoalm Aug 17 '16 at 06:08

1 Answers1

2

Using a simple client below, based on this 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();
    }

and using a slightly modified version of your server:

    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();
    }

shows the effect of using or not \r\n as terminator for a request:

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

Requests without a terminator are concatenated with the next one until the terminator is found.

jsanalytics
  • 13,058
  • 4
  • 22
  • 43