Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 554665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:44:02+00:00 2026-05-13T11:44:02+00:00

I have been looking for a solution to this problem for ages. I have

  • 0

I have been looking for a solution to this problem for ages. I have tried many things, including BeginReceive(), but all to no avail. There must be a way to do this, make a UDP-Client receive call that is non blocking AND thread safe. I am trying to receive a message and write it to a Rich Text Box.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace Chat
{
    public partial class Form1 : Form
    {
        public bool contfunctioning = true; public bool changedsplay = false;
        public string enteredchats, msg, chatstring, Chats;

        UdpClient subscriber = new UdpClient(8899);
        UdpClient publisher = new UdpClient("230.0.0.100", 8898);


        public Form1()
        {
            InitializeComponent();
        }


        private void btnConnect_Click(object sender, EventArgs e)
        {

            string ConnectIPAddress;
            ConnectIPAddress = txtboxIP.Text;
            IPAddress addr = IPAddress.Parse(ConnectIPAddress);

            MessageBox.Show("Subscribing to chat server on " + ConnectIPAddress + ".", ConnectIPAddress);

            EndPoint ep = null;

            // This is where The UDPClient subscriber needs to Begin.Receive()


        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            enteredchats = txtboxUsr.Text + " " + txtboxentertxt.Text;
            txtboxentertxt.Clear();

            msg = String.Format(enteredchats);
            sdata = Encoding.ASCII.GetBytes(msg);
            publisher.Send(sdata, sdata.Length);
        }

    }
}

Enough code, yes?
Thanks in advance for all responses.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:44:03+00:00Added an answer on May 13, 2026 at 11:44 am

    I found that using a Timer() allowed me to circumvent this problem.

    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.Net.Sockets;
    using System.IO;
    using System.Threading;
    using System.Diagnostics;
    
    
    namespace Chat
    {
        public partial class Form1 : Form
        {
    
    
            public bool contfunctioning = true; public bool changedsplay = false; public bool setupntwrk = false;
            public string enteredchats, msg, chatstring, Chats, lastchatstring;
    
            UdpClient subscriber = new UdpClient(8899);
            UdpClient publisher = new UdpClient("230.0.0.100", 8898);
    
            System.Windows.Forms.Timer timer1use = new System.Windows.Forms.Timer();
    
    
    
            public Form1()
            {
                InitializeComponent();
            }
    
    
    
            private void btnConnect_Click(object sender, EventArgs e)
            {
                Thread rcvchats = new Thread(ReceiveChats);
                rcvchats.Start();
    
                timer1use.Interval = 1000;
                timer1use.Start();
            }
    
            private void btnSend_Click(object sender, EventArgs e)
            {
                enteredchats = txtboxUsr.Text + ": " + txtboxentertxt.Text;
                txtboxentertxt.Clear();
    
                msg = String.Format(enteredchats);
                byte[] sdata = Encoding.ASCII.GetBytes(msg);
                publisher.Send(sdata, sdata.Length);
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                if (chatstring != lastchatstring)
                dsplay.AppendText("\r\n" + chatstring);
    
                lastchatstring = chatstring;
    
    
            }
    
            public void ReceiveChats()
            {
                while (true)
                {
                    if (setupntwrk == false)
                    {
                        string ConnectIPAddress;
                        ConnectIPAddress = txtboxIP.Text;
                        IPAddress addr = IPAddress.Parse(ConnectIPAddress);
                        MessageBox.Show("Subscribing to chat server on " + ConnectIPAddress + ".", ConnectIPAddress);
                        subscriber.JoinMulticastGroup(addr);
    
                        setupntwrk = true;
                    }
    
                    IPEndPoint ep = null;
                    chatstring = Encoding.ASCII.GetString(subscriber.Receive(ref ep));
    
                    Thread.Sleep(1000);
                }
    
            }
    
            private void btnHost_Click(object sender, EventArgs e)
            {
                Process starthost = new Process();
                starthost.StartInfo.FileName = "C:\\ChatServ.exe";
                starthost.Start();
    
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been looking around but I have not found a solution for this problem:
Figure there is a simple solution to this problem but I have been unable
I have been looking for a solution to this problem for a while now
I have a problem with this code. I have been looking for the solution
I have been looking around for a solution for this problem that works across
I have been looking in google for a solution to my problem but no
I have been looking for a little while for a solution to this problem,
I've been looking everywhere for a solution to this problem but can't seen to
I have been looking for an answer to this on Stack Overflow, but I
I have been looking for a solution and I found many different opinions to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.