Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > .NET Web Services
|
.NET Web Services Discussions about .NET XML Web Service technologies including ASMX files, WSDL and SOAP.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Web Services section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 25th, 2008, 01:11 PM
Registered User
 
Join Date: Mar 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default getting client ip in service using tcp connection

Hi there. I'm building a small chat in which there is a main server that hosts a service, and there are many clients. Each client hosts a service too, so it can get any messages sent by other clients, through a call done by the server.

But I have a little problem. I need to know every client IP. I searched a lot, and I found it to be easier using HttpChannel, where I can just use something like:
Code:
string clientAddress = HttpContext.Current.Request.UserHostAddress;
This is great as I can do it inside the service.

But I'm using a TcpChannel as a connection.
Code:
namespace Server
{
    class Server
    {
        static void Main(string[] args)
        {
            TcpChannel channel = new TcpChannel(8086);
            ChannelServices.RegisterChannel(channel, true);

            RemotingConfiguration.RegisterWellKnownServiceType(
                typeof(ServerService),
                "server",
                WellKnownObjectMode.Singleton);

            System.Console.WriteLine("press <enter> to quit...");
            System.Console.ReadLine();
        }
    }
}
How can I, inside of the ServerService, get the client IP?

Thanks in advance.

My best regards.

 
Old March 26th, 2008, 03:46 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

http://www.alperguc.com/blog/dotnet/...s-iphostentry/

/- Sam Judson : Wrox Technical Editor -/
 
Old March 26th, 2008, 12:18 PM
Registered User
 
Join Date: Mar 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That would give the ip of the local machine, but I want to detect the client's ip on the server service, the one used on the tcp connection.

 
Old March 27th, 2008, 09:10 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Hi

I think I understand a bit better what you are doing - but why do you need the client's IP Address?

If you do need the client's IP address then you might get hold of the TcpChannel.ChannelData and possibly cast it as a ChannelStore. However since you are using remoting and hence running the object on the server as if it was on the client wouldn't it be easier just to pass the ip address as a parameter to one of your methods (I've no idea what your ServerService class does).

/- Sam Judson : Wrox Technical Editor -/
 
Old March 27th, 2008, 05:44 PM
Registered User
 
Join Date: Mar 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

 
Old March 31st, 2008, 05:36 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You might be able to use the CreateObjectRef() method and the ObjRef.ChannelInfo property to get the information.

/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
ZModem over TCP/IP geogomez Pro VB 6 4 December 27th, 2007 03:59 AM
another tcp/ip related query watashi C# 2005 0 August 30th, 2007 07:31 PM
Serial to tcp/ip watashi C# 2005 1 August 28th, 2007 01:48 AM
TCP/IP HELP jpsultana VB.NET 2002/2003 Basics 0 September 29th, 2005 01:37 PM
TCP/IP Data is Being Concatenated owain Pro VB 6 2 December 9th, 2003 08:12 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.