I have a question. I would like to create a socket listener, but the listen address is from a remote server.
So say SERVER A has the socket listener. SERVER B (ex. IP = 123.456.78.23:1970) has the listen port.
If I create the socket like this (and execute it on server A):
IP EndPoint endpointIp = new IPEndPoint(123.456.78.23, 1970);
this._socket = new Socket(endpointIp.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
this._socket.Bind(endpointIp); // crashes here
this._socket.Listen(MaxPending开发者_运维技巧Connections);
The program crashes on the Bind code: "The requested address is not valid in its context".
How can I listen on a remote port?
Thank you!
You can't listen to a port on one computer from another. (Hence the message "not valid in this context")
What you CAN do is write one program/service that runs on SERVER B and listens to the port, and use Remoting or WCF to write ANOTHER program that runs on SERVER A that monitors your service on SERVER B rather than trying to listen on a port on SERVER B.
You can't open a socket using an IPEndPoint
(basically IP and port) of a different server. What you are asking about is server spoofing - pretending to be a different server and receiving the traffic addressed to it. You will need to configure server A to have the IP of server B, and make sure every router on the way to it can reach server A with this IP.
Edit: Following your comment on the original question, what you need is a load balancer. In order to allow for server B to take over when server A is dead, you must have a router or a server that receives all traffic and directs it to server A, then if it senses that server A is down will redirect all traffic to server B. Of course since you already are doing load balancing, might as well make it a real load balancer, forwarding the traffic to both servers while both are up, and to the remaining one when the other one fails.
精彩评论