none
replace sockets with a different communication method c# RRS feed

  • Question

  • Greetings,

    I have developed a custom windows service in C# and a desktop winforms application in c#.

    The desktop communication application sends commands to the windows service using sockets.  Unfortunately, some users don't have networking or their networking is very unreliable.  Since both processes run on the same PC (windows 10), I am going to replace the Socket based code which requires networking, with another approach which does not require networking.

    Would appreciate recommendations on unnamed pipes, named pipes, messages or shared memory for the approach to implement.

    Thanks in advance.

    Thursday, December 26, 2019 9:38 PM

All replies

  • You should be looking into Named Pipes communications between two processes running on the same machine.

    https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-use-named-pipes-for-network-interprocess-communication

    https://docs.microsoft.com/en-us/dotnet/framework/wcf/samples/netnamedpipebinding

    Thursday, December 26, 2019 11:13 PM
  • Hi ksukat63,
    As DA924x said, you can use Named Pipes to replace it.
    Besides, you can also use shared memory, Anonymous Pipes, message that you pointed.
    More details you can refer to these documents.
    [Anonymous Pipes]
    [Shared memory]
    [WM_COPYDATA]
    [Use Named Pipes and Shared Memory for inter process communication with a child process or two]
    [how to pass value between two process (.net c# exe)]

    Hope these are useful for you.
    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, December 27, 2019 9:45 AM
  • But don't named pipes require networking to function ?
    Friday, December 27, 2019 2:28 PM
  • But don't named pipes require networking to function ?

    You'll have to verify, but I doubt it.
    • Edited by DA924x Friday, December 27, 2019 3:09 PM
    Friday, December 27, 2019 3:08 PM
  • > Unfortunately, some users don't have networking or their networking is very unreliable.

    You are being fed a load of nonsense by someone.  ALL Windows machines have networking, even if they don't have a network card installed.  It is absolutely fundamental to the operation of the system.   Further, if both processes are on the same PC, then network reliability is not an issue.  It's all handled internally.  The packets never leave the machine.

    Now, it's quite possible that you have made a mistake in your service, so that you are offering your service to the whole network instead of just to the localhost (127.0.0.1).  If you only want to offer services to local processes, then sockets are always available and are 100% reliable.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Friday, December 27, 2019 8:34 PM
  • Yes.  Named pipes are implemented using TCP port 445 or UDP port 135, depending on the pipe type.  As I said below, localhost networking is implemented on ALL Windows machines, and have been since the 20th Century.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Friday, December 27, 2019 8:48 PM
  • My apps use a config file where the user must enter the ip address of the pc.   If they enter 127.0.0.1 then packets stay internal to the pc?  If they enter 192.168.1.1 (or any other routeable ip address), then it leaves and comes back to the pc  when using sockets ?

    Thanks.

    Monday, December 30, 2019 3:15 PM
  • If you enter the routable address assigned to an adapter in the PC, the packets will not leave the machine, but they will be handled by the driver for that network adapter.  The net effect is the same.

    The point is this.  If your service always needs to send packets to another process on the same machine, don't even ASK for an IP address.  Use 127.0.0.1.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Monday, December 30, 2019 8:07 PM
  • For now 127.0.0.1 but in near future the desktop and service can run on different networked pc's.

    thanks for the clarifications.  Helps me.

    Monday, December 30, 2019 9:36 PM