EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Local TcpListener

Also by EldoS: Solid File System
A virtual file system that offers a feature-rich storage for application documents and data with built-in compression and encryption.
#35214
Posted: 12/11/2015 09:19:24
by ingbabic  (Standard support level)
Joined: 09/27/2011
Posts: 114

I want to make small HTTPServer which would listen to calls only from localhost (no network, I want to disable any calls from network). I suppose that I have to set TcpListener, but I don't see field for localhost. Maybe it can be done somehow different?
#35215
Posted: 12/11/2015 09:24:39
by Vsevolod Ievgiienko (EldoS Corp.)

Thank you for contacting us.

If you mean System.Net.Sockets.TcpListener then you can use one of its constructors to adjust what you need: https://msdn.microsoft.com/ru-ru/libra....110).aspx

You can also use our TElSocket class to implement server side sockets. It has TElSocket.ListenAddress property that will do the job.
#35216
Posted: 12/11/2015 10:19:47
by ingbabic  (Standard support level)
Joined: 09/27/2011
Posts: 114

Yes, I meant System.Net.Sockets.TcpListener. This was used in your sample HTTPServer in SBB samples.
So if I set something like this:
Code
IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
m_tcpListener = new TcpListener(ipAddress, m_port);

my listener will only accept calls from localhost?
#35217
Posted: 12/11/2015 14:42:06
by Vsevolod Ievgiienko (EldoS Corp.)

Next code will do the job:
Code
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
TcpListener m_tcpListener = new TcpListener(ipAddress , m_port);
#35218
Posted: 12/12/2015 06:17:53
by ingbabic  (Standard support level)
Joined: 09/27/2011
Posts: 114

Hmm it seems that this does not work that way. TcpListener constructor actually accepts only local IP address. Even one constructor overload (although obsolete) accepts only number and that is a port number. So specifying 127.0.0.1 for IP address does not ensure that you accepting calls from specific address (in my case I want to accept clients only from localhost). Probably I have to check that other way..
#35219
Posted: 12/12/2015 09:25:05
by Eugene Mayevski (EldoS Corp.)

According to MSDN there's a constructor with two parameters there: https://msdn.microsoft.com/en-us/libra...10%29.aspx

Are we looking at the different class?


Sincerely yours
Eugene Mayevski
#35225
Posted: 12/14/2015 09:37:03
by ingbabic  (Standard support level)
Joined: 09/27/2011
Posts: 114

Yup, we're looking same class. Just first parameter (IPAddress) is not parameter from which we want to accept calls. It's rather local IP address. If you do not care which local address is assigned, you can specify IPAddress.Any for the localaddr parameter, and the underlying service provider will assign the most appropriate network address. In Micosoft help they say that, this might help simplify your application if you have multiple network interfaces. So, once again first parametar is local address (e.g. 127.0.0.1), you can't set there any other address than local.
#35226
Posted: 12/14/2015 09:44:24
by Eugene Mayevski (EldoS Corp.)

You are confusing me here. You initially asked how to accept requests only from the local host.

In Windows you have no option to choose from which remote addresses you would accept connections. You need to accept all of them, then deny the unwanted by closing the socket.


Sincerely yours
Eugene Mayevski
#35229
Posted: 12/15/2015 02:45:24
by ingbabic  (Standard support level)
Joined: 09/27/2011
Posts: 114

My understanding is the same. I think that I have no other option but to accept request and then check from which address I got request. If it is different then localhost, then I close socket immediately and go back for listening. In that sense how I construct TCPListener is not important (I can use IPAddress.Any, or 127.0.0.1, it is the same, they're both just local addresses).
Also by EldoS: CallbackRegistry
A component to monitor and control Windows registry access and create virtual registry keys.

Reply

Statistics

Topic viewed 2142 times

Number of guests: 2, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!