Sending message to the network. RRS feed

  • Question

  • Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
    IPAddress serverAddr = IPAddress.Parse("");
    IPEndPoint endPoint = new IPEndPoint(serverAddr, 11000);
    string text = "Hello";
    byte[] send_buffer = Encoding.ASCII.GetBytes(text );
    sock.SendTo(send_buffer , endPoint);
    What I am trying to do here is to notify all the computers inside the network that something will happen. For example, one of the computers sends a message like "How are you" and then all the other computers inside the network will be able to receive it. The code above only sends the data to a specific computer but i want all the computers to be able to receive the data aswell.

    • Edited by Dikong42 Tuesday, February 26, 2019 4:50 AM
    Tuesday, February 26, 2019 4:49 AM

All replies

  • You can use the same method as used by the Console command :

    msg * message

    It uses WinStationSendMessageW, which is the same as WTSSendMessage (by enumerating all sessions to send to everyone)

            public const int WTS_CURRENT_SERVER_HANDLE = 0;
            public const int WTS_CURRENT_SESSION = -1;
            [DllImport("WTSApi32.dll", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Auto, EntryPoint = "WTSSendMessageW")]
            public static extern bool WTSSendMessage(IntPtr hServer, int SessionId, string pTitle, int TitleLength, string pMessage, int MessageLength, int Style, int Timeout, out int pResponse, Boolean bWait);
             [DllImport("WTSApi32.dll", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Auto, EntryPoint = "WTSEnumerateSessionsW")]
            public static extern bool WTSEnumerateSessions(IntPtr hServer, int Reserved, int Version, out IntPtr ppSessionInfo, out int pCount);
            [DllImport("WTSApi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
            public static extern void WTSFreeMemory(IntPtr pMemory);
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
            public struct WTS_SESSION_INFO
                public int SessionId;             // session id
                public string pWinStationName;      // name of WinStation this session is connected to
                public WTS_CONNECTSTATE_CLASS State; // connection state (see enum)
            public enum WTS_CONNECTSTATE_CLASS
                WTSActive,              // User logged on to WinStation
                WTSConnected,           // WinStation connected to client
                WTSConnectQuery,        // In the process of connecting to client
                WTSShadow,              // Shadowing another WinStation
                WTSDisconnected,        // WinStation logged on without client
                WTSIdle,                // Waiting for client to connect
                WTSListen,              // WinStation is listening for connection
                WTSReset,               // WinStation is being reset
                WTSDown,                // WinStation is down due to error
                WTSInit,                // WinStation in initialization

    Tuesday, February 26, 2019 9:58 AM
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find a solution quickly if they face a similar issue.

    Best Regards,


    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.

    Thursday, February 28, 2019 3:23 AM