How can I send data over the internet using a socket?

后端 未结 8 1527
情歌与酒
情歌与酒 2020-12-25 09:11

I would like to send data over internet through a desktop application. I know a little bit about sockets. I have transferred the data within the LAN, but now I want to trans

8条回答
  •  天命终不由人
    2020-12-25 09:52

    More information about your connection needs is required in order to give you an appropriate solution. There are many protocols at your disposal and there are trade-offs for all of them. You will probably choose one of these two transport layers:

    UDP - This is a send-and-forget method of sending packets. Good for streaming media that doesn't necessarily have to be 100% correct.

    The good:

    1. No connection required.
    2. Very lightweight.

    The bad:

    1. No guarantee of your packet reaching the destination (although most of the time they make it).
    2. Packets can arrive out of the order in which you sent them.
    3. No guarantee that their contents are the same as when you sent the packet.

    TCP - This is a connection-based protocol that guarantees predictable behavior.

    The good:

    1. You will know for sure whether the packet has reached the destination or not.
    2. Packets will arrive in the order you sent them.
    3. You are guaranteed that 99.999999999% of the time your packets will arrive with their contents unaltered.
    4. Flow control - if the machine sending packets is sending too quickly, the receiving machine is able to throttle the sender's packet-sending rate.

    The bad:

    1. Requires a connection to be established.
    2. Considerable more overhead than UDP.

    The list of pros and cons is by no means complete but it should be enough information to give you the ability to make an informed decision. If possible, you should take advantage of application layer-based protocols that already exist, such as HTTP if you are transferring ASCII text, FTP if you are transferring files, and so on.

提交回复
热议问题