클라이언트가 서버에 데이터 (예 : 영화)를 보내는 클라이언트 서버 상황이 발생하면 서버는 해당 데이터를 HDD에 저장합니다.TCP를 통한 데이터 전송
고정 된 바이트 배열로 데이터를 보냅니다. 바이트가 전송 된 후 서버는 더 많은 메시지가 있는지 묻습니다. 모든 일이 잘 진행되고 모든 데이터가 전달됩니다.
그러나 영화를 재생하려고하면 재생할 수 없으며 각 영화 (클라이언트 및 서버)의 파일 길이를 보면 서버 동영상이 클라이언트 영화보다 커집니다. 명령을 보면 화면에는 송수신 데이터의 끝 부분에 100 % 이상의 바이트가 있습니다.
내가 잘못 생각할 수있는 유일한 점은 고정 버퍼 배열이 가득 찰 때까지 내 서버가 스트림에서 읽는 것이므로 결국 바이트 수보다 많은 클라이언트가 있다는 것입니다. 그러나 이것이 문제라면 어떻게 해결할 수 있습니까?
TCP 연결이 작동하기 때문에 나는 2methods를 추가했습니다. 어떤 도움도 환영합니다.
클라이언트
public void SendData(NetworkStream nws, StreamReader sr, StreamWriter sw)
{
using (FileStream reader = new FileStream(this.path, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[1024];
int currentBlockSize = 0;
while ((currentBlockSize = reader.Read(buffer, 0, buffer.Length)) > 0)
{
sw.WriteLine(true.ToString());
sw.Flush();
string wait = sr.ReadLine();
nws.Write(buffer, 0, buffer.Length);
nws.Flush();
label1.Text = sr.ReadLine();
}
sw.WriteLine(false.ToString());
sw.Flush();
}
}
서버
private void GetMovieData(NetworkStream nws, StreamReader sr, StreamWriter sw, Film filmInfo)
{
Console.WriteLine("Adding Movie: {0}", filmInfo.Titel);
double persentage = 0;
string thePath = this.Path + @"\films\" + filmInfo.Titel + @"\";
Directory.CreateDirectory(thePath);
thePath += filmInfo.Titel + filmInfo.Extentie;
try
{
byte[] buffer = new byte[1024]; //1Kb buffer
long fileLength = filmInfo.TotalBytes;
long totalBytes = 0;
using (FileStream writer = new FileStream(thePath, FileMode.CreateNew, FileAccess.Write))
{
int currentBlockSize = 0;
bool more;
sw.WriteLine("DATA");
sw.Flush();
more = Convert.ToBoolean(sr.ReadLine());
while (more)
{
sw.WriteLine("SEND");
sw.Flush();
currentBlockSize = nws.Read(buffer, 0, buffer.Length);
totalBytes += currentBlockSize;
writer.Write(buffer, 0, currentBlockSize);
persentage = (double)totalBytes * 100.0/fileLength;
Console.WriteLine(persentage.ToString());
sw.WriteLine("MORE");
sw.Flush();
string test = sr.ReadLine();
more = Convert.ToBoolean(test);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
버퍼의 나머지 바이트를 제로되지 않습니다, 그들은의 이전 내용이 될 것입니다 버퍼 – svick