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 7759409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:37:16+00:00 2026-06-01T13:37:16+00:00

When i try to send a message from my client , the server is

  • 0

When i try to send a message from my client , the server is not able to receive that message and print it. Can anyone tell me the error in the following server client application.

I have created two WinForm projects, one is UDP server and the other is UDP client.

In UDP server project, I created a form which contains a RichTextBox named richTextBox1 to show message and a Button named btStart to start/stop the listening. This is the code snippet:

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;

namespace UDPServer
{
public partial class Form1 : Form
{
    delegate void ShowMessageMethod(string msg);

    UdpClient _server = null;
    IPEndPoint _client = null;
    Thread _listenThread = null;
    private bool _isServerStarted = false;

    public Form1()
    {
        InitializeComponent();
    }
    private void serverMsgBox_Load(object sender, EventArgs e)
    {
        this.btStart.Text = "StartServer";
    } 

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void btStart_Click(object sender, EventArgs e)
    {
        if (_isServerStarted)
        {
            Stop();
            btStart.Text = "StartServer";
        }
        else
        {
            Start();
            btStart.Text = "StopServer";
        }

    }
    private void Start()
    {
        //Create the server.
        IPEndPoint serverEnd = new IPEndPoint(IPAddress.Any, 1234);
        _server = new UdpClient(serverEnd);
        ShowMsg("Waiting for a client...");
        //Create the client end.
        _client = new IPEndPoint(IPAddress.Any, 0);

        //Start listening.
        Thread listenThread = new Thread(new ThreadStart(Listening));
        listenThread.Start();
        //Change state to indicate the server starts.
        _isServerStarted = true;
    }

    private void Stop()
    {
        try
        {
            //Stop listening.
            listenThread.Join();
            ShowMsg("Server stops.");
            _server.Close();
            //Changet state to indicate the server stops.
            _isServerStarted = false;
        }
        catch (Exception excp)
        { }
    }

    private void Listening()
    {
        byte[] data;
        //Listening loop.
        while (true)
        {
            //receieve a message form a client.
            data = _server.Receive(ref _client);
            string receivedMsg = Encoding.ASCII.GetString(data, 0, data.Length);
            //Show the message.
            this.Invoke(new ShowMessageMethod(ShowMsg), new object[] { "Client:" + receivedMsg });
            //Send a response message.
            data = Encoding.ASCII.GetBytes("Server:" + receivedMsg);
            _server.Send(data, data.Length, _client);
            //Sleep for UI to work.
            Thread.Sleep(500);
        }
    }
    private void ShowMsg(string msg)
    {
        this.richTextBox1.Text += msg + "\r\n";
    }
}
}

In UDP client project, I also created a form which contains a RichTextBox named richTextBox1 to input or show message and a Button named btSend to send the input message. You can run several instances of this project. The server would cope with all the running clients. This is the code snippet:

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.Sockets;
using System.Net;
using System.Threading;

namespace UDPClient
{
public partial class Form1 : Form
{
    UdpClient _server = null;
    IPEndPoint _client = null;

    public Form1()
    {
        InitializeComponent();
    }



    private void Form1_Load(object sender, EventArgs e)
    {

    }
   private void serverMsgBox_Load(object sender, EventArgs e)
    {
        //Get the server.
        _server = new UdpClient("127.0.0.1", 16000);
        //Create a client.
        _client = new IPEndPoint(IPAddress.Any, 0);
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        try
        {
            _server.Close();
        }
        catch (Exception s)
        { 
        }
    }

    private void btSend_Click(object sender, EventArgs e)
    {
        try
        {
            //Send the input message.
            string text = this.richTextBox1.Text;
            _server.Send(Encoding.ASCII.GetBytes(text), text.Length);
            //Receive the response message.
            byte[] data = _server.Receive(ref _client);
            string msg = Encoding.ASCII.GetString(data, 0, data.Length);
            //Show the response message.
            this.richTextBox1.Text = msg;
        }
        catch (Exception exp)
        { 

        }
    }

}
}
  • 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-06-01T13:37:18+00:00Added an answer on June 1, 2026 at 1:37 pm

    You are not setting your destination. You need to either use UdpClient.Connect before using UdpClient.Send(Byte[], Int32) or use UdpClient.Send(Byte[], Int32, IPEndPoint).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to send a message from to global page to my injected.js on
I want to send a message from a server to all clients. There are
I try to send a sms using next code, but recipient gets my message
I try to send a request to my server via GET, but qooxdoo sends
i try to send some data to my php file from ajax , but
I try to send json array to ajax (from PHP to jQuery) in my
Hi I am trying to send a simple HTTP message from Flex to C#
Update - Moving to consistent type provided solution Client sends message to server socket,
I have a server that waits for incoming client messages and uses UDP. When
The context: We’re a small company that does not have an Exchange Server (or

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.