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

#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++);
//
//		}
		
	}