c# - Multiple file sending over to a server -
i have made windows service motive behind send multiple files designated server.
private void sendfile() { timer.stop(); registrykey thelocalmachine = registry.localmachine; registrykey thesystem2 = thelocalmachine.opensubkey(@"software\\networkusagemonitoring\\", true); registrykey interfacekey4 = thesystem2.opensubkey("usagerecorder", true); string serverno = interfacekey4.getvalue("serverno").tostring(); (int = 0; < netarr1.length; i++) { (int j = 0; j < netarr2.length; j++) { if (netarr1[i].name == netarr2[j]) { ipendpoint ipend = new ipendpoint(ipaddress.parse(serverno), 5656); socket clientsock = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.ip); try { if (file.exists(@"c:\" + netarr1[j].name + "_record.xml")) { filename = (@"c:\" + netarr1[j].name + "_record.xml"); filename = filename.replace("\\", "/"); while (filename.indexof("/") > -1) { filepath += filename.substring(0, filename.indexof("/") + 1); filename = filename.substring(filename.indexof("/") + 1); } byte[] filenamebyte = encoding.ascii.getbytes(filename); if (filenamebyte.length > 850 * 1024) { return; } byte[] filedata = file.readallbytes(filepath + filename); byte[] clientdata = new byte[4 + filenamebyte.length + filedata.length]; byte[] filenamelen = bitconverter.getbytes(filenamebyte.length); filenamelen.copyto(clientdata, 0); filenamebyte.copyto(clientdata, 4); filedata.copyto(clientdata, 4 + filenamebyte.length); clientsock.connect(ipend); clientsock.send(clientdata); clientsock.close(); recorded[j] = 0; file.delete(@"c:\" + netarr1[j].name + "_record.xml"); } else { update1network_interface(); } } catch (exception ex) { if (ex.message == "no connection made because target machine actively refused it") { logevent("no connection made because target machine actively refused it", eventlogentrytype.information); break; } } { if (clientsock != null) { logevent("client socket closed", eventlogentrytype.information); clientsock.close(); sendfile(); } } } } } restart(); }
but service file start execution..there seem 3 files needs sent server tends send 1 neglecting other one...as in code snippet under 2 loops checking existence of files , if present need transmitted.
as in server side testing purpose following code
private void form1_load(object sender, eventargs e) { ftservercode.receivedpath = (@"c:\receiving\"); if (ftservercode.receivedpath.length > 0) backgroundworker1.runworkerasync(); } } class ftservercode { ipendpoint ipend; socket sock; public ftservercode() { ipend = new ipendpoint(ipaddress.any, 5656); sock = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.ip); sock.bind(ipend); } public static string receivedpath; public static string curmsg = "stopped"; public void startserver() { try { curmsg = "starting..."; sock.listen(100); curmsg = "running , waiting receive file."; socket clientsock = sock.accept(); byte[] clientdata = new byte[1024 * 5120]; int receivedbyteslen = clientsock.receive(clientdata); curmsg = "receiving data..."; int filenamelen = bitconverter.toint32(clientdata, 0); string filename = encoding.ascii.getstring(clientdata, 4, filenamelen); binarywriter bwrite = new binarywriter(file.open(receivedpath +"/"+ filename, filemode.append)); ; bwrite.write(clientdata, 4 + filenamelen, receivedbyteslen - 4 - filenamelen); curmsg = "saving file..."; bwrite.close(); clientsock.close(); startserver(); } catch (exception ex) { curmsg = "file receving error."; } } }
what want check existence of files , transmit them server , if there error file should re transmitted....
any highly acknowledged.....
on first glance, using netarr1[j].name in inner loop... should netarr1[i].name (or netarr2[j].name since executing when equal)??
maybe , j hard distinguish in text, maybe more descriptive naem
also system.io.path.combine use on file paths....
Comments
Post a Comment