fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14.  
  15. namespace UDPTest
  16. {
  17. public partial class Form1 : Form
  18. {
  19. public Form1()
  20. {
  21. InitializeComponent();
  22. }
  23.  
  24.  
  25. private void button1_Click(object sender, EventArgs e)
  26. {
  27. UdpClient server = new UdpClient(8008);
  28. IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, 0);
  29. List<IPEndPoint> endPoints = new List<IPEndPoint>();
  30. bool add = true;
  31.  
  32. Thread serverThread = new Thread(delegate()
  33. {
  34. Thread serverReceive = new Thread(delegate()
  35. {
  36. while(true)
  37. {
  38. byte[] receivedBytes = server.Receive(ref remoteIPEndPoint);
  39. string data = Encoding.UTF8.GetString(receivedBytes);
  40. MessageBox.Show(data);
  41.  
  42. byte[] returned = Encoding.UTF8.GetBytes("Klient otrzymał potwierdzenie od serwera.");
  43.  
  44. if(endPoints.Count < 3)
  45. {
  46. for (int i = 0; i < endPoints.Count; i++ )
  47. {
  48. if (remoteIPEndPoint.Port == endPoints[i].Port)
  49. add = false;
  50.  
  51. }
  52.  
  53. if(add == true)
  54. endPoints.Add(remoteIPEndPoint);
  55. }
  56.  
  57. for (int i = 0; i < endPoints.Count; i++)
  58. server.Send(returned, returned.Length, endPoints[i]);
  59.  
  60. add = true;
  61.  
  62. }
  63.  
  64. });
  65.  
  66. serverReceive.IsBackground = true;
  67. serverReceive.Start();
  68.  
  69. });
  70.  
  71. serverThread.IsBackground = true;
  72. serverThread.Start();
  73.  
  74. }
  75.  
  76. UdpClient client;
  77. UdpClient client2;
  78. IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, 0);
  79.  
  80. private void button2_Click(object sender, EventArgs e)
  81. {
  82. client = new UdpClient();
  83. IPAddress address = IPAddress.Parse("127.0.0.1");
  84. client.Connect(address, 8008);
  85.  
  86. Thread receiveClient = new Thread(delegate()
  87. {
  88. while(true)
  89. {
  90. byte[] rcvPacket = client.Receive(ref remoteIPEndPoint);
  91. string rcvData = Encoding.UTF8.GetString(rcvPacket);
  92. MessageBox.Show("Klient: " + rcvData);
  93. }
  94.  
  95. });
  96.  
  97. receiveClient.IsBackground = true;
  98. receiveClient.Start();
  99.  
  100. }
  101.  
  102.  
  103. private void button3_Click(object sender, EventArgs e)
  104. {
  105. client2 = new UdpClient();
  106. IPAddress address = IPAddress.Parse("127.0.0.1");
  107. client2.Connect(address, 8008);
  108.  
  109. Thread receiveClient = new Thread(delegate()
  110. {
  111. while(true)
  112. {
  113. byte[] rcvPacket = client2.Receive(ref remoteIPEndPoint);
  114. string rcvData = Encoding.UTF8.GetString(rcvPacket);
  115. MessageBox.Show("Klient2: " + rcvData);
  116. }
  117.  
  118. });
  119.  
  120. receiveClient.IsBackground = true;
  121. receiveClient.Start();
  122. }
  123.  
  124. private void button5_Click(object sender, EventArgs e)
  125. {
  126. sendMessage(client2, "Klient2 wysłał wiadomość.");
  127.  
  128. }
  129.  
  130. private void button4_Click(object sender, EventArgs e)
  131. {
  132. sendMessage(client, "Klient wysłał wiadomość.");
  133. }
  134. void sendMessage(UdpClient udp, string message)
  135. {
  136. byte[] sendBytes = Encoding.UTF8.GetBytes(message);
  137. udp.Send(sendBytes, sendBytes.Length);
  138. }
  139.  
  140. }
  141. }
  142.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(4,14): error CS0234: The type or namespace name `Data' does not exist in the namespace `System'. Are you missing an assembly reference?
prog.cs(12,24): error CS0234: The type or namespace name `Tasks' does not exist in the namespace `System.Threading'. Are you missing an assembly reference?
prog.cs(13,14): error CS0234: The type or namespace name `Windows' does not exist in the namespace `System'. Are you missing an assembly reference?
Compilation failed: 3 error(s), 0 warnings
stdout
Standard output is empty