I'm developing a WP7 app, working alongside a Windows (server) application. They are talking with each other using sockets.
As soon as the WP7 app opens, I wish to perform a network scan to find the server application.
I already found out how to get the IP address of the WP7. So let's say I know the server should be somewhere at 192.168.0.*. How do I go about scanning this network?
I tried many things, the last of which is the code below. The problem here is that somehow the TIMEOUT_MILLISECONDS parameter seems to be like playing roulette (tried everything in the range of 100-2000 with different success). In addition, if I sweep the whole subnet like in the code below the phone cannot seem to handle the work. I only get it to work if I set the timeout to 2000 (which is way to long) and by scanning up to 5 IP's in the for-loop, instead of the whole subnet.
Is there anyone that knows a better and much more efficient(!) way to do this?
Code:
string thisIP = "192.168.0.101"; // I normally get this from some other function
string[] arrIP = thisIP.ToString().Split('.');
string IPBase = arrIP[0] + "." + arrIP[1] + "." + arrIP[2] + ".";
//MessageBox.Show(address.ToString());
for (int i = 2; i < 254; i++) {
string IP = IPBase + i;
CheckConnection CheckConn = new CheckConnection();
string resultConnect = CheckConn.Connect(IP, int.Parse(Resource1.port));
if (resultConnect == "Success") {
CheckConn.Send("isAlive");
string result = CheckConn.Receive();
if (result.Contains("yes")) {
// We have found the server
break;
}
}
CheckConn.Close();
}
Code:
public class CheckConnection
{
// Cached Socket object that will be used by each call for the lifetime of this class
Socket _socket = null;
static ManualResetEvent _clientDone = new ManualResetEvent(false);
const int TIMEOUT_MILLISECONDS = 100;
const int MAX_BUFFER_SIZE = 2048;
public string Connect(string hostName, int portNumber) {
string result = string.Empty;
DnsEndPoint hostEntry = new DnsEndPoint(hostName, portNumber);
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
socketEventArg.RemoteEndPoint = hostEntry;
socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e) {
result = e.SocketError.ToString();
_clientDone.Set();
});
_clientDone.Reset();
_socket.ConnectAsync(socketEventArg);
_clientDone.WaitOne(TIMEOUT_MILLISECONDS);
return result;
}
public string Send(string data) {
string response = "Operation Timeout";
if (_socket != null) {
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
socketEventArg.RemoteEndPoint = _socket.RemoteEndPoint;
socketEventArg.UserToken = null;
socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e) {
response = e.SocketError.ToString();
_clientDone.Set();
});
byte[] payload = Encoding.UTF8.GetBytes(data);
socketEventArg.SetBuffer(payload, 0, payload.Length);
_clientDone.Reset();
_socket.SendAsync(socketEventArg);
// Block the UI thread for a maximum of TIMEOUT_MILLISECONDS seconds.
// If no response comes back within this time then proceed
_clientDone.WaitOne(TIMEOUT_MILLISECONDS);
}
else {
response = "Socket is not initialized";
}
return response;
}
public string Receive() {
string response = "Operation Timeout";
if (_socket != null) {
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
socketEventArg.RemoteEndPoint = _socket.RemoteEndPoint;
socketEventArg.SetBuffer(new Byte[MAX_BUFFER_SIZE], 0, MAX_BUFFER_SIZE);
socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e) {
if (e.SocketError == SocketError.Success) {
response = Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred);
response = response.Trim('\0');
}
else {
response = e.SocketError.ToString();
}
_clientDone.Set();
});
_clientDone.Reset();
_socket.ReceiveAsync(socketEventArg);
_clientDone.WaitOne(TIMEOUT_MILLISECONDS);
}
else {
response = "Socket is not initialized";
}
return response;
}
public void Close() {
if (_socket != null) {
_socket.Close();
}
}
}
Why you don't know the server IP?
I can't help you with your subnet scanning problem, don't have experience with that corner of WP7 programming.
But I do wonder what kind of arrangement leads to the problem that your app does not know the server address and therefore has to scan for it. Is neither a fixed IP available for your server nor some kind of DNS service up to tell you the current IP?
Even if there are multiple servers running and the phone has the job to somehow decide which one of those is responsible for it, you still could set up some kind of super-server that the phones could ask first which server they should address.
In any way, phones scanning subnets to find servers as some routine app startup action is a bad idea, if you ask me.
Please note that by 'server' I'm just revering to a software that accepts socket connections. This software is installed into a Windows computer that most likely has it's IP from DHCP.
The server software itself could present it's IP address so the end-user can type this address into the client on their WP7. Nice for tech-savvy folks, but that's not how software should be designed in my opinion. People don't need to know what an IP address is and shouldn't be force into typing 'weird numbers'. Properly designed software just have to work instantly and developers (again in my opinion) need to take care of a good design and that includes making things plug-and-play where they can.
Take a look at 'PC Remote' and you will see the kind of easy plug-and-play experience I'm looking for.
Did you check broadcast?
I'm really not network guy, but from Googling I got the impression that people use broadcast instead of IP range scanning for such IP number discovery tasks, and there seems to be a way to do broadcast on WP7 (albeit only using some tricks / hardly documented functionalilty):
E.g. see this thread:
http://stackoverflow.com/questions/8533471/udp-broadcasting-in-windows-phone-7
Which links to this:
http://forums.create.msdn.com/forums/t/88975.aspx
This makes sense to me after reading that broadcast is also the method how a client finds its DHCP server - isn't that the exact same scenario like yours?
Did you already check this way of looking at the problem, or maybe rule it out already for some reason?
It's better if you use a multicast group to send/receive packages. I had way better experience with it than using the Broadcast IP.
You need to join the multicast group with the phone and with the server app. I know there are multicast classes but I found it easier and mor convient to implement it with a socket...
And after you found the server, I'd use a persistent tcp connection which should improve your network performance (udp and wp7 is sometimes really strange...)
Thanks rbrunner7 and chabun for your comments, I appreciate your input!
rbrunner7 said:
Did you already check this way of looking at the problem, or maybe rule it out already for some reason?
Click to expand...
Click to collapse
I did, briefly, but abandoned this path after reading about the physical network equipment (switches etc.) having the need to support broadcasting which might not always be the case.
Anyway, your mentioned post describes 'limited broadcasting', which might be exactly what I should be looking for. Hopefully hardware limitations will not be applicable; I will look into this method further and will let you know how it works out!
roady001 said:
the physical network equipment (switches etc.) having the need to support broadcasting which might not always be the case.
Click to expand...
Click to collapse
I had the same doubts about that as it's also the case for multicasting. Most modern equipement supports it though and you can always provide the possibility to manually enter the server ip if you can't discover one via multicast/broadcast
Related
Hi all guys
i am new to C# and .net stuff as i was working with C++ for many years
i want to know that is it possibhle that i can have an MDI Child Form on my main Form and want it to return me some data
like for example i have my main Form has a "settings" button
when i click on "settings" button it should show a small MDI child form which has all the settings and return some data (i'll take care of data but i dont know how to return through a form)
thnx in advance
Well...as far as 'returning' data from the Form, you can't do that as a "return" call because Forms are objects, so the constructor cannot have a return type, however you can have a method that will set a variable in another class (that is public) to the data you want to return ... You can call this method when the form is closing.
I am not entirely sure if you can achieve the 'small form INSIDE another form' without creating your own control. I am not sure how practical/possible this would be, but you can try 1 of the following: extend the Control class (to make a control) or override the OnPaint of a new Form. This way you can define where stuff goes, and what gets drawn onto the screen.
I have used this piece of code in the past, which worked great.
in my example frmA was an MDIchildform of the main (MDIparent)
frmA()
{
public int a=0;
private btn_click(blabla)
{
frmOptions options = new frmOptions(this);
dialog.ShowDialog();
if(dialog.DialogResult == DialogResult.Yes)
{
dosomethingwith(a);
}
else
{
donothingwith(a);
}
}
}
public partial class frmOptions : Form
{
frmA callingform = null;
public frmOptions(frmA x)
{
callingform = x;
}
private void dosomething()
{
callingform.a = value;
}
private void btnSaveSettings_click()
{
this.DialogResult = DialogResult.Yes;
}
private void btnCancelSettings_click()
{
this.DialogResult = DialogResult.No;
}
}
good luck
Hey all
I have the following code to open a socket on Windows Mobile 6.1:
Code:
string hostIP = "192.168.0.2";
IPAddress ip = IPAddress.Parse("192.168.0.2");
int port = 80;
IPEndPoint localEP = new IPEndPoint(ip,port);
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp);
try
{
listener.Bind(localEP); // socket exception here
listener.Listen(15);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
The goal is to simply open a socket, so I can develop on top of this and receive web requests but I receive a socket exception on the line where the bind takes place. Any ideas what might be going on?
maybe the options?
serverIPEndPoint = new IPEndPoint( serverIP, serverPort );
listenerSock = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
listenerSock.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1 );
listenerSock.Bind( serverIPEndPoint );
listenerSock.Listen( 200 );
I am learning programming and trying to finish off my first WP7 application.
My day job is a corporate commercial attorney in Montreal, Quebec Canada but I like to experiment with programming as a hobby.
I was surprised that no easy mechanism was provided for an End User Licence Agreement so I drafted one using precedents.
This EULA not only protects your ip but it provides for updates, additional services and, most importantly, limitation of liability and warranty.
I want the user to be presented with the usual "I agree" button when loading the application so I set up a key in the application settings entitled licence with a bool variable that stores false if the user has not accepted the EULA and true if the user has accepted this. I then check this key on app startup to determine the page I present to the user, whether it be the EULA page or the 1st page of the application. Once the user clicks the I agree, the key is set to true and the EULA is not presented again.
On mainpage I use the following code to test if the licence was accepted (it is basic, but works):
public Classes.AppSettings appsettings = new Classes.AppSettings();
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
// Display Message box for licence
if (!appsettings.getsettings("licence"))
{
this.NavigationService.Navigate(new Uri("/EULA.xaml", UriKind.Relative));
}
}
As you can see, if licence is false, there is a navigation to the eula page.
Here is the function I use to store key in the application settings. It works for bool variables and can be re-used.
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.IO.IsolatedStorage;
namespace Car_Mileage_Tracker_2.Classes
{
public class AppSettings
{
// This class is used to store setting in application settings
// SETTINGS
// FIRSTUSE bool TRUE is not first use FALSE is first use
// LICENCE bool TRUE is licence accepted FALSE is licence not accepted
// HELP bool TRUE is help messaged displayed FALSE is help message not displayed
// UNITS bool TRUE is km and FALSE is miles
public AppSettings(){}
public bool getsettings(string key)
{
var store = IsolatedStorageSettings.ApplicationSettings;
bool value = true;
if (store.Contains(key))
{
try
{
store.TryGetValue(key, out value);
return value;
}
catch
{
MessageBox.Show("Could not retrieve setting " + key + " from Application Settings");
return false;
}
}
else { return false; }
}
public void setsettings(string key, bool value)
{
var store = IsolatedStorageSettings.ApplicationSettings;
if (store.Contains(key))
{
try { store[key] = value; }
catch
{
MessageBox.Show("Could not save " + key + " setting in Application Settings");
}
}
else { store.Add(key, value); }
}
}
}
I have attached the EULA xaml page as well as the EULA cs page and a word document containing my sample EULA. To circumvent upload restrictions I have added txt to the xaml and cs files, you simply need to rename them. You will need to fill in the info for your company and edit section 13 to provide for your local court district and state/province/country. Also, I have not yet implemented the code for clicking on the I disagree button. This seems to be a touchy issue in WP7 Silverlight as there is no way to cause an application to terminate except by raising an exception which may be against marketplace rules. I probably will show another page explaining to the user that he/she cannot use the application without agreeing to the EULA but I am not there yet
I feel that this EULA affords me sufficient protection as a developer of an application but make no warranties to this effect and it is always recommended to have it verified by your lawyer.
I hope this may be of use to some of you developing apps.
If you have comments or suggestions, let me know or send me a pm. If you appreciate and use this, then you can, but are not obligated to send me some coffee money or a copy of your app
I will eventually translate the EULA in French, but if would be great if we could get a Spanish, Italian and German version of this.
Have fun.
Message to thread Moderator: If this is not posted in the proper thread/forum, please feel free to move it to the correct one.
Hi all!
Since days I have a problem now. I have an app that manage Entries in a list A. If one of these entries End-Date is today I whant that entry to be shown in a second list B. This is checked when I return from the "addNewItem" Window so I use onNavigatedTo.
Code:
// Static Variables
public static ObservableCollection<Item> lstToday = new ObservableCollection<Item>();
public static ObservableCollection<Item> lstWeek = new ObservableCollection<Item>();
public static ObservableCollection<Item> lstAll = new ObservableCollection<Item>();
// Constructor
public MainPage()
{
InitializeComponent();
MessageBox.Show("Initialize Component");
lbToday.ItemsSource = lstToday;
lbWeek.ItemsSource = lstWeek;
lbAll.ItemsSource = lstAll;
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
DateTime currentDate = DateTime.Now;
foreach (Item item in lstAll)
{
if (item.endDate.ToString("yyyy'-'MM'-'dd").Equals(currentDate.ToString("yyyy'-'MM'-'dd")))
{
lstToday.Add(item);
MessageBox.Show("Item '" + item.name + "' added to Todaylist");
}
}
}
private void appBarNew_Click(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri(string.Format("/EditAddItem.xaml"), UriKind.Relative));
}
After that I get an "System.Diagnostics.Debugger.Break();" error that doesn't occur when I delete "lbToday.ItemsSource = lstToday;" to avoid the ListBox Binding.
With lbAll ist runs without any problems!
Can I bind the Listbox direct to the ObservableCollection in XAML=
I really don't know, whats to do. So do you?
It would make my day!
Thanks!
What is the exception message and stack trace when the exception is thrown? (you can browse the properties of the exception using visual studio).
Your code actually works for me (although I had to make some assumptions about what is in lstAll as you don't mention that, and you may have an error in the DataTemplate for the listbox for binding your Item class)
Things to try:
Have you tried it with non static observable collections?
Use binding assignments in the xaml rather than setting the itemssource directly (e.g. using a viewmodel). Then you can let the phone engine worry about when to do the assigments. If you do that don't forget to set the page's datacontext property.
Try it with simple collections of strings first (rather than binding an 'item' class) so you can check it's working without a datatemplate.
Hope you fix it.
Ian
Hi,
I'm trying to hack an old game (it isn't available in the store anymore) for fun and learning purposes. I'm running W10 but the app is targeted for WP7.
The game encrypts all the files it saves with AES, using the Device Unique Id as the key. It gets the DUID with the following code:
Code:
private string DeviceId
{
get
{
if (string.IsNullOrEmpty(this.id))
{
byte[] numArray = (byte[]) DeviceExtendedProperties.GetValue("DeviceUniqueId");
this.id = "";
foreach (byte num in numArray)
this.id += num.ToString("X2");
if (string.IsNullOrEmpty(this.id))
this.id = "0D0FC36982524EBB25E1CA471F32824286B90F47";
}
return this.id;
}
}
The thing is I don't know how to get that ID from outside the app, which might be unique for the device and publisher. I've searched in all the app files in AppData and seems like it doesn't record the ID anywhere.
I've tried for days and considered other approaches with no luck. Ideas? Suggestions? Any help? Thanks.