Creating the Desktop Windows Application
The next step is to create the desktop application for sending messages. Add a new project to the current solution and name it
SMSSender_Desktop.
Populate the default Form1 with the controls shown in Figure 2.
 | |
Figure 2. Form1: Populate Form1 with these controls. |
Switch to the code-behind of
Form1 and import the following namespaces:
using System;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Text;
Listing 2 defines the SMSSender class to facilitate communication with the Windows Mobile application.
The SMSSender class contains a public SendMessage() method that allows you to send a message to the application on the Windows Mobile device. The private ReceiveMessage() method receives messages from the application on the Windows Mobile device and simply shows them using the MessageBox class. The constructor for the SMSSender class takes in a TcpClient object.
In Form1, define the following constant and objects:
public partial class Form1 : Form
{
const int portNo = 3456;
static IPAddress localAdd = IPAddress.Parse("169.254.2.2");
static TcpListener listener = new TcpListener(localAdd, portNo);
SMSSender smsSender;
When the form loads, listen for a TCP connection from the Windows Mobile device and then create an instance of the SMSSender class by passing it an instance of the
TcpClient object:
private void Form1_Load(object sender, EventArgs e)
{
//---listens from a connection from the WM device---
listener.Start();
smsSender = new SMSSender(
listener.AcceptTcpClient());
}
When the user has finished composing a message and clicks the Send button, the
SMSSender object sends the message to the Windows Mobile application to process:
//----send a message using the SmsSender object---
private void btnSend_Click(object sender, EventArgs e)
{
string sms = string.Empty;
sms += txtPhoneNumber.Text + "|";
sms += txtMessage.Text;
smsSender.SendMessage(sms);
}
As the user types the message, the number of characters remaining is displayed:
private void txtMessage_TextChanged(object sender, EventArgs e)
{
lblCount.Text = txtMessage.Text.Length + "/160";
}
 | |
Figure 3. It Works! You can now send SMS messages from the comfort of your desktop. |
Testing the Applications
To test the applications, first connect your device to the desktop using ActiveSync. Next, run the SMSSender_Desktop project and it will wait for an incoming socket connection from the device. At the same time, deploy the SMSSender project to the device and run it. When the application is started, it will automatically connect to the Windows application at >169.254.2.2. Once the connection is successful, you will see the Windows application appear. You can now send SMS messages from the comfort of your desktop (see Figure 3)!
You can use the techniques discussed to extend the sample applications in the following ways:
- Modify the application to retrieve all the messages stored on the device and then send them to the Windows application.
- Notify the user when an incoming message is received.
- Retrieve all the contact information from the device and show them in a dropdown list on the Windows application.