streamwriter - Error on writer.close() of GetRequestStream of FtpWebRequest in VB.NET -
so have simple ftp transfer function works on buffer send chunks of files server. tried uploading 300mb file , there absolutely no issue after function had finished sending every single byte of 1.5gb file, function crashes on writer.close().
i received following error: "the underlying connection closed: unexpected error occurred on receive."
public function upload(byref fullpath string, byref filename string) boolean if filename <> "" try dim clsrequest ftpwebrequest = directcast(webrequest.create("ftp://myserver" & filename), ftpwebrequest) clsrequest.credentials = new networkcredential(user, password) clsrequest.method = webrequestmethods.ftp.uploadfile clsrequest.keepalive = true clsrequest.timeout = -1 clsrequest.usepassive = true dim fileinfo fileinfo = new fileinfo(fullpath) dim bfile() byte = new byte((fileinfo.length) - 1) {} clsrequest.contentlength = fileinfo.length dim bytesread integer dim buffer(4096) byte using reader filestream = fileinfo.openread using writer stream = clsrequest.getrequeststream bytesread = reader.read(buffer, 0, buffer.length) if bytesread > 0 writer.write(buffer, 0, bytesread) end if loop while bytesread > 0 writer.flush() ''crashes here >>>>>>> writer.close() end using reader.flush() reader.close() end using return true catch ex exception return false end try end if end function
edit:
so found "solution" , consists of doing .abort() on connection after bytes have been transferred. works , don't see downsides find kind of sloppy so. there real solution?
from here:
this problem occurs when server or network device unexpectedly closes existing transmission control protocol (tcp) connection. problem may occur when time-out value on server or on network device set low. ... problem can occur if server resets connection unexpectedly, such if unhandled exception crashes server process. analyze server logs see if may issue.
there multiple solutions presented.
also, using
block handling closing/disposal of writer
(and 1 reader
), don't need dispose of manually, .close
doing.
from stream.close method:
this method calls dispose, specifying true release resources. not have call close method. instead, ensure every stream object disposed. can declare stream objects within using block (or using block in visual basic) ensure stream , of resources disposed, or can explicitly call dispose method.
Comments
Post a Comment