File Transfer App for Windows - Part 2 - Send Files Between PCs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi there this is the second part of a series of videos where i go through the creation of a simple file sharing application for windows i'm building the application up in parts to make it easy to test as i go along so in this part i'm going to create two test applications to copy a file from one pc which is going to be the sender to another tcp which is a receiver using tcp so without further ado let's get started what i want to do is um i want to be able to send two files between these these computers and what i'm going to do is i'm going to create my own protocol and the protocol is going to be very very simple it's just going to be very similar to http uh it's going to have well let's define it um so what i want to do is i want to call this sender so i'm going to have a new class and then let's just call this send file um and then we're going to add that let's call it transfer file uh transfer file manager well no we're gonna have a file manager for there we'll call it transfer file voila that'll do and this is going to transfer a file between to one place and between your local machine and a client so i'm going to say um i need to have a destination and i need to have a port so i'm going to say public transfer file file and that transfer file is going to be a hostname and that's really all i need apart from string file name so i'm going to say hostname equals hostname filename equals filename um read-only field generate read-only field um and then we want to have like a start transfer actually we just need to start only um and we want to create a client host that we want to send across and then we want to send that information so um we're going to do um let's have private ttp client um client i'm going to say client uh equals new tcp client and then we're going to pass in what we're going to pass in we need the host name in the port so we know the port number host name um but maybe we can sort of specify it here as well so we've got well we want to send the file name the hostname and then import equals 54 000. so we get that there um port so we need to remember that so because we want to so we're not necessarily going to be changing it from 54 000 but we could configure it to be something else but that means that everything has to change so uh if you're on this network and you want to receive a file then you're gonna have to change it to the known port so in this case we're changing it to 54 000. 54 000 might not work for you as the default port so we need to change it um in a config which we'll get to so we've got a client here we would say client.com um and then we specify the hostname report there okay okay so we don't need that there that there and then we want to do client dot send get stream send where's it right async and then we want the buffer the offset and the count okay uh client okay i don't know if i want right begin right that's what i want begin right okay begin right and then i want the buffer the offset the size and the callback so i'm going to call this buffer offset is zero um file size and then the callback is going to be [Music] right the result and then the state is going to be the client um so where am i getting the buffer from well the buffer is from the file name so actually i actually want that to be something else so what if i wrote it out a memory stream okay let me try this ms equals new memory stream ms.write string um what i want to write i want to write a buffer um and let me do right header so i'm going to create a function here called write header and right header is going to take in the memory stream it's going to take in the file name uh it's also going to take in uh that's it it's just going to be that so then i'm going to do private void right header um actually let me do var buffer equals that um uh create offer writing this on the fly so it might not be perfect and we need our memory stream in there uh and we also need the file name which we've got in there anyway so we don't really need that and that's going to return back a array of bytes um well in that case do i need the memory stream in there probably not let's have this here um i think i'm just moving these things right i'm sorry um so we create buffer there uh some memory stream is that and then i want to be able to write to the memory stream so i want to say private void right string memory stream string s uh and then i want to say bar bytes equals in coding dot ascii dot get bytes s and then i'm gonna do ms.write bytes uh offset zero offset in the buffer to begin copying okay and then the count is going to be byte spot blank okay and that's that so i want to do create buffer this is going to be return ms.2 array boom uh let's have using uh [Music] far uh return buffer uh buffer equals ms.2 array oh there you go returns the array of unsighted bytes from which the stream is created okay so what's the difference in that into array then write the stream contents to a byte array regardless of memory stream position property okay and we just need that all right well let's see if that works um so we get the byte in there and then we want to write out the header okay so the header is going to be write string ms and then it's gonna be the file name but just the file name so it's gonna be um path dot get file name file name so it's just the name of the file um plus that so that's gonna give us that we now want to have the size of it as well okay so we want to write that in the next line because we want to have something like um my file.txt and then the size of it which is going to be 673 bytes okay and then we do another blank line and then we do the data so that's okay and so that's going to be the format of it so that's our first one there and then the next one we want to get the file info so we're going to do uh file info e f i equals file info uh equals new file info um file name and i'm gonna do write string ms comma failing for dot length um go to string plus that there um okay so that that is basically signaling that the the slash r slash n slash r slash n which is um return new line return new line twice is going to signify the end of the array and now we want to do is we want to write the contents of a file into the array as well so then we do um um bar oops bar file contents equals um file dot read all bytes file name and then we do ms.write file contents offset file content stop length okay and then we'd return that buffer back to there okay so our file size is buffer dots length and our right result is going to be private void um ie sync result result if the result is complete um and then we need to do var client equals result dot state as tcp client um uh var bytes percent equals client [Music] we end that there let me close our client so we do client dot close boom okay so we don't need that don't need that so we're now we take the file name the host name the port uh so that's the that's where we're gonna send it to um we then create a connection to it we get the buffer information then we write that buffer out and then once it's complete we end the write close the stream everything's good and then create buffer is going to do just that so it's going to create a buffer here um using the memory stream it's going to write that out it's going to write that and then it's going to write the contents there okay and then the way back um so that's gonna be start transfer and then we need to have one that's gonna listen so this is transfer file uh we also need to make this public we also need to wait for the file um and so what we need to do is we need to get uh do we do everything in here i think maybe we do everything in here let me just i think what we can do is we can do public oh no i don't want to do that transfer file um i think we have a receive file as well so let's have a receive file i mean we could combine the two but i'm not gonna bother file public class receive file um and then for receive file i'm going to borrow quite a bit from this so i'm going to say public void uh public receive file um tcp client um well dcp receive file i need to wait for an incoming connection for this and then i need to spin it off so i need to do public receive file port um sure okay let's do this we for now private tcp listener listener goes support tcp listener and then we want to put that into port so we're listening on there oh yeah it doesn't like that it's uh where's the other one oh yeah local endpoint ip endpoint new ip endpoint uh ip address dot any [Music] accept socket async except tcp client async nope start accept begin accept tcp claim that's the one connected listener okay so the client is connected we want to be able to transfer the file um so that's going to happen here private void nice result results um if result result dot is completed um we want to get our async state rstcp listener at our client equals listener dot end accept tcp result and now we want to listen on there with that client so you want to say client dot get stream begin read and now we need to specify a buffer and offset a size and a callback okay um we don't know what size that buffer is going to be so we need to put that in somewhere so i think we need to click create well okay let's assume we've only got one client just now so i'm going to do private byte buffer new buffer new bite eight um let's give it 64k we'll see we'll see what happens uh so we're going to do client dot get stream begin read buffer offset buffer dot length uh client receive and then it's gonna be client okay um and now we need to put this someplace as well so um i i think what we can do is we should probably have um um memory stream should we have a memory stream probably okay let's just do a membership because again it's going to be um just one client just now private memory stream uh let's just call it ms okay um and then um i'm going to do private void client received async result result um uh what do we got here we've got um we need to know the size that we're receiving so you need to read the header okay so uh that is gonna be we're gonna assume that we get in the full size to start with and if we don't we need to we need to rethink it okay so um if result dot is completed so in other words this operation is completed in other words now what that means though is it could mean that we've received all the data that we need to receive uh or it could mean that we've we've filled that buffer so if we fill the buffer that means we know we get more data if we haven't filled the buffer and it's complete that means that we don't need to read any more so we can see if uh actually it's gonna be var bytes received we need equals var client equals results dot async state as tcp client uh client get stream and read result now if we detect that the buffer is length is less than the length the bytes received is less than the length that means we're done so if bytes received is less than buffer dot length uh actually let's do we haven't received the header yet so let's go back to private bool received our processed header so if we've not processed the header we need to do that processed header equals true so there so we say if not processed header process header um in the process header we're going to read in the string so we need to find out where in the array the end of the header is and the way we can do that is we can say private void um it's going to be string get header um and then we're going to pass in byte buffer and then we're going to find inside the string where the if we go back to the transfer file here where this part of the that basically this part here is and once we find that that means we know where the end of the header is which means you can parse the header quite easily so um i'm going to do for int i equals 0 i less than buffer minus length minus 4 i plus plus so we want we want to check um the the four position we want to check the the the four characters the slash r slash n slash r slash so i want to say char uh equals buffer i buffer i plus one [Music] oh bite okay actually it's going to be checked there and then we do charge our three equals jar i plus two and it's give me three four so if ch1 equals slash r and ch2 equals slash n um you could probably do this as a regex but my regex is pitiful so i'm just gonna do it this way uh if you're gonna do it with regex and you want to leave a comment below um just do so that would be great and it will save lots and lots of time for other people so three three equals that and c4 equals that then um i need to process the um let's do var point pause equals minus one uh pause equals c uh i plus three uh break uh if pause is greater than zero then i want to get this sub string in that buffer so i want to say var header equals um actually it's going to be var raw equals encoding dot ascii dot get string and then i can pass in the bytes which is the buffer and then the size which is index is to do zero and then the count is going to be pause and then i can say var split equals raw dot split and then ignore string split remove empty entries return split otherwise i'm going to return null so okay just to recap here so get header what this is going to do is it's going to find the slash r slash n slash r slash n position it's then going to take a note of that position and then we're going to use this get raw thing and then we're going to split that back out and then that is going to um uh return that position there but actually what i think i want it to do is get into get header size um and then i want to return pause there okay so i want to get the head of size um so i want to say var header size equals get header size um buffer size is greater than zero now what i want to do is i want to take that buffer uh header size there and now i can save our file name equals split zero var um length equals split one i can do in long dot parse that because going back to the transfer file our first line is myfile.txt and the second line is length so now [Music] um actually that's going to and be that's going to be length so i'm going to say private string file name private long blank [Music] so now i want to copy the remainder of the bytes into um here oh yeah i want to have processed error true that there okay so now that we've done that i want to process the remainder of it out so i want to say the bytes received is that there but i want to remove the header from that so i'm going to say var length of data equals and it's going to be bytes received right received minus the length of the header because we're ignoring everything from the header um and then we want to copy those bytes into um the memory stream so i'm going to do memory stream dot write and it's going to be buffer and then the offset is going to be um header size plus one and then the count is going to be length of data okay right so uh if we're not processed how do we do that otherwise um what we want to do is we want to write the buffer to there uh okay so length of data is that so length is that okay so so we want to say if length if yeah uh if length uh no if ms. length is less than length we want to copy those those in there but we're going to do that as an elsa so if the memory stream so if the the length of the memory stream is less than the length that we get from here we want to add we want to keep adding those values so we want to say want to copy those in there so we're going to say ms.right and then it's going to be buffer and then the offset is going to be zero and then the count is going to be bytes received okay and then at the very end here we want to say if ms.length is still less than length we want to go around again so the way we do that is we just want to do this so we want to begin read we want to do that for the buffer but we also want to do array dot clear i'm going to array clear and then that's going to be the the array which is me a buffer index 0 buffer [Music] length and that will clear everything in the array so we don't need to to start again else we're done okay so we want to close the connection uh so we want to say client dot close and then we want to process the file so um we want to say um file dot write all bytes file name and then ms.2 array ms.q close close the current stream release any resources instead of calling this method ensure the stream is properly disposed okay i suppose okay let's do that there and then uh let's not have that there um and then this new memory stream okay so only create that when the client gets uh added to it so that's our client received so if we haven't processed the header process the header um and then add the remaining data um to yeah add the remaining data to there and if that's still shy of the amount of data that's to be received it's going to end up going in here otherwise we close it and we send off that file there and then this one here is bytes received so if the length is still less than that then we want to do that there okay so what we're going to do is we're going to set this up so that this other computer over here is going to send data to this one so we're just going to make sure that we've we've done everything okay here um so everything seems to be succeeded so for this one i'm going to create another another project add new project and this is going to be a console application again and then this one's going to be called um receive file uh next console upped on everything i keep doing that console.net framework next um receive file create okay and so receive file is just going to wait for a file to be received so you're going to do var receive file equals new receive file uh then the port is gonna be fifty four thousand c file dot oh do not start that oh it just starts oh i can't have that uh that's not right let's do that public void listen okay let's do that oh yeah we can't have that there now so that's got to be there okay get rid of that that tidy things up a bit then we go back to here and then we do receive file.listen okay remove all of these okay and then we're gonna have send file as well so we're gonna do add new project dotnet framework next uh send file um and i'm going to do create and send file is going to be very basic it's going to be var hostname equals args 0. bar file equals args one because i'm not going to do any parsing here this is just to test things um we're just testing these these um these pieces so we can build them up later on that's kind of the part of this project's about or the reason why we're doing this part of the reason why we're doing this project this way our transfer file equals new transfer file uh and then we specify the file name the host name and we don't need the port transfer file dot send or start so i call it start and that should be it okay all right so now uh if i go into here uh get status uh get add store about sln what oh that's because i'm way way too high up uh get add star dot cs hit add star dot cs proj hit and start our sln status uh i don't want that one [Music] either uh oh get restored okay and then this other one here don't want this one either uh properties okay and i don't want anything under one app config get add up i don't want any of those bins and objects i need to do by ignore oh did i only do that in there ah damn it okay um i had thing to do in here don't know uh so receive uh where are we we're gonna wait for it i need to get ignore and send file and receive file okay c file dot get ignore and send file okay so that is everything done and now i need to put it on the other machine so what we've got here is um and i'm not entirely sure where i'm going to put this but i have um my other machine running wait for it and i'm going to call send it here and that's just going to kind of show where those like you can do this across the network so i'm going to run it on this machine now so i'm going to type in wait for it dot exe and it's going to run there and of course because this is i've never run this on here it's going to make me do that so i'm going to click on allow and it's going to take a couple of seconds so now that's running there and now i'm going to run send it here and that is going to appear over there and you can see now that this is now the problem i had before was i wasn't able to send it on the same machine because i had to change the ports which i don't want to do because i want everything to run off of port whatever we call it 54 000 so you can only have one instance running on a pc at any time and that's kind of what i want so uh yeah like i said over here we've got hello from this ip address here so if i do ip config you'll see that this is our local ip address here so everything's working it's all good it's exactly what we're gonna be so handy and the way to do that on the other machine is i do get fetch get pulled that's not right oh i never pushed that's why push okay uh get fetch there we go boom okay so we go back to our this one here reload so we should now have all those ones in there so i'm going to rebuild this okay build succeeded so i'm going to go back to our file in here and we're going to go to a send file then debug um i'm going to start this uh and then i'm gonna have a file called hello world because you know why not hello world okay and i'm just gonna call it hello world okay so that is uh uh hello world is 45 bytes so it's well within that limit there so we go back to this machine here and then i do receive file debug receive file i should get the the warning i do so i'm going to click on allow access and then for this one here i'm going to use the ip address that we already established from the other one which if i um where are we if i do the new tab new console dialog current folder okay so if i do this which is 192. um send file and then it's the hostname which is 192.168. [Music] and then i go back to actually i don't need to go back to here because this is going to be oh receive file just stopped oh well that's not good it should listen uh what oh that's why oh shoot okay well that's that's okay because uh and you just need to do uh while true thread.sleep okay because this is the command line it just immediately exits after that which is not great save file okay what build okay there we go oh okay so that's going to send that there i've got this other window open here because i want to do cd projects cd uh cd ben okay all right so we don't have anything in there just now so i'm going to send this file across here and this will pop up a message saying i need to send it across or has it so the big truth is if i do dir just now will it say hello world there it is there um start hello world.txt boom there you go this is the hello world message and that's it that's that's those files transferred across so but it's working uh we need to debug it i don't know why it says 46 here and it is um 45 over there we're gonna have to figure that out my guess is that it is putting in an extra character somewhere uh but we can we can debug that later on but yeah that is all right this is coming together so this one here should still be receiving files which means we should be able to send another file over so if i do um touch another let's just call it another and then i do vi another dot txt i this is another file uh and then i do send what is the one i did send file and this will be another will this work will it do two in a row well we'll see this could be a complete epic fail this is why we don't do live tests but it did seem to send it so let's see if that is received over here no it didn't well that's that's for the next video but um yeah that's what we've got so far thank you so much for taking the time to watch this video it's part of a series of videos where i create a simple file transfer application for the windows operating system so windows 10 so on if you like this video please give it a thumbs up and share really really does help the channel and if you want timely reminders of when another part of the series or anytime i post up a video up then please hit the subscribe button and notification bell and youtube will work its magic and give you a notification when one of those videos goes up but yeah thank you so much again for watching and i will catch you in the next one bye
Info
Channel: Sloan Kelly
Views: 319
Rating: undefined out of 5
Keywords: networking, winsock, c-sharp, csharp, c#, coding, gamedev, file transfer, send files, share files, copy files, network copy, rcp, warpinator, warpinator for windows, windows file share, sharing files, programming, dev, developer, winforms
Id: NocUd_LUqRc
Channel Id: undefined
Length: 54min 34sec (3274 seconds)
Published: Sun Jun 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.