0
Answer

UDP broadcast not received.. Help please..

JoJo

JoJo

16y
5.8k
1
Hi...
I have written a UDP client and server program (both in one application) which resides in the same computer. The server's ip is 192.168.2.2 and its port is 1001. The client's ip is also 192.168.2.2 and of course it will send data to 1001. The server is able to receive data from the client perfectly.

Next, I tried to send UDP broadcast packet from another computer. This time, the server is not able to capture the UDP broadcast. However, the packet is captured by Wireshark (on the server's computer) which means that packets did arrive..

The message on Wireshark is
"1    0.000000    192.168.2.4    255.255.255.255    UDP    Source port: 4328  Destination port: discard"
From the message on Wireshark, it can be seen that the UDP packet was sent from 192.168.2.4.

Anybody know why my server program below is able to receive data from client (in the same computer) but not able to receive a broadcast packet from another computer..?
Please advise.. Thanks..

Below is the client/server code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace UDPClient1
{
    public partial class Form1 : Form
    {
        IPEndPoint ipepC;
        IPEndPoint ipepS;   //ipep
        EndPoint epC;
        EndPoint epS;       //Remote
        EndPoint Sender;    //Sender
        Socket udpsrv;
        Socket udpclnt;     //newsock
        byte[] byteSend;
        byte[] byteRcv;     //data
        int intRcv;
        bool epSFlag = false;
        bool displayRcvFlag = false;
        int bufferSize = 320;
        string stringRcv = "";
        Thread serverThread;

        public Form1()
        {
            InitializeComponent();
        }
//****************** CLIENT
        private void button1_Click(object sender, EventArgs e)
        {
            ipepC = new IPEndPoint(IPAddress.Parse(textBox1.Text), Convert.ToInt16(textBox2.Text));
            udpclnt = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            udpclnt.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            byteSend = new byte[bufferSize];
            byteSend = Encoding.ASCII.GetBytes(textBox3.Text);
            udpclnt.SendTo(byteSend, ipepC);
        }

//************ SERVER
        private void button5_Click(object sender, EventArgs e)
        {
            ipepS = new IPEndPoint(IPAddress.Parse(textBox5.Text), Convert.ToInt16(textBox6.Text));
            udpsrv = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            udpsrv.Bind(ipepS);
            Sender = new IPEndPoint(IPAddress.Any, 0);
            epS = (EndPoint)(Sender);
           
            serverThread = new Thread(new ThreadStart(WaitReceive));
            serverThread.Start();            //epSFlag = true;
        }

        private void WaitReceive()
        {
            int recv;
            byte[] data;
            while (true)
            {
                data = new byte[1024];
                recv = udpsrv.ReceiveFrom(data, ref epS);
                stringRcv = ((Encoding.ASCII.GetString(data, 0, recv)));
                displayRcvFlag = true;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            udpsrv.Close();
            serverThread.Abort();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox4.Text = "";
        }

       

        private void tmrRcv_Tick(object sender, EventArgs e)
        {
         
        }

        private void tmrDisplay_Tick(object sender, EventArgs e)
        {
            if (displayRcvFlag == true)
            {
                textBox4.Text = stringRcv;
                displayRcvFlag = false;
            }
        }


    }
}