Home Forums Support client-server to exchange files (like in p2p?)

Tagged: ,

Viewing 7 posts - 21 through 27 (of 27 total)
  • Author
    Posts
  • #1001
    Anonymous
    Inactive

    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.

    #1002
    Anonymous
    Inactive

    Unfortunately 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,
    Marc

    #1003
    Anonymous
    Inactive

    I 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++);
    //
    //		}
    		
    	}
    #1004
    Anonymous
    Inactive

    Please 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 line Console.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)–

    #1005
    Anonymous
    Inactive

    I 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.Dictionary2 options) [0x00000] in <filename unknown>:0
    at DPSBase.DataSerializer.DeserialiseDataObject[String] (System.IO.MemoryStream receivedObjectStream, System.Collections.Generic.List1 dataProcessors, System.Collections.Generic.Dictionary2 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>:0

    Stack 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.Dictionary2 options) [0x00000] in <filename unknown>:0
    at DPSBase.DataSerializer.DeserialiseDataObject[String] (System.IO.MemoryStream receivedObjectStream, System.Collections.Generic.List1 dataProcessors, System.Collections.Generic.Dictionary2 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>:0

    What does It means ?
    Thanks
    Max

    #1007
    Anonymous
    Inactive

    That 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.

    #1008
    Anonymous
    Inactive

    I 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 🙁

Viewing 7 posts - 21 through 27 (of 27 total)
  • You must be logged in to reply to this topic.