Reputation: 41
I am trying to learn UDP socket programming in C#. I have made a WinForm that will send the data, the code is listed below. It's a small form with three text boxes and a button.
using System;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace UDP_example
{
public partial class Form1 : Form1
{
public Form1()
{
InitializeComponent();
}
private void btnSend_Click(object sender, EventArgs e)
{
// make sure all text boxes contain data, or do nothing.
if (string.IsNullOrEmpty(txtIP.Text)) return;
if (string.IsNullOrEmpty(txtPort.Text)) return;
if (string.IsNullOrEmpty(txtMessage.Text)) return;
// convert the text into byte array
byte[] message = Encoding.ASCII.GetBytes(txtMessage.Text);
string ipAddress = txtIP.Text;
int sendPort = Convert.ToInt32(txtPort.Text);
// send the data
try
{
using (var client = new UdpClient())
{
IPEndPoint server = new IPEndPoint(IPAddress.Parse(ipAddress), sendPort);
client.Connect(server);
client.Send(message, message.Length);
}
}
// display error if one occurs
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
// clean up
txtMessage.Clear();
txtMessage.Focus();
}
}
This part appears to work fine. What I need is to display the received data in a textbox or a messagebox on the other side of the connection.
Can someone show me how to do that? I don't know how to basically get the socket to listen with async and display incoming data on the receiving side. It would be just a form one with textbox or a single messagebox that displays incoming data.
EDIT: This is the receive code, but I want it to receive from any IP address, not just the local. (Found and modified this code snippet.)
using System;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace UDP_Server
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ConnectUDP));
}
private void ConnectUDP(object state)
{
UdpClient subscriber = new UdpClient(4345);
IPAddress addr = IPAddress.Parse("127.0.0.1");
subscriber.JoinMulticastGroup(addr);
IPEndPoint ep = null;
byte[] pdata = subscriber.Receive(ref ep);
string rdata = Encoding.ASCII.GetString(pdata);
txtReceived.Text += rdata;
}
}
}
I've been reading a lot of samples today and watching some videos on YouTube, but I'm fairly stuck in what I thought would be very simple. My only requirement is that it puts the received data from the UDP port into a textbox. Thanks for any help.
Upvotes: 0
Views: 2007
Reputation: 157
As mentioned from @JuanR you would need something like this on your server side
// This constructor arbitrarily assigns the local port number.
UdpClient udpClient = new UdpClient(yourUDPPort);
//IPEndPoint object will allow us to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
// Blocks until a message returns on this socket from a remote host.
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);
Upvotes: 1