Search code examples
c#socketsmultipart

c# tcp server browser sometimes send extra data into buffer in multipart file upload


sometimes my multipart section of my webserver recieves additional data for example add some binary data in middle of file. this is code of this part:

string content = "";

content = sBuffer;                                                
int boundaryContentStart = content.IndexOf("boundary=") + "boundary=".Length;
string boundary = content.Substring(boundaryContentStart, content.Length - boundaryContentStart - "boundary=".Length);                                                
string boundarSeprator = boundary.Remove(boundary.IndexOf("\r\n"));

string temp = sBuffer;

while (!content.Contains(boundarSeprator + "--"))
{
    mySocket.Receive(bReceive, bReceive.Length, 0);

    temp = Encoding.GetEncoding(1252).GetString(bReceive);
    content += temp;

    temp = "";
}  

sBuffer is my string recieve buffer which is 1024 byte array why this happens and sometimes browser sends extra information in middle of file part in multipart form post.

whole content is added to conetent variable as you see in code above.

remember this not always happens. just sometimes wreck my files is that a mechanism that browser sometimes resend some part of data to server that I dont know? mySocket is a tcp socket. thanks for help


Solution

  • You are ignoring the return value of mySocket.Receive(bReceive, bReceive.Length, 0); which indicates the number of bytes actually written to the buffer by the read, you will therefore be re-reading data left in the buffer from a previous read when Receive reads less than bReceive.Length bytes.

    You should instead store the return value from Receive in a local variable, and use the overload of GetString that accepts a start and length (passing it as the length parameter).

    You should also check if the receive reads 0 bytes, indicating the connection has been closed, otherwise your code will enter an infinite loop it the other side closes the connection without sending the expected data.

    Finally, depending on the amount of data you're expecting, you might want to rethink how you search for the boundary separator on the received content. If we assume the content to be 1MB, and it gets received in 1KB chunks, your content.Contains will first be run over 1KB, then 2KB, 3KB, etc. by the time the whole 1MB has been read, you will effectively have performed Contains over about 500MB of data. Similarly, the repeated concatenation of the received data to content is rather inefficient as a new string is created, requiring all of the data to be copied each time.