I'll try to explain it better. My idea is to make a chat server, where may clients should connect to.
I managed to do a http version, but it requires some IIS configuration, and I don't really want that.
Code:
namespace Server
{
class Server
{
static void Main(string[] args)
{
HttpChannel channel = new HttpChannel(8090);
ChannelServices.RegisterChannel(channel, true);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(ServerService), "server", WellKnownObjectMode.Singleton);
Console.WriteLine("Press enter to stop the server...");
Console.ReadLine();
}
}
}
namespace Server
{
class ServerService : MarshalByRefObject, IServerService
{
Hashtable clients;
ArrayList messages;
public ServerService()
{
clients = new Hashtable();
messages = new ArrayList();
}
public void register(string nick)
{
// get client ip
string clientAddress = HttpContext.Current.Request.UserHostAddress;
clients.Add(nick, clientAddress);
// get client service object using ip
IClientService obj = (IClientService)Activator.GetObject(
typeof(IClientService),
"http://" + clientAddress + ":8091/client");
if (!clients.Contains(nick))
{
// send message to client
obj.receive("Welcome!");
}
else
{
// send message to client
obj.receive("Nickname already taken.");
}
}
}
}
So the main idea is a client being able to register on the server, the server stores his address and is able to get the clients service object any time he wants. When one client sends a message, the server can iterate through the clients hashtable, get their objects and then, make them receive the message. Is this a good a approach?
If you ask me, why shouldn't the client pass the his own IP to the register method, I'll say, if the client is behind a NAT, like most people today are, it would give the internal IP. The safest way is to use an external entity to get the proper IP, in this case, I would use the main chat server.
So, while using an HttpChannel, it is possible to get the client IP inside the server service, it should be too, using a TcpChannel. Accessing the tcp connection data or so... But I'm sort of new to .NET framework, and couldn't find any tcp solutions, only http.
I hope I've explained my problem in a clear way.
Thanks in advance.
Cheers.