问题
I want to use WCF to enable two way communication without opening a port on the client.
I'm developing something like a P2P application (similar to teamviewer/logmein) where you don't need to open ports to communicate.
How do I accomplish two way communication through HTTP/HTTPS without the need to open a port in the client?
Note : Port 80 can be opened in the server...no issues on that.
Thanks
回答1:
Well those systems you mention work as follows. They first try to make client A and client B communicate directly via a range of different topologies which basically require one of them to allow incoming connections if that fails they fall back on a third party which acts as a man in the middle. So client A talks to the server and sends it messages for client B. Then Client A gets the messages addressed to it back in response. Client B sends it messages to the server and it's gets the message from client A back from the server. This way both client A and B always initiate the connection and don't need to have a port open for incoming traffic.
If I understand correctly in your case you would always want the man in the middle. In order to do this you would have to write a WCF service that provides all relevant methods. For instance things like
- void SendMessageToClient(Guid senderId, Guid recipientId, Message msg)
- Message[] GetMessages(Guid recipientId)
then have those methods respectively store and retrieve those Message objects from somewhere (like a database or a queue or something).
Then write a client that connects to the WCF service using the HTTP binding and call the methods on the server and process the results.
I hope you understand that
- a) this isn't a very efficient way to communicate.
- b) that it's difficult to test and debug and understand whats going on since there are so many parties involved and communication is asynchronous living in 3 different processes.
- c) it adds an extra layer ontop of the communication so you need to keep it clear for yourself in your head (and prefereably in code) when you are dealing with the infrastructure bits and when you are dealing with the actual protocol clientA and clientB speak to each other in the Message objects.
Pseudo (code) Example
in this example I assume the message object is nothing more then a string and the only command is "whattimeisit" to which the response is the local time in string form
- ClientA makes call to server.SendMessageToClient("clientA", "clientB", "whattimeisit");
- Server stores this message in the database with ID 1
- ClientB makes call to server GetMessages("clientB");
- Server retrieves message with ID 1
- ClientB recieves back "whattimeisit" as a response
- ClientB makes call to server.SendMessageToClient("clientB", "clientA", "19:50:12");
- Server stores this message in the database with ID 2
- ClientA makes call to server GetMessages("clientA");
- Server retrieves message with ID 2
- ClientA recieves back "19:50:12" as a response
回答2:
I'm not sure I understand. The purpose of digital firewalls is (generally) control communication channels. If you want to communicate bypassing firewalls you have two choices.
- Hide the message in something the firewall lets through
- Use a communications channel the firewall doesn't control
In the case of the earlier:
You could pass messages to proxy that passed them on (email is a good but not exactly responsive example).
In the case of the latter:
You could put the messages on say file where some other transport layer carries them
来源:https://stackoverflow.com/questions/1317697/wcf-two-way-http-communication-to-bypass-firewalls