tags:

views:

1068

answers:

9

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 transfer the data over the internet. What is the best way to transfer both large and small quantities of data?

My system is connected to the server which has the access to the internet. My system's IP address is dynamic. I don't know how to send the data to another system which is connected to the internet. Do I need to find the router address? (My IP address is generated as 192.168.1.15).

Is using a socket enough, or is HTTP required?

+5  A: 

Socket is enough if no firewalls/proxies are involved.

But, as Internet is involved (not the fastest connection), I suggest for the sake of convenience you should better opt for remoting over http. That way, even if in the future the setup changes, and firewalls/proxies get involved in the equation, you should not worry.

Sunny
OK, that's ok this answer was downvoted. I'd like to know the reason - for self educational purposes only :)
Sunny
Upvoted because I see no reason for the downvote. :) Plus I like the answer.
Bobby Cannon
I think its by mistake
Mohanavel
+1  A: 

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.

lewap
A: 

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.

Vilx-
+3  A: 

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

Mystic
A: 

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).

bezieur
A: 

this could help you

Fredou
A: 

What you want to know depends heavily on many parts of your infrastructure.

If you want to send data to a server that is transparently connected to the internet, it is as easy as connecting to it's IP adress.

If you want to connect to some friend with a broadband connection, things get tricky. You usually have to configure both of your routers (or at least the target one) for NAT.

Familiarize yourself with NAT, and the basics of IP routing. The details you provided are not sufficient to describe exactly what you want to do.

AndreasT
+3  A: 

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();
sipwiz
+2  A: 

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.

James Jones