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

  • Home
  • SEARCH
  • 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 8556199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:17:33+00:00 2026-06-11T15:17:33+00:00

I am using system.Net.Socket to send and receive message from server to client.but am

  • 0

I am using system.Net.Socket to send and receive message from server to client.but am facing problem when multiple server broadcasting message then receiver program is being closed suddenly.

My server code for broadcasting is as follows..

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class Broadcst
{
    public static void Main()
    {
        Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        IPEndPoint iep1 = new IPEndPoint(IPAddress.Broadcast, 9050);
        IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("192.168.1.255"), 9050);

        string hostname = Dns.GetHostName();
        byte[] data = Encoding.ASCII.GetBytes(hostname);

        sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
        sock.SendTo(data, iep1);
        sock.SendTo(data, iep2);
        sock.Close();
    }
}

My Receiver Code is as follows for the client…

  using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;

    public class RecvBroadcst
    {
        public static void Main()
        {
            Socket sock = new Socket(AddressFamily.InterNetwork,
                            SocketType.Dgram, ProtocolType.Udp);

While(true){
  IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
            sock.Bind(iep);
            EndPoint ep = (EndPoint)iep;
            Console.WriteLine("Ready to receive...");

            byte[] data = new byte[1024];
            int recv = sock.ReceiveFrom(data, ref ep);
            string stringData = Encoding.ASCII.GetString(data, 0, recv);
            Console.WriteLine("received: {0}  from: {1}",
                                  stringData, ep.ToString());

            data = new byte[1024];
            recv = sock.ReceiveFrom(data, ref ep);
            stringData = Encoding.ASCII.GetString(data, 0, recv);
            Console.WriteLine("received: {0}  from: {1}",
                                  stringData, ep.ToString());

}
            sock.Close();
        }
    }

So how can a client receive more than one broadcast messages when there is more than one server broadcasting?

when i send message again from server its throwing error like “Only one usage of each socket address (protocol/network address/port) is normally permitted”

  • 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-11T15:17:34+00:00Added an answer on June 11, 2026 at 3:17 pm

    i had Solved the issue with the change in my above code with following in receiver side.i was just binding socket every time so i am just now binding it once and iterate only incoming data every time from the specified port.

    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    
    public class RecvBroadcst
    {
        public static void Main()
        {
           Socket sock = new Socket(AddressFamily.InterNetwork,
                               SocketType.Dgram, ProtocolType.Udp);
         //  int port_no=Convert.ToInt32(sock.RemoteEndPoint);
    
    
             int portnumber = 9050;
    
                sock = new Socket(AddressFamily.InterNetwork,
                               SocketType.Dgram, ProtocolType.Udp);
                IPEndPoint iep = new IPEndPoint(IPAddress.Any,portnumber);
                sock.Bind(iep);
                EndPoint ep = (EndPoint)iep;
                Console.WriteLine("Ready to receive...");
              // int port_no = Convert.ToInt32(sock.RemoteEndPoint);
                while (true)
                {
    
                    byte[] data = new byte[1024];
                    int recv = sock.ReceiveFrom(data, ref ep);
                    string stringData = Encoding.ASCII.GetString(data, 0, recv);
                    Console.WriteLine("received: {0}  from: {1}",
                                          stringData, ep.ToString());
    
                    //data = new byte[1024];
                    //recv = sock.ReceiveFrom(data, ref ep);
                    //stringData = Encoding.ASCII.GetString(data, 0, recv);
                    //Console.WriteLine("received: {0}  from: {1}",
                    //                      stringData, ep.ToString());
    
                    data = null;
                }
                //portnumber--;
    
    
            sock.Close();
        }
    }
    

    I just got succeeded..thanks for the response.that helps a lot.

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

Sidebar

Related Questions

I am trying to send an email in ASP.NET using system.net.mail the problem is
I'm developing an Asynchronous Game Server using .Net Socket Asynchronous Model( BeginAccept/EndAccept...etc.) The problem
i have a small problem sending image over Socket(client-server), i receive only UTF text
I am wondering what the best way to send bulk emails is using System.Net.Mail
I have a problem decoding HTML entities. I have tried using both System.Net.WebUtility.HtmlDecode() and
I'm trying to mock out the System.net.Sockets.Socket class in C# - I tried using
I'm using this client/server code which i found on the net for communication: client:
I read the msdn tutorial for using socket with Windows phone. ( http://msdn.microsoft.com/en-us/library/system.net.sockets.socket(v=VS.96).aspx )
My domain model is using System.Net.Uri to represent URLs, and System.Drawing.Color to represent colors.
using System; using System.IO; using System.Net; using System.Text.RegularExpressions; namespace Working { class Program4 {

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.