简体   繁体   中英

c# get post form data sent from browser by socket sometimes socket closes.why?

I have webserver receive data by async sockets:

var e = new SocketAsyncEventArgs();
            e.Completed += new EventHandler<SocketAsyncEventArgs>(e_Completed);

 while (true)
                { allDone.Reset();
                    mySocket.AcceptAsync(e);
                    allDone.WaitOne();
                }

and the other method:

    public void e_Completed(object sender, SocketAsyncEventArgs e)
    {
        var socket = (Socket)sender;
        ThreadPool.QueueUserWorkItem(handleTcpRequest, e.AcceptSocket);
        e.AcceptSocket = null;
        socket.AcceptAsync(e);
    }

this is the handleTcpRequest method.in this part I receive data from socket and do operation:

public void handleTcpRequest(object state)
{
   string sBuffer = "";
   string BufferTotal = "";
   byte[] secureMessage;
   Byte[] bReceive = new Byte[1024];
   var mySocket = (Socket)state;
            do
            {


                  try
                  {
                    firstBufferRead = mySocket.Receive(bReceive, bReceive.Length, 0);
                  }
                  catch (Exception ex)
                  {
                    Console.WriteLine("Error Occurred (:))) " + ex.Message);
                  }

                sBuffer += Encoding.GetEncoding(1252).GetString(bReceive, 0, firstBufferRead);
                BufferTotal += Encoding.UTF8.GetString(bReceive, 0, firstBufferRead);
            } while (mySocket.Available != 0);
.
.
.
.
mySocket.Close();
}

whats wrong? sometimes connection resets and closes. this happens when distance is far or post data not multipart. but in multipart happens rarely. more with forms not in multipart. when and where should I close socket? when I work with socket in handleTcpRequest method its local. isn't it correct? I can't find the origin of the problem

The only way to know that you've received everything in a HTTP request is to understand the HTTP request. And to understand a HTTP request you have to choices:

  1. Use a complete HTTP server
  2. Create a HTTP parser

The reason to why your code fails for multi-part data is probably because the other party sends one part at a time, which means that your code manages to do a mySocket.Available != 0 before the rest is sent.

If you want to do the latter you have to read the HTTP header (in format headerName: headervalue , do note that there are also white space rules that you have to consider). Search for a header named content-length , parse it's value as an integer. Then wait for two line feeds in a row ( \\r\\n\\r\\n ). Finally start count bytes until you have received the number of bytes that was specified in the content-length header.

ohh.. ony more thing.. pray to God that Transfer-Encoding: Chunked is not used.

My advice is that you give up on using sockets directly as it's apparent that you don't understand how they work or how to do research on them.

如果响应的标题为Connection:Close,则套接字将自动关闭。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM