You have to use the result from nws.Read() when you do a.Write(), like this:
int received = nws.Read(...);
a.Write(fileBytes, 0, received);
If you do not do this, it will indeed write the full buffer, instead of just what you have received.
문제
I'm working on a client-server project, which contains a client-application and a server-application. The client-application can send a file to the server-application and the server-application receives this file and write it in his folder. The projectworks, but only with a (buffer)byte-array with a length of 2.
The client-application and the server-application are both using a byte-array with a length of 2. If I choose a bigger size, such as 1024, than I have the problem, that the recieved file in the server-application has not the same size of the original file from the client-side.
Client:
Byte[] fileBytes = new Byte[2];
long count = filesize;
while (count > 0)
{
int recieved = a.Read(fileBytes, 0, fileBytes.Length);
a.Flush();
nws.Write(fileBytes, 0, fileBytes.Length);
nws.Flush();
count -= recieved;
}
Server:
long count = filesize;
Byte[] fileBytes = new Byte[2];
var a = File.OpenWrite(filename);
while (count > 0)
{
int recieved = nws.Read(fileBytes, 0, fileBytes.Length);
nws.Flush();
a.Write(fileBytes, 0, fileBytes.Length);
a.Flush();
count -= recieved;
}
해결책
You have to use the result from nws.Read() when you do a.Write(), like this:
int received = nws.Read(...);
a.Write(fileBytes, 0, received);
If you do not do this, it will indeed write the full buffer, instead of just what you have received.
다른 팁
You are not using the value of recieved
when writing.
You might want to switch to a higher level solution such as HTTP or FTP. Socket programming is quite hard and error-prone.