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.Parse("127.0.0.1"), 8008);
  29.  
  30. Thread serverThread = new Thread(delegate()
  31. {
  32. Thread serverReceive = new Thread(delegate()
  33. {
  34. while(true)
  35. {
  36. byte[] receivedBytes = server.Receive(ref remoteIPEndPoint);
  37. string data = Encoding.UTF8.GetString(receivedBytes);
  38. MessageBox.Show(data);
  39.  
  40. byte[] returned = Encoding.UTF8.GetBytes("Klient otrzymał potwierdzenie od serwera.");
  41. server.Send(returned, returned.Length, remoteIPEndPoint);
  42.  
  43. }
  44.  
  45. });
  46.  
  47. serverReceive.IsBackground = true;
  48. serverReceive.Start();
  49.  
  50. });
  51.  
  52. serverThread.IsBackground = true;
  53. serverThread.Start();
  54.  
  55. }
  56.  
  57. UdpClient client;
  58. UdpClient client2;
  59. IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, 0);
  60.  
  61.  
  62. private void button2_Click(object sender, EventArgs e)
  63. {
  64. client = new UdpClient();
  65. IPAddress address = IPAddress.Parse("127.0.0.1");
  66. client.Connect(address, 8008);
  67.  
  68. Thread receiveClient = new Thread(delegate()
  69. {
  70. while(true)
  71. {
  72. byte[] rcvPacket = client.Receive(ref remoteIPEndPoint);
  73. string rcvData = Encoding.UTF8.GetString(rcvPacket);
  74. MessageBox.Show("Klient: " + rcvData);
  75. }
  76.  
  77. });
  78.  
  79. receiveClient.IsBackground = true;
  80. receiveClient.Start();
  81.  
  82. }
  83.  
  84.  
  85. private void button3_Click(object sender, EventArgs e)
  86. {
  87. client2 = new UdpClient();
  88. IPAddress address = IPAddress.Parse("127.0.0.1");
  89. client2.Connect(address, 8008);
  90.  
  91. Thread receiveClient = new Thread(delegate()
  92. {
  93. while(true)
  94. {
  95. byte[] rcvPacket = client2.Receive(ref remoteIPEndPoint);
  96. string rcvData = Encoding.UTF8.GetString(rcvPacket);
  97. MessageBox.Show("Klient2: " + rcvData);
  98. }
  99.  
  100.  
  101. });
  102.  
  103. receiveClient.IsBackground = true;
  104. receiveClient.Start();
  105. }
  106.  
  107. private void button5_Click(object sender, EventArgs e)
  108. {
  109. sendMessage(client2, "Klient2 wysłał wiadomość.");
  110.  
  111. }
  112.  
  113. private void button4_Click(object sender, EventArgs e)
  114. {
  115. sendMessage(client, "Klient wysłał wiadomość.");
  116. }
  117. void sendMessage(UdpClient udp, string message)
  118. {
  119. byte[] sendBytes = Encoding.UTF8.GetBytes(message);
  120. udp.Send(sendBytes, sendBytes.Length);
  121. }
  122.  
  123. private void Form1_Load(object sender, EventArgs e)
  124. {
  125.  
  126. }
  127.  
  128.  
  129.  
  130. }
  131. }
  132.  
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