Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > what is the code(UDP )bettween server and multible client in C#
 

what is the code(UDP )bettween server and multible client in C#

what is the code(UDP )bettween server and multible client in C#

do you have ready code for one server connect with two client in resturant domain
written in C#??

nana_nisreen  Wednesday, August 19, 2009 8:38 PM

Hi nana_nisreen,

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 btStart_Click(object sed, 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, 16000);
            _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()
        {
            //Stop listening.
            _listenThread.Join();
            ShowMsg("Server stops.");
            _server.Close();
            //Changet state to indicate the server stops.
            _isServerStarted = false;
        }

        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 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)
        {
            _server.Close();
        }

        private void btSend_Click(object sender, EventArgs e)
        {
            //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;      
        }
        
    }
}

This link shows the details about UDPClient class and a sample:
http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.aspx.

Let me know if this helps.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, August 21, 2009 9:20 AM

Hi nana_nisreen,

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 btStart_Click(object sed, 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, 16000);
            _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()
        {
            //Stop listening.
            _listenThread.Join();
            ShowMsg("Server stops.");
            _server.Close();
            //Changet state to indicate the server stops.
            _isServerStarted = false;
        }

        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 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)
        {
            _server.Close();
        }

        private void btSend_Click(object sender, EventArgs e)
        {
            //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;      
        }
        
    }
}

This link shows the details about UDPClient class and a sample:
http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.aspx.

Let me know if this helps.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, August 21, 2009 9:20 AM

You can use google to search for other answers

Custom Search

More Threads

• three tier archetecture
• Problem with the configurationManager Class
• combobox in datagridview
• Other connection method
• Windows application communication
• Multi-dataset vs. multi-table
• Server and Office Web Components
• moving to the first,previous,next,last records
• pocket taskvision + vs2005 + emulator
• Incorrect parameter