클라이언트에서 데이터를 가져 와서 로컬 호스트의 로컬 드라이브에 저장하고 있습니다. 221MB의 파일을 검사했지만 1Gb의 파일 테스트에서 다음 예외가 발생했습니다.클라이언트 측 파일의 서버 측에 System.OutOfMemoryException이 있습니다.
형 'System.OutOfMemoryException'처리되지 않은 예외가 mscorlib.dll 다음
발생 예외 밖으로 줄기 서버 측의 코드이다.
업데이트]
서버 :
public void Thread()
{
TcpListener tcpListener = new TcpListener(ipaddr, port);
tcpListener.Start();
MessageBox.Show("Listening on port" + port);
TcpClient client=new TcpClient();
int bufferSize = 1024;
NetworkStream netStream;
int bytesRead = 0;
int allBytesRead = 0;
// Start listening
tcpListener.Start();
// Accept client
client = tcpListener.AcceptTcpClient();
netStream = client.GetStream();
// Read length of incoming data to reserver buffer for it
byte[] length = new byte[4];
bytesRead = netStream.Read(length, 0, 4);
int dataLength = BitConverter.ToInt32(length,0);
// Read the data
int bytesLeft = dataLength;
byte[] data = new byte[dataLength];
while (bytesLeft > 0)
{
int nextPacketSize = (bytesLeft > bufferSize) ? bufferSize : bytesLeft;
bytesRead = netStream.Read(data, allBytesRead, nextPacketSize);
allBytesRead += bytesRead;
bytesLeft -= bytesRead;
}
// Save to desktop
File.WriteAllBytes(@"D:\LALA\Miscellaneous\" + shortFileName, data);
// Clean up
netStream.Close();
client.Close();
}
내가 데이터를 다음 클라이언트 측에서 먼저 파일 크기를 얻고있다.
1). 버퍼 크기 나 다른 기법을 늘려야합니까?
2). File.WriteAllBytes()
및 File.ReadAllBytes()
이 차단되어 PC가 멈추는 것처럼 보입니다. 서버 쪽에서받은 파일의 진행 상태를 알려주는 비동기 메소드가 있습니다.
1GB 바이트 배열을 할당하고 있습니다.평범한 컴퓨터에서 메모리가 부족한 것도 당연합니다. 스트림을 사용하여 chunk-by-chunk 파일에 데이터를 씁니다. –
전체 파일을 메모리에 보관하지 마십시오. 파일 스트림을받은 즉시 파일 스트림을 사용하여 바이트를 디스크에 씁니다. 또한 그 시점에서 진행 상황을 계산할 수 있습니다. –
어떻게 그 시간을 계산할 수 있습니까? @DavidLibido – Khan