views:

87

answers:

3

Hi,

I want to create app client-server in C# but not only in LAN. There it's easy: TcpListener, TcpClient etc.

I want to make sth like in this e.g. On my comp is server that's waiting for a connection. Someone in another network has client. He begin connection, but...where...what is a IP of server? He see only ip of router.

Any ideas? :)

+1  A: 

You still use the same TcpListener and TcpClient (if you want to make it that low level).

The technologies for communicating over a WAN are the same for communicating over a LAN. The difficult part is getting the networks in between the client and server to allow you to use the necessary ports and protocols.

Justin Niessner
Yes, it's true, but how do this? :)
Saint_pl
@Saint_pl - You'll either have to talk to your network administrator or, if you're on a home network, learn how to configure your network hardware to not block traffic and forward the traffic for the port you want to use.
Justin Niessner
No, I know how to forward ports on my router but problem is not here. Look to my talking with 'Mark H' two Answers below to see what I mean.
Saint_pl
+1  A: 

You question doesn't explain the whole scenario.. but with my understanding of the problem I can suggest the following answer: Depending on the Target customer base's locations (support for Corporate networks or NOT), you can use various routing options like UPnP, STUNT or IPv6, or some other NAT traversal options, so that you can inform the client about where the listener is. There should always be a central registry server to which the Listener would inform its whereabouts and the mode of the communications permitted in its environments. Use of an XMPP server would be an easy option for such purposes, which solves most of such issues. Once the client queries about the location of the listener from the discovery server, it can directly connect to the Listener.

Bhuvan
Thank you very much for this answer. I didn't hear earlier about it. NAT traversal is good idea, but I've problem now, because I'm behind two routers - "double NAT". I try to use this UPnP library but it doesn't work yet :) I'm still trying.But it makes me think: why do you say that should be a central registry server...if server doesn't move ever then of course it's not necessary, right? Client could send data straight to the server?
Saint_pl
If there is a option of a external DMZ'd registry server, and you are planning anyway to use TCP (which is duplex), try tunneling through the server, if UPnP fails. This method might be little bandwidth intensive on the server side, but it is a guaranteed option.
Bhuvan
Yes, I'm thinking about tunelling, but it's a little impossible to have external IP on the server. Double NAT isn't dependent on me. Is it possible tunelling to my application-server over double NAT?
Saint_pl
for Tunneling through server you do not need external IP address. The server should be in the DMZ. The client will connect to the server allowing callback. Try using NetTCPBinding for this and set the mode to duplex. All the calls will be made to the server.
Bhuvan
Hmm, maybe I don't understand sth...but how can I do, that server will be in DMZ on my router? And with what IP address? Because I think it'll be external IP of my NAT, but still internal of the second NAT, right? And in this case client don't know still where it should make connection?
Saint_pl
A: 

Sounds like you just want to set up port forwarding on your router. When an incoming connection is made on the specific port a client connects to, the router should redirect the connection to the machine you specify on the LAN. (Usually an IP like 192.168.x.x). This should also be the IP address your TcpListener is listening for connections on.

You can try portforward.com if you need help setting it up for a particular router.

Mark H
Yes, port forwarding it's solution, but I'm not sure in this situation. I explain maybe more.On my comp is server that waiting for connection, but from time to time he want to send some informations to client. Client is out of my control e.g. in other city. How can I send him this packet without making changes on his router?
Saint_pl
You cannot send anything to the client until it connects to you first. After the connection is made and you've accepted it (using `tcpListener.AcceptTcpClient` or `.AcceptSocket`) you use the accepted socket to send data along.
Mark H
Hmm, but what with his router?I'll have his IP(btw external or internal?) and port but e.g it'll be port number 7001. And his router doesn't accept connection on this port - right?(because for what? default this port it's probably closed)What should I do with this?
Saint_pl
There's **nothing** you can do to get past his router from your position - you can only ask the client to forward his port if necessary. Otherwise, the technique is to make his client connect to you first (there shouldn't be any router issues for him making outgoing connections), then you send the information once he is connected. You could perhaps make this happen on a schedule so that it connects to you regularly to check for updates.
Mark H
OK, there's second problem :) double-NAT without access to second router. What do you suggest now? :)
Saint_pl