The remote client is a desktop application running a windows.forms application. How do I have the server send the client a message, knowing the client's IP address?
views:
227answers:
5A common way to do this is using sockets.
Basically you open up a communication channel to the remote client using TCP/IP and can send messages back and forth. Both sides must know about the other and must agree on a message format.
When using sockets, you communicate on a port. Several ports are reserved for (or at least typically used by) certain protocols. For example, HTTP uses port 80 by default. You will want to select a port not already in common use. Also be sure that any firewalls between both ends allow communication on that port.
Check out these tutorials:
- Simple Threaded TCP Server
- High Performance TCP/IP Server using C#.NET
- Building a TCP/IP server using C#
and these books
It could largely depend on the existing protocol that is being used between the client and the server. If the server is HTTP, then the client could poll to see if there are messages. If the client is already using TCP/IP, then ideally the server can send a message to the client any time it needs to.
Also, keep in mind that opening a communication channel may essentially be one way (i.e. from the client to the server), depending on firewall configuration (such as NAT).
Although sockets is an option, you may want to consider using a higher level abstraction such as the one provided by WCF.
WCF's Duplex Services allows you to implement communication going both ways (client -> server) and (server -> client) with some flexibility on choosing transport and protocol.
In general terms you do that by replying to a request of the client. If you don't have client requests, the distinction between servers and client becomes rather blurred.