Use UdpClient to send and receive message
Client to receive message sent by server:
1: using System;
2: using System.Collections.Generic;
3: using System.ComponentModel;
4: using System.Data;
5: using System.Drawing;
6: using System.Linq;
7: using System.Text;
8: using System.Windows.Forms;
9: using System.Net.Sockets;
10: using System.Threading;
11: using System.Net;
12:
13: namespace NoticeSystemClient
14: {
15: public partial class MainForm : Form
16: {
17: public delegate void ShowMessage(string message);
18: public ShowMessage myDelegate;
19: Int32 port = 11000;
20: UdpClient udpClient = new UdpClient(11000);
21: Thread thread;
22: public MainForm()
23: {
24: //CheckForIllegalCrossThreadCalls = false;
25: InitializeComponent();
26: }
27:
28: private void MainForm_KeyDown(object sender, KeyEventArgs e)
29: {
30: if (e.KeyCode == Keys.Escape)
31: {
32: thread.Abort();
33: udpClient.Close();
34: Close();
35: }
36: }
37:
38: private void ReceiveMessage()
39: {
40: while (true)
41: {
42: IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, port);
43: byte[] content = udpClient.Receive(ref remoteIPEndPoint);
44:
45: if (content.Length > 0)
46: {
47: string message = Encoding.ASCII.GetString(content);
48:
49: this.Invoke(myDelegate, new object[] { message });
50: }
51: }
52: }
53:
54:
55: private void ShowMessageMethod(string message)
56: {
57: richText.Text = message;
58: }
59:
60: private void MainForm_Load(object sender, EventArgs e)
61: {
62: myDelegate = new ShowMessage(ShowMessageMethod);
63: thread = new Thread(new ThreadStart(ReceiveMessage));
64: thread.IsBackground = true;
65: thread.Start();
66: }
67: }
68: }
Server:
1: using System;
2: using System.Collections.Generic;
3: using System.ComponentModel;
4: using System.Data;
5: using System.Drawing;
6: using System.Linq;
7: using System.Text;
8: using System.Windows.Forms;
9: using System.Net.Sockets;
10: using System.Net;
11:
12: namespace NoticeSystem
13: {
14: public partial class MainForm : Form
15: {
16: UdpClient udpClient = new UdpClient();
17: public MainForm()
18: {
19: InitializeComponent();
20: }
21:
22: private void btnClose_Click(object sender, EventArgs e)
23: {
24: udpClient.Close();
25: Close();
26: }
27:
28: private void btnSend_Click(object sender, EventArgs e)
29: {
30: Int32 port = 11000;
31: IPAddress ip = IPAddress.Parse(txtStartIP.Text.Trim());
32: IPEndPoint ipEndPoint = new IPEndPoint(ip,port);
33: byte[] content = Encoding.ASCII.GetBytes(richText.Text);
34: try
35: {
36: int count = udpClient.Send(content, content.Length, ipEndPoint);
37: if (count > 0)
38: {
39: MessageBox.Show("Message has been sent.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
40: }
41: }
42: catch
43: {
44: MessageBox.Show("Error occurs.", "Exclamation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
45: }
46: }
47: }
48: }
There’s no more to say but the Client source code, red words.
We must indicate the port of the UdpClient, otherwise, it will pop errors.
code:
UdpClient udpClient = new UdpClient(11000);
At the outset, i don’t indicate the port, so when i debug the code, there’s nothing happen on client side.
Source code disassembled by Red Gate’s .Net Reflector of the UdpClient:
1: public UdpClient(int port) : this(port, AddressFamily.InterNetwork)
2: {
3: }
1: public UdpClient(int port, AddressFamily family)
2: {
3: IPEndPoint point;
4: this.m_Buffer = new byte[0x10000];
5: this.m_Family = AddressFamily.InterNetwork;
6: if (!ValidationHelper.ValidateTcpPort(port))
7: {
8: throw new ArgumentOutOfRangeException("port");
9: }
10: if ((family != AddressFamily.InterNetwork) && (family != AddressFamily.InterNetworkV6))
11: {
12: throw new ArgumentException(SR.GetString("net_protocol_invalid_family"), "family");
13: }
14: this.m_Family = family;
15: if (this.m_Family == AddressFamily.InterNetwork)
16: {
17: point = new IPEndPoint(IPAddress.Any, port);
18: }
19: else
20: {
21: point = new IPEndPoint(IPAddress.IPv6Any, port);
22: }
23: this.createClientSocket();
24: this.Client.Bind(point);
25: }
You will find that the UdpClient need a port to bind to to listen the IPENDPOINT.