Guest User

Untitled

a guest
Jan 17th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;
  5.  
  6. public class Broadcst
  7. {
  8. public static void Main()
  9. {
  10. Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  11.  
  12. int portnumber = 9050;
  13.  
  14.  
  15.  
  16. IPEndPoint iep1 = new IPEndPoint(IPAddress.Broadcast, portnumber);
  17.  
  18. IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("192.168.1.255"), portnumber);
  19.  
  20. string hostname = Dns.GetHostName();
  21. byte[] data = Encoding.ASCII.GetBytes(hostname);
  22.  
  23. sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  24. sock.SendTo(data, iep1);
  25. sock.SendTo(data, iep2);
  26.  
  27. // portnumber--;
  28. sock.Close();
  29. }
  30. }
  31.  
  32. using System;
  33. using System.Net;
  34. using System.Net.Sockets;
  35. using System.Text;
  36.  
  37. public class RecvBroadcst
  38. {
  39. public static void Main()
  40. {
  41. Socket sock = new Socket(AddressFamily.InterNetwork,
  42. SocketType.Dgram, ProtocolType.Udp);
  43. // int port_no=Convert.ToInt32(sock.RemoteEndPoint);
  44.  
  45.  
  46. int portnumber = 9050;
  47.  
  48. sock = new Socket(AddressFamily.InterNetwork,
  49. SocketType.Dgram, ProtocolType.Udp);
  50. IPEndPoint iep = new IPEndPoint(IPAddress.Any,portnumber);
  51. sock.Bind(iep);
  52. EndPoint ep = (EndPoint)iep;
  53. Console.WriteLine("Ready to receive...");
  54. // int port_no = Convert.ToInt32(sock.RemoteEndPoint);
  55. while (true)
  56. {
  57.  
  58. byte[] data = new byte[1024];
  59. int recv = sock.ReceiveFrom(data, ref ep);
  60. string stringData = Encoding.ASCII.GetString(data, 0, recv);
  61. Console.WriteLine("received: {0} from: {1}",
  62. stringData, ep.ToString());
  63.  
  64. //data = new byte[1024];
  65. //recv = sock.ReceiveFrom(data, ref ep);
  66. //stringData = Encoding.ASCII.GetString(data, 0, recv);
  67. //Console.WriteLine("received: {0} from: {1}",
  68. // stringData, ep.ToString());
  69.  
  70. data = null;
  71. }
  72. //portnumber--;
  73.  
  74.  
  75. sock.Close();
  76. }
  77. }
Add Comment
Please, Sign In to add comment