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

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

    Visual Studio has a lot of well made facilities for creating and consuming SOAP XML Web Services. I'd look into it if I were you. Sure, there is some overhead, but coding against it is extremely easy.

    Of course, I'm not sure how well that would scale if you had to transfer, say, tens or hundreads of megabytes of data across slow internet connections. It does offer asynchronous I/O, but I don't think you can get a progress indicator, and there most definately isn't a resume functionality.

    Added: You can also continue using your socket. There is no extra work invloved for connecting to a server across the internet. Just specify the server's IP address, and away you go. Your OS will take care of all the gory details like routers, missing packets, etc.

    0 讨论(0)
  • 2020-12-25 09:50

    First you should make a decision what protocol you want to use TCP or UDP. Then you have two options: 1. use Socket (lower level) or 2. Use class like TCPClient or UDPClient (which represents a little higher abstraction. I'd suggest (for the begging the second option).

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-25 09:55

    If all you want to do is transfer raw data from one machine to another it's very easy to do using a TCP socket.

    Here's a quick example.

    Server:

     ThreadPool.QueueUserWorkItem(StartTCPServer);
    
     private static void StartTCPServer(object state) {
            TcpListener tcpServer = new TcpListener(IPAddress.Parse("192.168.1.15"), 5442);
            tcpServer.Start();
            TcpClient client = tcpServer.AcceptTcpClient();
    
            Console.WriteLine("Client connection accepted from " + client.Client.RemoteEndPoint + ".");
    
            StreamWriter sw = new StreamWriter("destination.txt");
    
            byte[] buffer = new byte[1500];
            int bytesRead = 1;
    
            while (bytesRead > 0) {
                bytesRead = client.GetStream().Read(buffer, 0, 1500);
    
                if (bytesRead == 0) {
                    break;
                }
    
                sw.BaseStream.Write(buffer, 0, bytesRead);
                Console.WriteLine(bytesRead + " written.");
            }
    
            sw.Close();
        }
    

    Client:

     StreamReader sr = new StreamReader("source.txt");
    
     TcpClient tcpClient = new TcpClient();
     tcpClient.Connect(new IPEndPoint(IPAddress.Parse("192.168.1.15"), 5442));
    
     byte[] buffer = new byte[1500];
     long bytesSent = 0;
    
     while (bytesSent < sr.BaseStream.Length) {
            int bytesRead = sr.BaseStream.Read(buffer, 0, 1500);
            tcpClient.GetStream().Write(buffer, 0, bytesRead);
            Console.WriteLine(bytesRead + " bytes sent.");
    
            bytesSent += bytesRead;
      }
    
      tcpClient.Close();
    
      Console.WriteLine("finished");
      Console.ReadLine();
    
    0 讨论(0)
  • 2020-12-25 10:07

    In your question you mix different things. Sockets are an abstraction for network communication. You will certainly need a socket to communicate over the network. However, possibly you will not see that a socket is used (like in a web-browser). Http is a communication protocol. This is what goes through a communication channel.

    0 讨论(0)
提交回复
热议问题