.NET 3.5 (C#) Named pipes over network - c#

I'm struggling to get a .NET (NOT a WCF) named pipe to communicate across the network. The MSDN documentation implies this is possible, nay trivial. But my code:
using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(servername, "myPipe", PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.Impersonation))
... works great when servername="localhost" and the pipe server is on the same box. But if it's another machine - regardless whether I make servername an IP address, DNS name, NetBIOS name, UNC path etc - means the pipe never connects. It doesn't actually fail, pipeClient.Connect() just sits there.
There's so many different and conflicting posts on various forums, blogs and sites about this. Can anyone set the record straight and tell me: how do I get a Named Pipe Client to connect from one C# application on one machine to a Named Pipe Server running on an another machine?

Are you doing this at work? Is a corporate firewall or anti-virus program running? Check the windows event logs and any applications in your task notification icons area on your start bar for hints as to who is blocking this.

Related

Programmatically blocking all connections between a process and particular host in C#/Windows

I'm trying to track down some failover problems in a third-party connector library that we use. At the moment I am starting and stopping remote services to simulate the failures, this works but is very impractical.
Is there a way that I can programmatically block all connections between a process and a given host?
EDIT: as well as blocking I'd like to drop any existing connections
I think that you can interact with the windows Firewall to achieve what you want ( programmatically block all connections between a process and a given host), take a look at the Windows Firewall Interfaces.
You can also block all connection to the host by writing an entry in the hosts file. You can just redirect the host name to localhost or whatever unreachable address. But it doesn't apply to a single process but to the whole machine. It's more simple than working with firewall, but more global.

Establish a P2P Connection in C#

I realize this question is similar to some others, but I figured my situation is sufficiently different to warrant its own question (hopefully).
What I'm planning on is deploying a program on another person's computer which will open a connection to my computer after which, I'm assuming, the computers should be able to communicate with each other. Once the program starts, it should be able to pull the address information and port (if they aren't blocked) to create a connection, right?
What's more is the internet configuration of the dorm I'm living in. Every room is assigned a unique port and a static IP address assigned by a DHCP server. How do I factor this into the design of my program?
I'm thinking that this setup does not require a server as an intermediate access point, as my address details will always remain the same and the host computer can simply connect to my computer without further information. Is this correct?
Finally, I am reading a few pages about creating a connection, but am confused with all the possibilities of TcpConnection, WCF, CORBA, etc. Which one would actually be the simplest one for me to start with assuming I only want to send messages to the other machine?
Update:
The address is static in the sense that it doesn't change. My IP address is of the form 130.83.20.xxx and I can either wait for the DHCP server to assign me this address, or I can manually enter it myself using a static IP configuration.
As for the messages itself, simple text messages will suffice for the start. The ports mentioned before are the switch ports and do not come into play during network programming I believe.
I would go with TcpClient and TcpListener. Check out the example code on MSDN, copy and paste it into two C# console projects and build them.
I would use 127.0.0.1 (localhost) for testing purposes on port 5001 (a commonly used test port).
TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 5001);
Then you should be able to test a simple client/server that runs on your computer only. Once you get that working, you can take the client to another computer in your dorm and make sure it still works. Once that works, you can go to a local coffee shop and take the client with you. Leave the server running at a known IP address in your dorm. In this case, have the server bind to your actual external IP (not localhost). You can do this by just specifying a port to the TcpListener constructor: `
TcpListener server = new TcpListener(5001);
Once you get all that working by yourself or with a friend, then send it external. Better to get these things working in a demo before sending it to your customer and having him troubleshoot with you. :)
Reasoning behind my answer:
Simple TCP client/server is very straightforward and will allow a simple chat program between two computers. Then you can beef it up to send any data stream you want. You can add code like this to get access to a StreamWriter:
NetworkStream stream = client.GetStream( );
StreamWriter writer = new StreamWriter(stream);

Having problems with .NET named pipes and ambiguous exceptions

I've written a client that connects to a server via a named pipe, and it works fine with my server. However, when I try to connect to a remote server, it fails when I call Connect(), and throws "Request not supported exception", and I have no idea what that means (tried looking on msdn, didn't help).
This is the relevant piece of code. I am sure the server and the path exist, because another client (whose source I can't see, but I know it uses nxpipe) can connect to it.
NamedPipeClientStream stream = new NamedPipeClientStream(serverName, pipeName, PipeDirection.InOut);
stream.Connect(timeout);
Does anyone have any ideas what that means?
Thanks.
EDIT (SOLVED) :
You will NOT BELIEVE what the problem was. First, the guy that ordered the app forgot to ran the server app and open the pipe, so we spent hours trying to figure out what's going on, assuming the pipe is opened on the remote machine. After he remembered that he forgot to run the server app (a few days later), we still had problems. At that point I already wrote a client using .NET pipes and the native pipes using CreateFile. Turns out the guy also forgot to tell us the whole name of the pipe (weird that we got "request not supported" for invalid pipe name, though). Luckily we had an app they used earlier, which had part of that pipe name hardcoded (and part of it you still had to specify) so we used process explorer to figure out the full name of the pipe and finally connected. Now it works :|
I think this must be a Win32 IO exception (ERROR_NOT_SUPPORTED - error code 50). If so it will be coming from the RPC/SMB protocol by which named pipe communications are remoted from one machine to another. It means that one side is trying to invoke a protocol operation which is not supported by the other side.
In your context I imagine this means that the security context from which you are trying to initiate the named pipe communication is not compatible with what is supported by the other side (or conceivably even some firewall in between which has rules at the protocol level).
If both sides were Windows machines I would start by checking using NET USE whether the security context on the initiating side can establish a connection to the IPC$ share on the other side. I afraid I have no knowledge of the Libra mainframe or what difference this might make.
Named pipes only exist within the current machine. You need to use something like TCP to cross the machine boundary.
EDIT:
Correction, according to this, it is possible across a network. I must have been mistaken, and perhaps the default behavior is that access to NT AUTHORITY\NETWORK is denied.

Identify user and machine on the local network

In my company we use small application called IPMsg, a messenger kind of tool to pass messages and file to other fellows in company, even it allows to multicast the message.
And also it lists the user name, host name and IP addresses of users.
How can it do that? There is no server present for message routing and when checked through netstat command in CMD it does not show any details like what protocol and port it is using to communicate.
There is source code also available on the same site which is in VC++. I didn't understand a line of code... (I'm a C# guy)
Can anyone explain me how it can do that?
One simple way would be to let the application listen on a certain network port, and when you start your instance of it, it tries to connect to that port on each computer on the same network. If that other computer has that port open, and answers correctly, then you have found another instance of the application.
IPMsg probably multicasts a request for all clients to report their user and host details.
A similar mechanism is used when Windows Explorer attempts to find other machines on a network. A good description of this type of multicasting discovery is described here.
IPMsg is a daemon which listens to incoming connections on a specific port which is the connection port. You can find out which port it used by using Wireshark.
Start wireshark, start listening on the interface where you have connected to LAN and then start sending any message, wireshark will show you the message on the screen with the port number also.
The application is a peer-to-peer software and doesn't require a central server software to route messages. it only has a small daemon which accepts incoming connections. This is the way Jabber Instant messaging protocol also works.
As you said it lists username, hostname and ip address of users, do you mean it pings the network and finds it? If yes, then it is actually possible to find the IP addresses of computers on the Local Network which requires you to know the subnet on which you are connected.
You can use ARP/ICMP Ping to know the hosts present on your network provided you enter the correct subnet information
Multicasting a message is also nothing special. It is a feature provided with all Networking Stacks.
If you want mutlicasting in .NET, it is allowed. Check this page on Code Project which gives a nice example

Blocking Connections By IP

I need to able to block any and all connections to my pc from a specific IP address , i know this is possible with a firewall but i need to do this in c#. Any idea how (need code).
Update :
Its a generic C# app not asp.net , target platform is WinXp till Win7
Need more information... if you're talking socket communication, you can simply close the connection to a client as soon as it connects if the IP address is blocked, or process the Connection Request and evaluate there.
Edit: Simplest way for you would probably just be to interact with Windows Firewall API... here's how:
http://www.shafqatahmed.com/2008/01/controlling-win.html
Your question is unclear but I'll try to answer the best I can, within my understanding.
Do you want to control machines from connecting to any port on your machine? if so, you need to control the built-in windows firewall or find yourself a filter driver you can control. In order to write your own filter driver, you must leave the land of managed code, so I am guessing that's not an option.
To learn how to control the firewall, here's a link:
http://www.shafqatahmed.com/2008/01/controlling-win.html
more on google.
Do you want to control remote machines from connection to a port on your machines that your application owns? You cannot do that either (see #1 above). However you can take action after the connection, and close the connection if you don't like the remote IP (check the remote endpoint's IP).
two caveats with this approach:
It doesn't save you from a DoS attack.
You will need to be careful if you need ipv6 support (you can't just check the IPV4 address in that case)
HTH
A "firewall" in c#?
First you would have to access the network interface on a low level, eg.: http://msdn.microsoft.com/en-us/library/ms817945.aspx
Then you have to parse all incoming packets and maybe discard them.
It's not an easy task and I don't recommend you to write a driver and a firewall in C#, because the .NET Framework will be loaded every time you start your machine.
Also traffic parsing can be tricky... I implemented a router/traffic analyzer in C# some time ago and it took me about one year to gain the experience with network programming to gain the knowledge to do this.

Categories

Resources