1

Apologies if my terminology is not correct with this question, but bear with me. I know what I want to achieve, just not sure what the proper names are for the classes/functions etc.

I need to make a client/server application using tcplistener and tcpclient which will allow me to send both strings and files... I think.

So in layman's terms, I want to make a server application that will listen for connections from a client on a specific port. The clients will essentially connect to the server and say "hey, I'm client 172.16.10.12. I'm going to send you a file called test.txt, which is 2330k in size. The file's MD5 hash is xxxxxxxxxxx".

Once that communication has taken place, I want the client to then send the file 'test.txt' to the server; the server saving the file to an appropriate location before then generating the file's MD5 hash and comparing it to the MD5 and file size the client proposed, before finally reporting back to the client 'Ok' or 'Success'.

If the file size or MD5 differ, it will be 'Fail'.

I'm not asking for anyone to do this for me, but for someone to nudge me in the right direction. All of the tutorials/examples I seem to find on here or youtube etc seem to focus on either files or strings. I cant seem to find one which would cater for both, which is what I think i may need, although I may be wrong.

I hope that all makes sense! Huge thanks in advance for any help on this. This is the first time i've played with TCP functions, having always used FTP before because of perceived ease.

John
  • 755
  • 1
  • 18
  • 46
  • You can see [**my answer to this question**](http://stackoverflow.com/a/35240061/3740093) which will help you implement a proper way to differentiate the data (_"packets"_) you send. – Visual Vincent May 08 '16 at 09:31
  • As for splitting it up in chunks (as mentioned in my comment on user3815486's answer) you would first have to send the server the exact amount of bytes that the file is, then you start sending the 8192 byte chunks to it until you've read the whole file. **The server** would have to do this everytime it receives a file-part packet: `8192 bytes -> Write to file -> Check if we've received the whole file (match against what the client sent)`. – Visual Vincent May 08 '16 at 09:40

1 Answers1

1

I believe for this small file you can convert a file to bytes on client side and then send it over tcp as byte array and the server application will rebuild the file from the byte array with File.WriteAllbytes.

Save byte array to file

Here is how to get bytes from file. PS I am not very experienced just posting something.. Reliable way to convert a file to a byte[]

This should work out of box for sending byte array as tcp I never tried what I wrote but it have to work.

 try
        {
            string Hostname = "127.0.0.1";
            TcpClient Client = new TcpClient(Hostname, 10000);
            byte[] packet = new byte[] { 0x17, 0x03, 0x01, 0x4c };
            Client.Client.Send(packet);
        }
        catch (SocketException e)
        {
        }
Community
  • 1
  • 1
  • What would happen if I converted a large file to bytes? – John May 07 '16 at 22:57
  • I dont know really I have never tried that but I would suggest you to break the byte array if its too big into smaller ones with some identifier to tell which part is which and rebuild the byte array like that. I think if you dont do that it will break into many parts anyway but you wont know how to rebuild it.. – I hate my brain sometimes.. May 07 '16 at 23:03
  • I found this it should work well for you. http://codetechnic.blogspot.cz/2009/02/sending-large-files-over-tcpip.html – I hate my brain sometimes.. May 07 '16 at 23:05
  • @John : If the file you're converting is too big (up to 2 GB for 32-bit applications) you will get an OutOfMemoryException. Also sending the entire file in one `Write()` will take time. Use a `FileStream` instead and read and send only a part of the file at a time (for example 8192 bytes = 8 KB at a time). – Visual Vincent May 08 '16 at 09:25