Afternoon,
I've just got into using sockets and found myself a little stuck so hopefully someone can point me in the right direction.
I am in the process of building a remote monitoring app for the office environment and want to add in a feature which will either generate a popup of customer text on the target machine or to all users who are currently using the client application.
Now my first ideas were either Net Send or MSG.exe but both of these are off the table so I've now looked into broadcasting over UDP and I'll happily admit I'm a bit of a novice so apologies if I've got anything wrong.
I have been looking at a pretty helpful site to get me started http://codeidol.com/csharp/csharp-ne...-Broadcasting/ and below is the code I have for both sending and receiving.
Sending
ReceivingCode:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("152.144.49.116"), 8080); string hostname = Dns.GetHostName(); byte[] data = Encoding.ASCII.GetBytes(hostname); sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); sock.SendTo(data, iep2); sock.Close(); } } }
The trouble I have is that when I open the code on the target machine I'm testing on within Visual C# Express, the console app just sits there saying "Ready to receive..." and nothing else happens regardless of when I launch the send code. Now I figured that there would be firewall issues with my network so I opted for port 80, is this the best way to achieve this?Code:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint iep = new IPEndPoint(IPAddress.Any, 8080); sock.Bind(iep); EndPoint ep = (EndPoint)iep; Console.WriteLine("Ready to receive..."); byte[] data = new byte[1024]; int recv = sock.ReceiveFrom(data, ref ep); string stringData = Encoding.ASCII.GetString(data, 0, recv); Console.WriteLine("received: {0} from: {1}", stringData, ep.ToString()); data = new byte[1024]; recv = sock.ReceiveFrom(data, ref ep); stringData = Encoding.ASCII.GetString(data, 0, recv); Console.WriteLine("received: {0} from: {1}", stringData, ep.ToString()); sock.Close(); } } }
Now I've followed the example I read in the link pretty much to the letter so I doubt I have many problems if any with the code but I'm a little stuck where abouts to go from here.
Also, this example is in a console application, what would be the best practice for moving it over to a Winform application? When does the application check the port for any incoming data?
Edit: I should also add that I have checked and confirmed the right IP address of the target machine and that they are both on the same Subnet.
I realise I'm asking a lot but any help or nudge in the right direction would be greatly appreciated!
Many thanks,




Reply With Quote