仅仅是作为项目前期的调研,并不一定真的要创建自己的SOAP Server,但至少目前我对此比较感兴趣,呵呵,一切都是要从头开始。
首先熟悉TcpListener在PDA上面的应用情况,下面的例子是一个时间服务器DEMO,服务器在13端口监听传入的连接,一旦侦听到客户端的链接后,就发送时间给客户端,然后断开连接。
服务器是基于PPC的应用程序,既可以运行在PC上面,也可以运行在PDA上面,注意的是当PDA连接到PC上后,PC的网络连接会多出来一个,这个是用于和PDA通信的,相当于另外一个子网,需要注意其IP地址。源码如下:
客户端为了方便测试,是基于Windows的,不能运行在PDA上面,它的功能就是连接到指定的服务器并获取时间。源码如下:
首先熟悉TcpListener在PDA上面的应用情况,下面的例子是一个时间服务器DEMO,服务器在13端口监听传入的连接,一旦侦听到客户端的链接后,就发送时间给客户端,然后断开连接。
服务器是基于PPC的应用程序,既可以运行在PC上面,也可以运行在PDA上面,注意的是当PDA连接到PC上后,PC的网络连接会多出来一个,这个是用于和PDA通信的,相当于另外一个子网,需要注意其IP地址。源码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace PDATimeServer
{
public partial class Form1 : Form
{
private bool done = false;
TcpListener listener = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
listener=new TcpListener(IPAddress.Parse(comboBox1.Text.Trim()), 13);
listener.Start();
done = false;
while (!done)
{
int index=listBox1.Items.Add("Waiting for connection");
while (false == listener.Pending() && false==done)
{
Application.DoEvents();
}
if (done)
break;
TcpClient client = listener.AcceptTcpClient();
listBox1.Items[index] += "Connection accepted.";
NetworkStream ns = client.GetStream();
StreamWriter writer = new StreamWriter(ns);
writer.Write(DateTime.Now);
writer.Close();
ns.Close();
client.Close();
}
listener.Stop();
listBox1.Items.Add("Stop listening");
}
private void button2_Click(object sender, EventArgs e)
{
done = true;
}
private void Form1_Load(object sender, EventArgs e)
{
IPAddress[] ipList=Dns.GetHostByName(Dns.GetHostName()).AddressList;
foreach (IPAddress ip in ipList)
{
comboBox1.Items.Add(ip);
}
comboBox1.SelectedIndex = 0;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace PDATimeServer
{
public partial class Form1 : Form
{
private bool done = false;
TcpListener listener = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
listener=new TcpListener(IPAddress.Parse(comboBox1.Text.Trim()), 13);
listener.Start();
done = false;
while (!done)
{
int index=listBox1.Items.Add("Waiting for connection");
while (false == listener.Pending() && false==done)
{
Application.DoEvents();
}
if (done)
break;
TcpClient client = listener.AcceptTcpClient();
listBox1.Items[index] += "Connection accepted.";
NetworkStream ns = client.GetStream();
StreamWriter writer = new StreamWriter(ns);
writer.Write(DateTime.Now);
writer.Close();
ns.Close();
client.Close();
}
listener.Stop();
listBox1.Items.Add("Stop listening");
}
private void button2_Click(object sender, EventArgs e)
{
done = true;
}
private void Form1_Load(object sender, EventArgs e)
{
IPAddress[] ipList=Dns.GetHostByName(Dns.GetHostName()).AddressList;
foreach (IPAddress ip in ipList)
{
comboBox1.Items.Add(ip);
}
comboBox1.SelectedIndex = 0;
}
}
}
客户端为了方便测试,是基于Windows的,不能运行在PDA上面,它的功能就是连接到指定的服务器并获取时间。源码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace TimeClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TcpClient client = new TcpClient();
client.Connect(comboBox1.Text.Trim(), 13);
NetworkStream stream = client.GetStream();
StreamReader reader = new StreamReader(stream);
textBox1.Text=reader.ReadLine();
reader.Close();
stream.Close();
client.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
IPAddress[] ipList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
foreach (IPAddress ip in ipList)
{
comboBox1.Items.Add(ip);
}
comboBox1.Items.Add(IPAddress.Parse("169.254.2.1"));
comboBox1.SelectedIndex = 0;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace TimeClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TcpClient client = new TcpClient();
client.Connect(comboBox1.Text.Trim(), 13);
NetworkStream stream = client.GetStream();
StreamReader reader = new StreamReader(stream);
textBox1.Text=reader.ReadLine();
reader.Close();
stream.Close();
client.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
IPAddress[] ipList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
foreach (IPAddress ip in ipList)
{
comboBox1.Items.Add(ip);
}
comboBox1.Items.Add(IPAddress.Parse("169.254.2.1"));
comboBox1.SelectedIndex = 0;
}
}
}