public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//开始监听
private void button1_Click(object sender, EventArgs e)
{
//服务器开始监听客户端的请求
IPEndPoint thePoint = new IPEndPoint(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text));
//开始监听某T端口
TCPConnection.StartListening(thePoint, false);
button1.Text = "监听中";
button1.Enabled = false;
//此方法中包含服务器具体的处理方法。
StartListening();
}
private void StartListening()
{
//禁用日志记录 服务器端正式使用时,禁用日志记录
NetworkComms.DisableLogging();
//服务器端处理收到的消息
//为简单起见,此示例中我们只处理字符类型的信息,也返回字符类型的信息。
//处理的信息可以使自定义类,具体见下一个Demo
NetworkComms.AppendGlobalIncomingPacketHandler<string>("ReqFruitEngName", IncomingMsgHandle);
}
//处理某个具体的请求
private void IncomingMsgHandle(PacketHeader header, Connection connection, string msg)
{
try
{
string resMsg = "";
if (msg == "苹果")
resMsg = "Apple";
else if (msg == "桔子")
resMsg = "Tangerine";
else if (msg == "香蕉")
resMsg = "Banana";
connection.SendObject("ResFruitEngName", resMsg);
}
catch (Exception ex)
{
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
NetworkComms.Shutdown();
this.Dispose();
this.Close();
}
}
www.networkcomms.cn编辑
http://www.cnblogs.com/networkcomms
http://shop115882994.taobao.com