从输出到 richtextbox 的特定端口接收 UDP 数据,同时保留文本 c#
Receive UDP data from specific port outputted to richtextbox while keeping text c#
我目前正在进行直接 UDP 连接的聊天。我看到很多关于 TCP 聊天和第三台计算机(服务器)的建议,但我不想要那样。
聊天应该只能在本地工作,正如我测试过的那样,在学校、工作场所或其他大地方都很棒。
到目前为止,当我在文本框中点击 Return 时,我已经让它发送 UDP 数据包,但是我发送它的人并没有接缝到程序中。
我正在使用 Wireshark 来检查它是否真的发送给了他,确实如此。
我已经尝试了几次 google 搜索和测试,但只接缝得到错误,或者没有得到任何程序。
我想要的是一个翻译器,它从特定端口接收 UDP 数据包,并且只显示数据包中的文本数据。我希望它显示在 richTextBox2 上并保留已有的内容。
这是代码和一些屏幕截图:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace chat
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// set this.FormBorderStyle to None here if needed
// if set to none, make sure you have a way to close the form!
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_NCHITTEST)
m.Result = (IntPtr)(HT_CAPTION);
}
private const int WM_NCHITTEST = 0x84;
private const int HT_CLIENT = 0x1;
private const int HT_CAPTION = 0x2;
private void pictureBox1_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
pictureBox1.BackColor = colorDialog1.Color;
this.BackColor = colorDialog1.Color;
}
private void button1_Click(object sender, EventArgs e)
{
}
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
{
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
string IpAddress = textBox3.Text;
IPAddress serverAddr = IPAddress.Parse(IpAddress);
IPEndPoint endPoint = new IPEndPoint(serverAddr, 2522);
string text = textBox4.Text + ": " + richTextBox1.Text;
byte[] send_buffer = Encoding.ASCII.GetBytes(text);
sock.SendTo(send_buffer, endPoint);
richTextBox2.Text = richTextBox2.Text + Environment.NewLine + textBox4.Text + ": " + richTextBox1.Text;
richTextBox1.Text = "";
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
截图:
我最近从 Visual Basic 跳到了 C#,我对网络的东西有点陌生。
如果您愿意提供帮助,非常感谢:)
成功了。
这是目前为止的代码,它有效!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Net.NetworkInformation;
namespace chat
{
public partial class Form1 : Form
{
private const int MyPort = 2522;
private UdpClient Client;
public Form1()
{
InitializeComponent();
// Create the UdpClient and start listening.
Client = new UdpClient(MyPort);
Client.BeginReceive(DataReceived, null);
}
private void DataReceived(IAsyncResult ar)
{
IPEndPoint ip = new IPEndPoint(IPAddress.Any, MyPort);
byte[] data;
try
{
data = Client.EndReceive(ar, ref ip);
if (data.Length == 0)
return; // No more to receive
Client.BeginReceive(DataReceived, null);
}
catch (ObjectDisposedException)
{
return; // Connection closed
}
// Send the data to the UI thread
this.BeginInvoke((Action<IPEndPoint, string>)DataReceivedUI, ip, Encoding.UTF8.GetString(data));
}
private void DataReceivedUI(IPEndPoint endPoint, string data)
{
richTextBox2.AppendText(data + Environment.NewLine);
label4.Text = ("[" + endPoint.ToString() + "]");
//richTextBox2.AppendText("[" + endPoint.ToString() + "] " + data + Environment.NewLine);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_NCHITTEST)
m.Result = (IntPtr)(HT_CAPTION);
}
private const int WM_NCHITTEST = 0x84;
private const int HT_CLIENT = 0x1;
private const int HT_CAPTION = 0x2;
private void pictureBox1_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
pictureBox1.BackColor = colorDialog1.Color;
this.BackColor = colorDialog1.Color;
}
private void button1_Click(object sender, EventArgs e)
{
}
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
{
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
string IpAddress = textBox3.Text;
IPAddress serverAddr = IPAddress.Parse(IpAddress);
IPEndPoint endPoint = new IPEndPoint(serverAddr, 2522);
string text = textBox4.Text + ": " + richTextBox1.Text;
byte[] send_buffer = Encoding.ASCII.GetBytes(text);
sock.SendTo(send_buffer, endPoint);
richTextBox2.Text = richTextBox2.Text + textBox4.Text + ": " + richTextBox1.Text + Environment.NewLine;
richTextBox1.Text = "";
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (var addr in Dns.GetHostEntry(string.Empty).AddressList)
{
if (addr.AddressFamily == AddressFamily.InterNetwork)
label5.Text = "" + addr;
}
}
}
}
如果您正在制作类似的东西,此代码仅适用于本地互联网
我目前正在进行直接 UDP 连接的聊天。我看到很多关于 TCP 聊天和第三台计算机(服务器)的建议,但我不想要那样。 聊天应该只能在本地工作,正如我测试过的那样,在学校、工作场所或其他大地方都很棒。
到目前为止,当我在文本框中点击 Return 时,我已经让它发送 UDP 数据包,但是我发送它的人并没有接缝到程序中。 我正在使用 Wireshark 来检查它是否真的发送给了他,确实如此。 我已经尝试了几次 google 搜索和测试,但只接缝得到错误,或者没有得到任何程序。
我想要的是一个翻译器,它从特定端口接收 UDP 数据包,并且只显示数据包中的文本数据。我希望它显示在 richTextBox2 上并保留已有的内容。
这是代码和一些屏幕截图:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace chat
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// set this.FormBorderStyle to None here if needed
// if set to none, make sure you have a way to close the form!
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_NCHITTEST)
m.Result = (IntPtr)(HT_CAPTION);
}
private const int WM_NCHITTEST = 0x84;
private const int HT_CLIENT = 0x1;
private const int HT_CAPTION = 0x2;
private void pictureBox1_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
pictureBox1.BackColor = colorDialog1.Color;
this.BackColor = colorDialog1.Color;
}
private void button1_Click(object sender, EventArgs e)
{
}
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
{
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
string IpAddress = textBox3.Text;
IPAddress serverAddr = IPAddress.Parse(IpAddress);
IPEndPoint endPoint = new IPEndPoint(serverAddr, 2522);
string text = textBox4.Text + ": " + richTextBox1.Text;
byte[] send_buffer = Encoding.ASCII.GetBytes(text);
sock.SendTo(send_buffer, endPoint);
richTextBox2.Text = richTextBox2.Text + Environment.NewLine + textBox4.Text + ": " + richTextBox1.Text;
richTextBox1.Text = "";
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
截图:
我最近从 Visual Basic 跳到了 C#,我对网络的东西有点陌生。 如果您愿意提供帮助,非常感谢:)
成功了。
这是目前为止的代码,它有效!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Net.NetworkInformation;
namespace chat
{
public partial class Form1 : Form
{
private const int MyPort = 2522;
private UdpClient Client;
public Form1()
{
InitializeComponent();
// Create the UdpClient and start listening.
Client = new UdpClient(MyPort);
Client.BeginReceive(DataReceived, null);
}
private void DataReceived(IAsyncResult ar)
{
IPEndPoint ip = new IPEndPoint(IPAddress.Any, MyPort);
byte[] data;
try
{
data = Client.EndReceive(ar, ref ip);
if (data.Length == 0)
return; // No more to receive
Client.BeginReceive(DataReceived, null);
}
catch (ObjectDisposedException)
{
return; // Connection closed
}
// Send the data to the UI thread
this.BeginInvoke((Action<IPEndPoint, string>)DataReceivedUI, ip, Encoding.UTF8.GetString(data));
}
private void DataReceivedUI(IPEndPoint endPoint, string data)
{
richTextBox2.AppendText(data + Environment.NewLine);
label4.Text = ("[" + endPoint.ToString() + "]");
//richTextBox2.AppendText("[" + endPoint.ToString() + "] " + data + Environment.NewLine);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_NCHITTEST)
m.Result = (IntPtr)(HT_CAPTION);
}
private const int WM_NCHITTEST = 0x84;
private const int HT_CLIENT = 0x1;
private const int HT_CAPTION = 0x2;
private void pictureBox1_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
pictureBox1.BackColor = colorDialog1.Color;
this.BackColor = colorDialog1.Color;
}
private void button1_Click(object sender, EventArgs e)
{
}
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
{
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
string IpAddress = textBox3.Text;
IPAddress serverAddr = IPAddress.Parse(IpAddress);
IPEndPoint endPoint = new IPEndPoint(serverAddr, 2522);
string text = textBox4.Text + ": " + richTextBox1.Text;
byte[] send_buffer = Encoding.ASCII.GetBytes(text);
sock.SendTo(send_buffer, endPoint);
richTextBox2.Text = richTextBox2.Text + textBox4.Text + ": " + richTextBox1.Text + Environment.NewLine;
richTextBox1.Text = "";
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (var addr in Dns.GetHostEntry(string.Empty).AddressList)
{
if (addr.AddressFamily == AddressFamily.InterNetwork)
label5.Text = "" + addr;
}
}
}
}
如果您正在制作类似的东西,此代码仅适用于本地互联网