c# - File transfer succeeds only the first time, fails consuquently -



c# - File transfer succeeds only the first time, fails consuquently -

this file transfer send/receive programme using sockets tcp. code included in both client , server application , works fine first time.

the sec time, side going receive gets 0 , transfer finishes. how can prepare can used many times?

public static void sendfile(string filepath) { filestream fs = new filestream(filepath, filemode.open, fileaccess.read); string filename = path.getfilename(filepath); byte[] filedata; seek { //sending file name , file size server busy = true; filesize = fs.length; byte[] filedetial = null; string detail = filename + "," + filesize.tostring(); filedetial = encoding.ascii.getbytes(detail); client.send(filedetial); //sending file info server filedata = new byte[packetsize]; count = 0; sum = 0; program.thfp.start(); // running transfer rate program.fp.statuslabel("sending data..."); transferrate.timeleft(); while (sum < filesize) { fs.seek(sum, seekorigin.begin); fs.read(filedata, 0, filedata.length); count = client.send(filedata, 0, filedata.length, socketflags.none); sum += count; program.fp.progressbarfilehandler(sum, filesize); } } { busy = false; fs.close(); filedata = null; messagebox.show(string.format("{0} sent successfully", filename)); } }

there's no problem @ code below guess .. think problem in sendfile method .. here's receivefile code .. might help

public static void receivefile() { //receving file name , file size server busy = true; byte[] commanddata = new byte[1024]; client.receive(commanddata); console.writeline(encoding.ascii.getstring(commanddata)); string[] command = encoding.ascii.getstring(commanddata).split(','); string filename = command[0]; filesize = convert.toint64(command[1]); program.thfp.start(); // running transfer rate program.fp.statuslabel("receiving data..."); transferrate.timeleft(); // receiving file info server filestream fs = new filestream(@"d:\" + filename, filemode.create, fileaccess.write); byte[] filedata = new byte[packetsize]; seek { count = 0; sum = 0; while (sum < filesize) { count = client.receive(filedata,0,filedata.length, socketflags.none); fs.seek(sum, seekorigin.begin); fs.write(filedata, 0, filedata.length); sum += count; program.fp.progressbarfilehandler(sum,filesize); } } { busy = false; fs.close(); filedata = null; messagebox.show(string.format("{0} recevied successfully", filename)); } }

i've fixed code. problem in sendfile method , in filestream

i should dispose it, can initialize 1 time again new path

finally { busy = false; fs.dispose(); //here fixed error fs.close() filedata = null; messagebox.show(string.format("{0} sent successfully", filename)); }

c# .net sockets tcp file-transfer

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -