- This topic has 26 replies, 2 voices, and was last updated 11 years, 5 months ago by Anonymous.
-
AuthorPosts
-
July 30, 2013 at 16:22 #1001AnonymousInactive
Hello. I’m returning on this post.
I have the necessity to find a way through your library to find in my wifi network all the device connected. In particular the Android or maybe iPhone, so
when I poll each I can know which kind of device is.
As you know I’ve tried the broadcast approach and It din’t work, so I’m asking if you are so gentle to submit to me a little sample code to start from (on xamarin)
Thanks in advance,
Max.July 30, 2013 at 16:37 #1002AnonymousInactiveUnfortunately it is not possible to provide a fully functional short code sample that would contain the necessary functionality. We provide larger code samples but only to our licensed customers.
My suggestion would be to have any connected devices listening on a given port and then send a short UDP packet from the server to all IP’s within your network. If any device responds you can then establish a TCP connection, e.g on the server:
int listeningPort = 10000; string localIPRange = "192.168.0"; for (int i = 1; i < 255; i++) UDPConnection.SendObject("aliveTest", 0, new IPEndPoint(IPAddress.Parse(localIPRange + "." + i.ToString()), listeningPort));
Regards,
MarcJuly 30, 2013 at 17:02 #1003AnonymousInactiveI have already done something similar, but It doesn’t work in the way I need to have in the same network devices which are server and client at the same time.
In this scenario I rum my sample. the device IP is 192.168.1.137. Tcp packets not returning to the device itself :public class MainActivity : Activity { int count = 1; private volatile bool _shouldStop = true; //private ThreadWorker workerObject = null; private Button button; private Button buttonstop; private Thread mThread = null; protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); // Set our view from the "main" layout resource SetContentView (Resource.Layout.Main); // // Get our button from the layout resource, // and attach an event to it button = FindViewById<Button> (Resource.Id.myButton); buttonstop = FindViewById<Button> (Resource.Id.buttonStop); buttonstop.Click += delegate { _shouldStop = false; if (mThread.ThreadState == ThreadState.Running) mThread.Join(); }; button.Click += delegate { // UDPConnection.SendObject("ChatMessage", // "This is the broadcast test message!", // new IPEndPoint(IPAddress.Broadcast, 10000)); // RangeFinder mRange = new RangeFinder(); // IPAddress ipStart = IPAddress.Parse("192.168.1.2"); // IPAddress ipEnd = IPAddress.Parse("192.168.1.255"); // IEnumerable<string> ipRange = mRange.GetIPRange(ipStart,ipEnd); mThread = new Thread(DoWork); mThread.Start(); }; NetworkComms.AppendGlobalIncomingPacketHandler<string>("ChatMessage", (packetHeader, connection, incomingString) => { Console.WriteLine("... Incoming message from " + connection.ToString() + " saying '" + incomingString + "'."); AlertDialog dialog = (new AlertDialog.Builder(this)).Create(); dialog.SetCancelable(false); // This blocks the 'BACK' button dialog.SetMessage("... Incoming message from " + connection.ToString() + " saying '" + incomingString + "'."); dialog.SetButton("OK", new EventHandler<DialogClickEventArgs>((obj, args) => { dialog.Dismiss(); })); dialog.Show(); }); //Start listening for incoming UDP data //UDPConnection.StartListening(true); //Start listening for new incoming TCP connections //Parameter is true so that we listen on a random port if the default is not available NetworkComms.DefaultListenPort = 10000; TCPConnection.StartListening(true); // foreach (var listenEndPoint in UDPConnection.ExistingLocalListenEndPoints()) { // Console.WriteLine (listenEndPoint.Address + ":" + listenEndPoint.Port); // } foreach (var listenEndPoint in TCPConnection.ExistingLocalListenEndPoints()) { Console.WriteLine (listenEndPoint.Address + ":" + listenEndPoint.Port); } } public void DoWork() { while (_shouldStop == true) { try{ IPAddress[] ips = new IPAddress[50]; string tempIP = "192.168.1."; for(int i = 100; i < 150; i++) { ips[i-100] = IPAddress.Parse(tempIP + (i + 1)); } foreach(IPAddress ip in ips) { Console.WriteLine("IP:"+ip.ToString()); RunOnUiThread(() => button.Text = string.Format ("{0} Message sent!", count++) ); ConnectionInfo mInfo = null; IPEndPoint targetEndPoint = new IPEndPoint(ip , 10000); try{ mInfo = new ConnectionInfo(targetEndPoint); TCPConnection.GetConnection(mInfo).SendObject("Message","helo"); //NetworkComms.SendObject("Message",ip.ToString(),10000,"helo"); Thread.Sleep(1); }catch (CommsException){} catch (Exception){} } } catch (System.NotSupportedException err1) { Console.WriteLine(err1.Message); } catch (CommsException err2) { Console.WriteLine(err2.Message); } catch (Exception err3) { Console.WriteLine(err3.Message); } } if (_shouldStop == false) mThread.Join (); Console.WriteLine("worker thread: terminating gracefully."); } //public extern void UpdateCounter(); //{ // // button.Text = string.Format ("{0} Message sent!", count++); // // } }
July 30, 2013 at 18:21 #1004AnonymousInactivePlease help me to understand why I can’t have any returned message (I’ve payed you a beer 😉 )
I adde to my code://Start listening for new incoming TCP connections //Parameter is true so that we listen on a random port if the default is not available NetworkComms.DefaultListenPort = 10000; NetworkComms.AppendGlobalIncomingPacketHandler<string>("Message", PrintIncomingMessage); UDPConnection.StartListening (true); private static void PrintIncomingMessage(PacketHeader header, Connection connection, string message) { Console.WriteLine("\nA message was recieved from " + connection.ToString() + " which said '" + message + "'."); }
This is debug log ( 192.168.1.137 is the ip of the device is running the app)
…..
192.168.1.136:10000
192.168.1.137:10000
Thread started: <Thread Pool> #5
Thread started: <Thread Pool> #6
192.168.1.138:10000
Thread started: ManagedThreadPool_9 #7
192.168.1.139:10000
…..
the app is never passing on this lineConsole.WriteLine("\nA message was recieved from " + connection.ToString() + " which said '" + message + "'.");
Please tell me Why ?
–I’m running the cicle into a thread (It is not thinkable to do in the main thread, because app will freeze)–July 30, 2013 at 20:22 #1005AnonymousInactiveI got this error into logs:
Base Exception Type: ProtoBuf.ProtoException: Invalid wire-type; this usually means you have over-written a file without truncating or setting the length; see http://stackoverflow.com/q/2152978/23354
at ProtoBuf.ProtoReader.ReadString () [0x00000] in <filename unknown>:0
at ProtoBuf.Meta.TypeModel.TryDeserializeAuxiliaryType (ProtoBuf.ProtoReader reader, DataFormat format, Int32 tag, System.Type type, System.Object& value, Boolean skipOtherFields, Boolean asListItem, Boolean autoCreate, Boolean insideList) [0x00000] in <filename unknown>:0
at ProtoBuf.Meta.TypeModel.DeserializeCore (ProtoBuf.ProtoReader reader, System.Type type, System.Object value, Boolean noAutoCreate) [0x00000] in <filename unknown>:0
at ProtoBuf.Meta.TypeModel.Deserialize (System.IO.Stream source, System.Object value, System.Type type, ProtoBuf.SerializationContext context) [0x00000] in <filename unknown>:0
at ProtoBuf.Meta.TypeModel.Deserialize (System.IO.Stream source, System.Object value, System.Type type) [0x00000] in <filename unknown>:0
at ProtoBuf.Serializer+NonGeneric.Deserialize (System.Type type, System.IO.Stream source) [0x00000] in <filename unknown>:0
at DPSBase.ProtobufSerializer.DeserialiseDataObjectInt (System.IO.Stream inputStream, System.Type resultType, System.Collections.Generic.Dictionary`2 options) [0x00000] in <filename unknown>:0
at DPSBase.DataSerializer.DeserialiseGeneralObject[String] (System.IO.MemoryStream receivedObjectStream, System.Collections.Generic.List1 dataProcessors, System.Collections.Generic.Dictionary
2 options) [0x00000] in <filename unknown>:0
at DPSBase.DataSerializer.DeserialiseDataObject[String] (System.IO.MemoryStream receivedObjectStream, System.Collections.Generic.List1 dataProcessors, System.Collections.Generic.Dictionary
2 options) [0x00000] in <filename unknown>:0
at NetworkCommsDotNet.PacketTypeHandlerDelegateWrapper`1[System.String].DeSerialize (System.IO.MemoryStream incomingBytes, NetworkCommsDotNet.SendReceiveOptions options) [0x00000] in <filename unknown>:0
at NetworkCommsDotNet.NetworkComms.TriggerGlobalPacketHandlers (NetworkCommsDotNet.PacketHeader packetHeader, NetworkCommsDotNet.Connection connection, System.IO.MemoryStream incomingDataStream, NetworkCommsDotNet.SendReceiveOptions options, Boolean ignoreUnknownPacketTypeOverride) [0x00000] in <filename unknown>:0Stack Trace: at ProtoBuf.ProtoReader.ReadString () [0x00000] in <filename unknown>:0
at ProtoBuf.Meta.TypeModel.TryDeserializeAuxiliaryType (ProtoBuf.ProtoReader reader, DataFormat format, Int32 tag, System.Type type, System.Object& value, Boolean skipOtherFields, Boolean asListItem, Boolean autoCreate, Boolean insideList) [0x00000] in <filename unknown>:0
at ProtoBuf.Meta.TypeModel.DeserializeCore (ProtoBuf.ProtoReader reader, System.Type type, System.Object value, Boolean noAutoCreate) [0x00000] in <filename unknown>:0
at ProtoBuf.Meta.TypeModel.Deserialize (System.IO.Stream source, System.Object value, System.Type type, ProtoBuf.SerializationContext context) [0x00000] in <filename unknown>:0
at ProtoBuf.Meta.TypeModel.Deserialize (System.IO.Stream source, System.Object value, System.Type type) [0x00000] in <filename unknown>:0
at ProtoBuf.Serializer+NonGeneric.Deserialize (System.Type type, System.IO.Stream source) [0x00000] in <filename unknown>:0
at DPSBase.ProtobufSerializer.DeserialiseDataObjectInt (System.IO.Stream inputStream, System.Type resultType, System.Collections.Generic.Dictionary`2 options) [0x00000] in <filename unknown>:0
at DPSBase.DataSerializer.DeserialiseGeneralObject[String] (System.IO.MemoryStream receivedObjectStream, System.Collections.Generic.List1 dataProcessors, System.Collections.Generic.Dictionary
2 options) [0x00000] in <filename unknown>:0
at DPSBase.DataSerializer.DeserialiseDataObject[String] (System.IO.MemoryStream receivedObjectStream, System.Collections.Generic.List1 dataProcessors, System.Collections.Generic.Dictionary
2 options) [0x00000] in <filename unknown>:0
at NetworkCommsDotNet.PacketTypeHandlerDelegateWrapper`1[System.String].DeSerialize (System.IO.MemoryStream incomingBytes, NetworkCommsDotNet.SendReceiveOptions options) [0x00000] in <filename unknown>:0
at NetworkCommsDotNet.NetworkComms.TriggerGlobalPacketHandlers (NetworkCommsDotNet.PacketHeader packetHeader, NetworkCommsDotNet.Connection connection, System.IO.MemoryStream incomingDataStream, NetworkCommsDotNet.SendReceiveOptions options, Boolean ignoreUnknownPacketTypeOverride) [0x00000] in <filename unknown>:0What does It means ?
Thanks
MaxJuly 31, 2013 at 11:15 #1007AnonymousInactiveThat error is most likely why things are not working correctly. That error generally means you are attempting to do something odd with the data you are sending and receiving.
– Are you only sending and receiving string objects at this point?
– Does your client or server write anything out to disk?To find out more enable logging on both client and server see this article, https://networkcomms.net/enable-logging/. If you post the log files somewhere that I can access them the problem is generally apparent.
Marc
N.B Thanks for the beer.
July 31, 2013 at 16:45 #1008AnonymousInactiveI have solved.
It seems the error was here:NetworkComms.AppendGlobalIncomingPacketHandler<string>("aliveTest", (packetHeader, connection, incomingString) => { try { Toast.MakeText(this, incomingString,ToastLength.Short).Show(); Console.WriteLine("... Incoming message from " + connection.ToString() + " saying '" + incomingString + "'."); }catch (Exception err){ Console.WriteLine(err.Message); } });
incomingString.ToString(); <—— Original code copy and pasted from your sample 🙁
-
AuthorPosts
- You must be logged in to reply to this topic.