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

后端 未结 8 1482
情歌与酒
情歌与酒 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

    You can do it with .Net's Socket class or you can work with the more convenient TcpClient class.

    Firstly though you need to figure out what server you intend to communicate with. Is it an HTTP server or an FTP server? Both HTTP and FTP are application-level protocols which are implemented on top of (using) sockets, which is really a transport layer interface.

    Your local IP address or the address of the router really doesn't matter. You however need to know the IP address of the remote host you intend to connect to. You can obtain this by calling:

    IPHostEntry host;
    
    host = Dns.GetHostEntry(hostname);
    

    You might also want to think about other issues when working with sockets, such as using timeouts to mask failure, the possibility of resuming upload/downloads when transferring large files, etc. If you spend sometime looking on the net, you should be able to find higher level HTTP/FTP apis that will let you work with file transfers much more easily.

    Judging by your question, you seem pretty new to sockets, so reading this might also help

提交回复
热议问题