Writing on socket to a server with delayed answer causes the socket to resend the request

旧巷老猫 提交于 2019-12-12 04:59:57

问题


I'm trying to send a GET request to a Server with Socket Class.. THIS IS MY TEST CODE:

ConnectInternet();
String responseData = String.Empty;
String buffer = "GET /" + gLoginString + "/" + Command + "?winmob=" +
                ClassGlobalClass.VersioneJack + " HTTP/1.1";

if (SendHeader != null)
{
    buffer = buffer + "\nX: " + SendHeader;
}

buffer = buffer + "\n\n";

try
{

    Byte[] bytesSent = Encoding.ASCII.GetBytes(buffer);
    Byte[] bytesReceived = new Byte[256];
    Socket client = ConnectSocket("217.172.185.199", 5001);

    if (client == null)
        return ("Connection failed");

    // Send request to the server.
    client.Send(bytesSent, bytesSent.Length, SocketFlags.DontRoute);


    bool StopWhile = false;
    while (!StopWhile)
    {
        int bytes = 0;
        bytes = client.Receive(bytesReceived, bytesReceived.Length, 0);
        responseData = Encoding.ASCII.GetString(bytesReceived, 0, bytes);

        if (responseData != null & responseData != string.Empty)
        {
            StopWhile = true;
        }
    }
}

When I write to this sock, the server waits before answering. This causes C# to make another request. How can I avoid this?

ps: I'm on Netcf3.5

EDIT: This is the output of Netcat when sniff received packets

$ nc -l -p 5001
GET /Test/send?winmob=version HTTP/1.1
X: 24   XXXXXXXXXX      123      pass                        test 2

GET /Test/send?winmob=version HTTP/1.1
X: 24   XXXXXXXXXX      123      pass                        test 2

GET /Test/send?winmob=version HTTP/1.1
X: 24   XXXXXXXXXX      123      pass                        test 2

GET /Test/send?winmob=version HTTP/1.1
X: 24   XXXXXXXXXX      123      pass                        test 2

GET /Test/send?winmob=version HTTP/1.1
X: 24   XXXXXXXXXX      123      pass                        test 2

来源:https://stackoverflow.com/questions/4656041/writing-on-socket-to-a-server-with-delayed-answer-causes-the-socket-to-resend-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!