Sending file using sockets.

Hi,

i want to send a file from one computer to another computer.am using sockets.in .net 2.0 there is a function in sockets socket.sendfile().now my question is there any function to receive the file at server side.If there is no function how to receive the incoming file How to send and receive a file in .net1.1



Answer this question

Sending file using sockets.

  • parlance

    "But i didnt get what exactly is 2 bytes 1024 bytes."

    Each Messaging protocol has sever parts:

    for example 1st 2 bytes tell the protocol type, second 2 version, third 2 tell what type of packet is this and in last the original message on several bytes: This was just an example.

    So when you want to send your file you need to write your own protocol which will be agreed by both the parties.

    for example you create a simplest Packet Type with 2 byte header and 1024 byte for data:

    Firts to bytes will tell what type of data is in the folling 1024 bytes.

    like first 2 byte will tell the follwing 1024 bytes include file name and length.

    so you'll parse the follwig bytes to the file name and get length.

    Other message's header tell that the follwing bytes are bytes from file then you will receives those bytes and write the the received file.

    I hope you understand it well now.

    "And also i want to know whether there is a function for receiving files like send file() function in .NET2.0....."

    No there is not function ReceiveFile and I have never used SendFile to because its not effective. Its a Synchronous Method which halts the GUI until it finishes sending all the file bytes to the receiver.

    I think it simply reads bytes from the file and sends them to other party. But really I dont feel any charm in using it. Its better to develop your own scheme.

    Best Regards,



  • garyvdailyware

    You have to write your own protpcol to do this. I share some of my Ideas:

    You need to create a protocol for sending and receiving file in Chunks.

    YOu should have your own Packet Type like 2 bytes 1024 bytes:

    First 2 bytes for example tell what type of data is coming. i.e is it start of file, or its a continued data retreival or its is the end of file. the last 1024 bytes will contain the actual information regarding that file.

    Both Sender and receiver must know what header bytes means and on that header what function should be performed.

    Not its about the overall implementation.

    1. Sender will open a file in Read Mode which it wants to send using FileStream class.
    2. Sender then will Send File name to the Receiver along with the toltal size of file so receiver my know when to end receving(Receive Completed).
    3. After Receving the file name receiver will create a file with the same name using the same FileStream object.
    4. Receiver now will Tell Sender star Sending file! I'm ready.
    5. Sender starts sending data i.e Read XXXX bytes from the File and Write on the NetworkStream.
    6. Receiver receives those bytes and write to the File Stream.
    7. On each recept and send receiver and sender both will maintain a counter that how much was the total length of the file and how much has been transfered so far. And when sentBytes >= totalBytes then File Trasnfer is completed and on the receiver side when receivedBytes >= totalBytes then its indicaiton that File Transfer is completed.

    I really hope this will help you alot.

    Best Regards,



  • DLdfrd

    Thanks for sharing ur ideas.

    But i didnt get what exactly is 2 bytes 1024 bytes.

    And also i want to know whether there is a function for receiving files like send file() function in .NET2.0.....


  • Sending file using sockets.