sending and receiving broadcast messages - broadcasting

Guys I need some help here..
I am doing a project in c# where the data needs to be sent as a datagram and receive data too which is broadcast.
The following is the code:
public void StartUdpListener(Object state)
{
receivedNotification = udpServer.Receive(ref remoteEndPoint);
notificationReceived = Encoding.ASCII.GetString(receivedNotification);
listBox = new StringBuilder(this.listBox1.Text);
listBox.AppendLine(notificationReceived);
if (listBox1.InvokeRequired)
{
this.Invoke((MethodInvoker)delegate { this.listBox1.Items.Add(listBox.ToString()); });
}
}
public void StartNotification()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(StartUdpListener));
hostName = Dns.GetHostName();
hostBuffer = Encoding.ASCII.GetBytes(hostName);
UdpClient newUdpClient = new UdpClient();
newUdpClient.Send(hostBuffer, hostBuffer.Length, notifyIP);
}
Could you guys please tell me if the code is fine because there is no one around with whom i can test the code on lan
Thanks a ton guys.

Why don't you set up your own broadcaster and listener in separate instances.
This is a great article on socket programming in c#

Related

Can a BLE Server identify which component is connected to it?

I have this project, where I need to create a BLE-Server (ESP32) to exchange data with a Laptop/Smartphone etc.... . The code is written with Arduino IDE, using the ESP32 BLE Arduino library and seems like the following
void Setup() {
BLEDevice::init("MIDI_BOX"); // Name of the BLE_Device
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService(SERVICE_UUID);
pCharacteristic = pService->createCharacteristic(
MIDI_CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_WRITE_NR
);
pCharacteristic->addDescriptor(new BLE2902());
pCharacteristic->setCallbacks(new MyCallbacks());
pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
BLEDevice::startAdvertising();
};
Having this said, when I want to connect my Laptop or Smartphone to this server. It needs to indetify which kind of device is connected to(Apple, Microsoft,). The manufacturer data seems a good idea for me but I don't how to get this information when there is a connection.
A callback fonction is provided by the BLEServer:
class MyServerCallbacks: public BLEServerCallbacks { //can I here do smth to get manufacturer data ??
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
Can anyone explain to me how it should be done, or are there any better way of thinking ?
Thnx in advance

Connection not working from Android to UWP on desktop - ZeroMq (NetMq)

I tried some examples of ZMQ on C++,C# and Python. I am trying to have Request-Reply pattern to connect Android device to PC running UWP with Xamarin forms.
Below is the Requestor code:
public void HelloWorld()
{
var timer = new Timer(60000);
timer.Start();
timer.Elapsed += (sender, args) =>
{
this.Cancel = true;
timer.Stop();
};
// Create
const string endpoint = "tcp://PC_ip:3245";
using (var request = new RequestSocket())
{
request.Bind(endpoint);
Thread.Sleep(2000);
while (!Cancel)
{
request.SendFrame("Requester says hello");
var reply = request.ReceiveFrameString();
Debug.WriteLine("Gets reply {0}",reply);
}
}
}
Reply socket code:
public void HelloWorld()
{
var timer = new Timer(60000);
const string endpoint = "tcp://PC_ip:3245";
timer.Start();
timer.Elapsed += (sender, args) =>
{
timer.Stop();
Cancel = true;
};
using (var replierSocket = new ResponseSocket())
{
replierSocket.Connect(endpoint);
Thread.Sleep(2000);
while (!Cancel)
{
var replyFromRequester = replierSocket.ReceiveFrameString();
Debug.WriteLine("Got reply {0}", replyFromRequester);
replierSocket.SendFrame("Response socket say hello");
}
}
}
Cancel is boolean
I went through some questions posted on this and added delay and these connection code blocks only trigger after button clicks on app.
While debugging , request.ReceiveFrameString() replierSocket.ReceiveFrameString(); are not even hit.
I am new to network programming , I understand that for REQ/REP pattern the code has to be in particular order which I traced and fixed I believe and turned off firewall on my PC so that firewall wont block my incoming connections from Android device.
PC_ip stands for IPv4 address I got from ipconfig /all for my wifi. I also tried external ip of my machine from sites like whatsmyip.org at ResponseSocket but I still dont get response between devices.
Please let me know what am I doing wrong.
Issue replication repository : GitHub/me/XamZeroMq

How to know who received a message in a MUC room

For my thesis, I am using Smack to log a XMPP network that uses the MUC module.
Another software is currently sending IoT sensor data into different MUC rooms.
I'd like to know for every message sent into a MUC room, which users were in that room at the time of the message. Is this possible? I could use a messageListener to every muc room, however the listener only receives a message as an argument. Therefore I could not know who is logged into the room inside the listener method.
you can get all muc message in StanzaListener in xmpp. Please follow few steps to done this
Step 1. Declare as a global variables
ChatManagerListener chatListener;
Chat chat;
StanzaListener packetListener;
Step 2. Use this code in oncreate or in fragment
Note: Make sure you have connected with chat server.
packetListener = new StanzaListener() {
#Override
public void processPacket(Stanza packet) throws SmackException.NotConnectedException, InterruptedException {
if (packet instanceof Message) {
final Message message = (Message) packet;
}
}
};
XMPP.getInstance().getConnection(acitiviy)).addAsyncStanzaListener(stanzaListener, null);
ServiceDiscoveryManager sdm = ServiceDiscoveryManager
.getInstanceFor(XMPP.getInstance().getConnection(acitiviy)));
sdm.addFeature("jabber.org/protocol/si");
sdm.addFeature("http://jabber.org/protocol/si");
sdm.addFeature("http://jabber.org/protocol/disco#info");
sdm.addFeature("jabber:iq:privacy");
Step 3. Methods for one to one chat purposer
void sendMessage(String message) {
if (chat != null) {
try {
chat.sendMessage(message);
Message msg = new Message();
msg.setTo(JidCreate.bareFrom(jid));
msg.setFrom(XMPP.getInstance().getConnection(acitiviy)
.getUser());
ChatStateExtension ext = new ChatStateExtension(
ChatState.paused);
msg.addExtension(ext);
lastComposing = System.currentTimeMillis();
chat.sendMessage(msg);
} catch (SmackException.NotConnectedException e) {
} catch (Exception e) {
}
}
}
Step 4. On destroy
XMPP.getInstance().getConnection(acitiviy)).removeAsyncStanzaListener(stanzaListener);
Hope this will help you and if you want more information take a look from here. Thankyou
Nothing prervents you from calling Multi UserCaht.getParticipants() from within the listener. But be warned: If your goal is to determine the other receivers of receivers, then this approach is fragile. I also suggest to think about using PubSub instead of MUC for your IoT use case.

MessageQueue Quirks while Sending Messages

Writing to remote MSMQ seems to be working on/off. I am not sure what is wrong and what else to do to confirm sending.
I am reluctant to setup some kind of ack. It seems to be an overkill.
using (var queue = new MessageQueue(queueName, QueueAccessMode.Send))
{
var messageQueueTransaction = new MessageQueueTransaction();
messageQueueTransaction.Begin();
try
{
queue.Formatter = new XmlMessageFormatter(new Type[] { typeof(EmailMessage) });
var msg = new Message();
msg.Label = emailMessage.Subject;
msg.Body = emailMessage;
queue.Send(msg, messageQueueTransaction);
messageQueueTransaction.Commit();
}
catch (Exception e)
{
LoggerLib.Logger.ErrorException(e, "Error Sending Email using MSMQ", emailMessage);
messageQueueTransaction.Abort();
}
finally
{
queue.Close();
}
}
The Connection string for MSMQ is in the format of:"FormatName:DIRECT=OS:FULLMACHINENAME\private$\emailmessagequeue"
Also, I used "FormatName:DIRECT:TCP:IPAddress\private$\emailmessagequeue".
It works without a glitch when I ran it locally. So, I allowed Everyone to have Full access and It still doesn't work.
Any ideas?
The port number 1801 was blocked. That resolved it. –

Looking for a SIP client that can use an h.264 RTSP stream as a video source

We are looking to integrate an IP camera (h.264 RTSP stream) with an asterisk PBX system for use in a school for distributed education (so a remote teacher can "dial in and teach").
Ideally we would like to be able to create a SIP client as an autoanswer pbx extension.
We are considering running a *nix box that can use a network video stream as a source for video, mix a separate audio source and present a SIP endpoint.
I understand that SIP express router may be able to:
"call an external C script, which could parse and change the SDP info within the SIP headers of clients it porxy's with, and change address of where it expects to recieve media from."
but I'm thinking it may be easier to look for a way to present an h.264 rtsp stream as /dev/videoX and use a standard SIP client.
If anyone has any pointers or any ideas for research I'd be really appreciative :-)
Thanks for reading!
W
p.s. there are IP cameras out there that claim to have SIP clients, but all I have seen only offer SIP for establishing a bi-directional audio session.
I don't know whether you found the correct answer to this question or not after all this time, but maybe I can give you some advice with VoIP and IP camera management and it would help anybody who has the same problem.
Your goal (if I understand your problem correctly) is basically creating a conference call solution which answers all the incoming calls and attach the IP camera video to all of these calls. I currently work at a company called Ozeki and - I don't know if there's an open-source solution for your problem or not - I'm going to show you an example code with SIP account registration, IP camera connection and answer all the incoming calls with the camera video.
So here is the source code:
public partial class Form1 : Form
{
private IIPCamera _camera;
private DrawingImageProvider _imageProvider;
private MediaConnector _connector;
private VideoViewerWF _videoViewerWf;
private ISoftPhone _softphone;
private IPhoneLine _phoneLine;
private IPhoneCall _call;
private PhoneCallAudioSender _audioSender;
private PhoneCallVideoSender _videoSender;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
_softphone = SoftPhoneFactory.CreateSoftPhone(5000, 10000);
_softphone.IncomingCall += softphone_IncomingCall;
_connector = new MediaConnector();
_videoSender = new PhoneCallVideoSender();
_audioSender = new PhoneCallAudioSender();
_imageProvider = new DrawingImageProvider();
_videoViewerWf = new VideoViewerWF();
SetVideoViewer();
}
private void SetVideoViewer()
{
CameraBox.Controls.Add(_videoViewerWf);
_videoViewerWf.Size = new Size(260, 180);
_videoViewerWf.BackColor = Color.Black;
_videoViewerWf.TabStop = false;
_videoViewerWf.FlipMode = FlipMode.None;
_videoViewerWf.Location = new Point(35, 30);
_videoViewerWf.Name = "_videoViewerWf";
}
private void Register(bool registrationRequired, string displayName, string userName, string authenticationId, string registerPassword, string domainHost)
{
try
{
var account = new SIPAccount(registrationRequired, displayName, userName, authenticationId, registerPassword, domainHost);
_phoneLine = _softphone.CreatePhoneLine(account);
_phoneLine.RegistrationStateChanged += phoneLine_RegistrationStateChanged;
_softphone.RegisterPhoneLine(_phoneLine);
}
catch (Exception ex)
{
label_Phoneline.Text = ex.Message;
}
}
private void phoneLine_RegistrationStateChanged(object sender, RegistrationStateChangedArgs e)
{
InvokeGuiThread(() => label_Phoneline.Text = e.State.ToString());
}
private void softphone_IncomingCall(object sender, VoIPEventArgs<iphonecall> e)
{
if (_camera != null)
{
_call = e.Item;
_call.CallStateChanged += call_CallStateChanged;
ConnectToCall();
_call.Answer();
}
}
private void ConnectToCall()
{
_videoSender.AttachToCall(_call);
_audioSender.AttachToCall(_call);
_connector.Connect(_camera.VideoChannel, _videoSender);
_connector.Connect(_camera.AudioChannel, _audioSender);
}
private void call_CallStateChanged(object sender, CallStateChangedArgs e)
{
InvokeGuiThread(() => label_Call.Text = e.State.ToString());
if (e.State == CallState.Completed)
if (_call != null)
{
_call.CallStateChanged -= call_CallStateChanged;
_connector.Disconnect(_camera.VideoChannel, _videoSender);
_connector.Disconnect(_camera.AudioChannel, _audioSender);
}
}
private void button_Connect_Click(object sender, EventArgs e)
{
_camera = IPCameraFactory.GetCamera("cameraIPAddress:8080", "admin", "admin");
_connector.Connect(_camera.VideoChannel, _imageProvider);
_videoViewerWf.SetImageProvider(_imageProvider);
_videoViewerWf.Start();
_camera.Start();
}
private void button_SIPRegister_Click(object sender, EventArgs e)
{
Register(true, "100", "100", "100", "100", "PBXAddress");
}
private void InvokeGuiThread(Action action)
{
BeginInvoke(action);
}
}
And the (example) GUI:
If you click on Connect button then the IPCameraFactory.GetCamera() will be called which is for connecting to the specified camera using the arguments (RTSP/RTP/HTTP messages in the background).
The Register button calls the Register() method to register your SIP account to the PBX. You can check the registration status at the Status label.
If there is an incoming call then the answer will occur automatically and the camera video and audio channel will attach to the call. So the caller will see and hear you.
Note:
Most of the IP camera microphones have really bad quality so you may connect your own microphone to the call. It's really simple too.
Create a Microphone object:
Microphone microphone = Microphone.GetDefaultDevice();
if (microphone != null)
microphone.Start();
And if there's an incoming call then connect this microphone to the call:
_connector.Connect(microphone, _audioSender);
And that's it. I hope I could help you!
live555 has some open-source implementations like openRTSP and playSIP which could help with your requirements.