Purpose
For this program, the intent is to have a html form send uploaded files to a server running a C# listener. So far sending the file works, and it gets dealt with, but then running the file does not work right.
This has been narrowed down to a missing byte seemingly at the end of the file. Manually adding the missing byte works for testing purposes, but it is not practical for dynamic versions.
Outcome
In one file type, it is a bad footer, and it seems the same in the other accepted type as well.
This is what is accessing the sent data.
TcpListener tcp = new TcpListener(IPAddress.Any, port);
// then the stream...
int read = net.Read(buffer, 0, buffer.Length);
for (int i = 0; i < read ; i++)
bw.Write(buffer[i]);
socket_send($sock, $data, strlen($data), 1);
And the PHP that sends the data is structured in the create, connect, send, close. The file is managed by POST, file_get_contents($file)
, and filesize($file)
for the socket.
Question [updated]
Since the data is sent as a raw string, does the problem of the missing byte lie in the method of sending or receiving?
The reason for why there is the missing data is still uncertain. It originates from the sending the file that was just uploaded, using PHP, to another destination through the network. What causes the missing data?
Update
Adding an async
begin/end to the stream reading the data was tried. Although. this missing char
could be due to an oddity in the PHP socket usage.
Moving the uploaded file that was sent to the server through PHP resolved the missing byte/char problem, although it doesn’t exactly answer why there is missing data.
So the problem lies in sending from the server itself using the PHP sockets.
2
Answers
The problem was in the stage of sending the data once the HTML form completed and the file was uploaded to the server.
It went awry with the flag set in the
socket_send
section of the PHP script.Setting the flag to 0, instead of 1, stopped the truncating.
If the size of the file is larger than the size of the buffer you’re reading the file into, not surprising that the file is not downloaded completely – you read the input stream only once