|
Hello all! Does anyone know of a good way to store information about the peers of a peer-to-peer network thru a web service? I want to be able to store this information locally on my server to tell others how to connect to each other thru the TcpListener / TcpClient objects.
Here's the scenario. Suppose I have a user, let's say Fred. I want to be able to store information about another user, again, let's say Joe.
Now, just passing the Port that Joe is listening on is all good IF, Joe is the only one on his LAN and the outside LAN port matches his port.
Here lies the problem, suppose I have Joe listening on port 6996, after he is NATed, his outside port is 18938(just some random open port)
Ok, wrapping up, there has to be a way to dynamically connect two peers. MSN Messenger, AOL IM for example both allow direct connections. Does anybody understand how they are doing this without opening and dedicating static ports thru NAT? They just seem to work.
Any help would be greatly appreciated. Thanks in advance.
Mike Kushner
mike@kushners.com
|
|
|
|
|
Sure. They don't care or shared IP and port information between clients. The servers that register the connections of clients do all the message passing from client to client. There is no direct connection between two clients.
|
|
|
|
|
When 2 servers are set to direct connect, no bits are passed thru the central server. MSN IM, AOL IM, etc etc etc. For example, AOL direct connect directly connects the 2 computers (Ports 375 - 425). My main question is, if these ports change on the outside of the LAN. Is there a way to detect these port mappings?
I need 2 things for the peers to connect to each other, IP & PORT. When 2 or computers on a LAN access the internet, they usually share a common IP (DHCP, Share WAN IP) or they have a direct internet connection (Single Computer or Static IP), this I can detect in either case. The port number on the other hand is somewhat difficult if there are more than one computer on the LAN. When setting up multiple computers on the LAN, I start to have problems. Single computer or static LAN IPs aren't a problem.
Here's the problem. When I set my TcpListener to start listening on some port, for example 6996, and have more than 1 client on the same LAN, their is no logical way to determine which computer is which. Take the following table for instance. I have 3 computers on 2 different LANs
Mike's Computer
LAN A
Local IP: 172.16.1.1
WAN IP: 65.42.105.105
Listening PORT: 6996
Joe's Computer
LAN B
Local IP: 169.16.1.1
WAN IP: 32.42.33.55
Local Listening PORT: 6996
WAN Listening Port: ???
Nick's Computer
LAN B
Local IP: 169.16.1.2
WAN IP: 32.42.33.55
Local Listening PORT: 6996
WAN Listening Port: ???
Given that example, how could I do the following without having to having Joe's system admin set NAT for IP 32.42.33.55 PORT 6996 to point to Joe's computer. This obviously works somehow with AOL Direct connect.
So basically, how can I log how Mike's Computer in LAN A can connect to Joe's computer in LAN B on his outside IP of 32.42.33.55 and local port 6996
Any thoughts?
Mike Kushner
mike@kushners.com
|
|
|
|
|
mkushner wrote:
Here's the problem. When I set my TcpListener to start listening on some port, for example 6996, and have more than 1 client on the same LAN, their is no logical way to determine which computer is which. Take the following table for instance. I have 3 computers on 2 different LANs
There's your problem. How is the NAT going to know which machine to forward your ports to? Without telling the NAT how to handle the connection, specifically which local machine to send which ports to, your never going to get a connection through. Most NAT's have a DMZ setting which tells it which machine to send all traffic to that doesn't match the port filters. Say you have 2 machines on the local side of your NAT. One is web server on port 80, the other is an every day workstation. The NAT's configuration knows that a request on the WAN side, port 80, should be forwarded to the local web server. A request being received on the same WAN IP address but on port 1000, for example, doesn't have a NAT rule configured for it. So, the NAT checks to see if it has a default machine configured in the DMZ. This machine will be the default local IP address to forward any traffic to that doesn't have a rule defined for it. So, the connection request is forwarded to the local IP configured in the NAT's DMZ. If this DMZ configuration is missing, the connection will be refused by the NAT.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
So, is there a way that if I have the ports just OPEN on the router that I can do somesort of dynamic port pooling?
Check this link out. http://kbserver.netgear.com/kb_web_files/n100495.asp[^]
See how AOL Direct Connect is using a port range of 375 - 425? Is this saying that AOL Direct connect uses these 50 ports for direct connections and computers somehow "Subscribe" to these open ports? Is it impossible to listen on these ports on the client computer inside the LAN without having to NAT?
Mike Kushner
Mike@Kushners.com
|
|
|
|
|
Hellow.There is way to determine when user click by Solution Window (an item)?
My code is Add-in in C#, i got delve Automtiztion model of VS.NET but not find out this thing.
Thank.
|
|
|
|
|
Hi,
i got an access database on a FTP-server.
Can anyone tell me how i can get to that database in a .net database application?
The underdog
|
|
|
|
|
First, be sure that the directory of your mdb is accessible in terms of user and directory permissions.
Then just feed DataSource property of your connection string (for OleDbConnection object) with the path of your mdb relative to the application path.
Give it a try....?
|
|
|
|
|
It was an intutition in the previous message.
I have tried it with IIS and it worked.
Oh no , you ask for only-FTP server?
|
|
|
|
|
Yes, I understand that (that's exactly what I said) and the point I was trying to make is that if you instantiate the NotifyIcon in the startup code for your service (which appears evident) then it'll only display the first time. When the window is destroyed (invariably when you log off) it doesn't get recreated because your service's startup code has already executed. Your evidence supports this assumption.
As I said, you should create a separate application to control the service. This Windows Forms application (hidden main form; just need to display the NotifyIcon ) runs when a Windows session is create to communicate and display status for the service. This is the correct and typical approach.
Otherwise, you'll need to P/Invoke several APIs to receive notification when a user logs in and re-create your NotifyIcon .
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi all,
I want to pass a large file across the network, so I compress them
part by part and stream them out to the other side. Can the zip support
such streaming compression ? i.e. at the sender side, the file is ziped
part by part and send out as zipped streams, and at the receiver side,
when all those zipped chunks are collected, they are unzipped back into
a single file by one unzip operation.
Thanks
|
|
|
|
|
I doubt it. The smaller the sample being zipped at a time, the less compression you can possibly hope to achieve. You're also still asking the system to send at least the same amount of info, so who cares if it got chopped up ?
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
ZIP compression doesn't support streaming. Since the compression algorithm uses an index based on the contents of the ENTIRE stream it's compressing. It sounds like you're already using the best method for doing ZIP-type compression on a stream. I mean, break your stream into fragments, run the compression acrossed a single fragment, send the ZIP'd version of the fragment down the wire, then continue with this procedure on the next fragment. The receiving side will have to un-ZIP each fragment and recombine each piece into the original stream.
But, since a ZIP contains the bit stream substitution table used to compress the fragment, you'll be wasting bandwidth on sending this table for each fragment. This overhead will ultimately reduce the benefit your getting with compression.
Your best bet is to ZIP the entire file all at once, then send it using normal file transfer means. Using the fragmentation method you described, you're actually getting a smaller return for your coding investment.
|
|
|
|
|
|
I've found some examples here on putting icons in subitems of a listview. that works fine, but I don't want an icon/image in the first column, so the listview puts a gap there. Is there any way around this?
If I write code in my sleep, does that make me brilliant, or just a lazy programmer?
My articles
www.stillwaterexpress.com
BlackDice - the programmer formerly known as bdiamond
|
|
|
|
|
You may have better luck asking on the message board for one of the articles you found, the best probably being C# ListView v1.3[^] (also known as Glacial ListView).
Not only have they probably got a similar question before (and hopefully answered it), the authors of the better articles have worked extensively with the Common Controls APIs (since almost all the Windows Forms controls encapsulate the Common Controls in the BCL, and most List View variants start with the List-View common control). You could also try searching this forum as I know I've answered similar questions in the past (long ago). The article message boards are still your best bet, however.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi folks!
Can someone tell me how to access a GridItem of the PropertyGrid programmatically? I don't the value but the label. I want to change the background color of a single field in the PropertyGrid. Is this possible and if yes, how can I do that?
Cheers!
...Dan
|
|
|
|
|
Without reflection, your only way to get a specific GridItem is to use either the SelectedGridItem property or the SelectedGridItemChanged event which are documented on the PropertyGrid . If you read the documentation for the GridItem , however, you'll see no property to change the background color. It's risky anyway. Since GridItem s are created and destroyed constantly, you need to make sure that any references you have to it (like an event handler) are destroyed otherwise you'll consume memory that will never be released (if you have, for example, an event handler for a GridItem and the PropertyGrid would normally release that GridItem it won't be released since you have a reference to it; therefore, it will never be garbage collected).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
I'm trying to get printer info from the OS using the GetProfileStringA function in Kernel32.dll. I used this logic in VB.NET for a while now without any problems, but in C#, the return string buffer is not getting filled. Does anyone have any ideas what might causing this?
<br />
[DllImport("Kernel32.dll")]<br />
static extern int GetProfileStringA( string lpAppName, string lpKeyName, string lpDefault, string lpReturnedString, int nSize) ;<br />
<br />
public bool SetDefaultPrinter(string printerName) <br />
{<br />
bool success = true;<br />
<br />
string sBuffer = new string(' ',255);<br />
string sPrinterSetup = "";<br />
string[] sEle;<br />
string sDriver;<br />
string sPort;<br />
<br />
try<br />
{<br />
<br />
int i;<br />
<br />
i=GetProfileStringA("PrinterPorts", printerName, "", sBuffer, sBuffer.Length);<br />
<br />
if (sBuffer.Trim() != "")<br />
{<br />
sEle = sBuffer.Split(',');<br />
if (sEle.Length > 0)<br />
{<br />
sDriver = sEle[0];<br />
sPort = sEle[1];<br />
sPrinterSetup = printerName.Trim() + "," + sDriver + "," + sPort;<br />
WriteProfileStringA("windows", "Device", sPrinterSetup);<br />
SendMessageA(HWND_BROADCAST, WM_WININICHANGE, 0, "windows");<br />
System.Windows.Forms.Application.DoEvents();<br />
}<br />
}<br />
else<br />
{<br />
errorMessage = "Printer "+printerName+" not detected.";<br />
success = false;<br />
}<br />
<br />
}<br />
catch (Exception ex)<br />
{<br />
errorMessage = ex.Message;<br />
success = false;<br />
}<br />
<br />
return success;<br />
<br />
}<br />
|
|
|
|
|
First, never allocate a string buffer with spaces, use '\0' for null characters. This ensures that - so long as you don't have any buffer overrun - your string is null-terminated whether the API supports it or not. It's far better to use a StringBuilder however, which also helps ensure this and may increase performance since it is mutable, unlike string .
Because you're declaring GetProfileStringA , you must also specify the DllImportAttribute.ExactSpelling . While not necessary (the default CharSet is CharSet.Ansi , it may also be a good idea to specify CharSet=CharSet.Ansi for better readability, IMO.
This is all not only a waste of time, though, but also a security problem. Because you're P/Invoking to unmanaged code your code requires [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)] . There's already a property defined by the BCL (and the BCL assemblies, by default, already have the necessary security permissions to execute) that does this: PrinterSettings.InstallPrinters . This makes the necessary calls and returns you a nice string[] array. Even using WMI is a better alternative to P/Invoking.
Why? Because the BCL assemblies allow partially-trusted assemblies and, since the BCL assemblies have the necessary permissions (by default), actually make the calls that require that permission and your code doesn't need that permission (it would still need the PrintingPermission , however, which is granted, by default, to even the Internet zone in .NET 1.1 (nothing is granted to the Internet zone by default in 1.0).
This also makes for cleaner code. Instead of allocating buffers, parsing strings, and everything that's necessary - and it may work differently, depending on what you're doing, in Windows vs. Windows NT - you enumerating one simple property: PrinterSettings.InstallPrinters .
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Thanks for your reply! Initializing the string with nulls made no difference by itself. However, adding DllImport attributes did help.
[DllImport("Kernel32.dll", CharSet=CharSet.Unicode,ExactSpelling=true)]<br />
As you can see, I decided to use Unicode, and instead of GetProfileStringA, declared GetProfileStringW.
I'm having a little trouble getting a StringBuilder in C#. StringBuilder is not showing up as a member of System.Text. All that does show up is RegularExpressions. Any ideas on that?
I do use PrinterSettings.InstalledPrinters to get the list of all printers installed. I'll look into using it to get the more detailed information for the specific printer I'm interested in. You'd think that PrinterSettings would have a method to set the system default printer without having to resort to API calls.
Thanks, again!
|
|
|
|
|
If you use Unicode, then it won't work on Win9x. Why not just declare it as:
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
[return: MarshalAs(UnmanagedType.U4)]
static extern int GetProfileString(string appName, string keyName, string default,
[Out] string returnedstring, [MarshalAs(UnmanagedType.U4)] int size); That way, it works on both platforms?
If you're typing using System.Text only namespaces show up. The whole point of importing namespaces is so you don't have to qualify all your classes. If you import System.Text the StringBuilder will show up as a usable class (it's defined in mscorlib.dll, which is always referenced by default whether you see it in VS.NET or not).
BTW, I didn't say that initializing the string buffer to NULL characters would fix the problem, only that you should to make sure the string is initialized correctly by being "zero'd out". It's still a better idea to use PrinterSettings.InstalledPrinters . That's why a BCL exists: to common routines trivial (and, in .NET's case, to alliviate the burden of your code requiring extra permissions).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Unfortunately, PrinterSettings.InstalledPrinters only returns a collection of strings (not a string array) that contain the printer names. Other items (driver name and printer port) are required to set the default printer (via WriteProfileString) that InstalledPrinters does not provide.
The MS documentation on GetProfileString seemed to imply that GetProfileStringA/W should be used instead. Also, we don't have any computers running Windows 9x any more. Anyone that is needs to upgrade.
|
|
|
|
|
The Platform SDK documentation wouldn't say that (trust me). You should use GetProfileString , which is a macro definition for GetProfileStringA or GetProfileStringW depending on whether or not UNICODE is defined. Practically all Microsoft APIs that take a string as a parameter are defined this way (unless string parameters are explicitly always ANSI or always Unicode).
If you want to set the default printer and don't have to worry about Windows machines (i.e., 9X and Me) or Windows NT 4, then enumerate PrinterSettings.InstalledPrinters , find the printer, and call SetDefaultPrinter defined in winspool.dll. You can more easily use WMI on supported platforms (the platforms that .NET supports, but, IIRC, 98 and NT 4 will need the WMI redist installed) to abstract the differences away if you want to support older Windows platforms. As I wrote some time back in this forum:
using System;
using System.Management;
using System.Reflection;
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyKeyName("dev")]
class Printers
{
static void Main(string[] args)
{
EnumeratePrinters(args.Length > 0 ? args[0] : null);
}
static void EnumeratePrinters(string defaultName)
{
ManagementObject defaultPrinter = null;
ManagementObjectSearcher searcher = new ManagementObjectSearcher(
"select * from Win32_Printer");
foreach (ManagementObject printer in searcher.Get())
{
string name = printer["Name"].ToString();
Console.WriteLine("Printer: " + name);
if (defaultName != null &&
string.Compare(name, defaultName, true) == 0)
defaultPrinter = printer;
}
if (defaultPrinter != null)
{
Console.WriteLine("Setting default printer to \"{0}\"",
defaultPrinter["Name"]);
defaultPrinter.InvokeMethod("SetDefaultPrinter", null);
}
}
} WMI is a services that allows scripting and other COM clients to query and modify data on local and remote machines.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Wow. You don't know how much time has been spent by folks trying to figure out ways to set the system default printer. I've scoured the internet, Microsoft KB, etc., and the only references found specify using API calls.
Your example works great. You've used some things I'm not familiar with (e.g., ManagementObject), so I'll have to study this a bit. Thanks, Heath.
|
|
|
|
|