• 大小: 5.86MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-09-22
  • 语言: 其他
  • 标签: supersocket  套接字  

资源简介

使用一个开源的套接字通信框架(supersocket)来实现的模拟路灯通信的控制系统。 有兴趣研究套接字和supersocket相关技术的人士可以下载下来参考一下。

资源截图

代码片段和文件信息

/*
 * 由SharpDevelop创建。
 * 用户: cjt
 * 日期: 2013-8-4
 * 时间: 7:46
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

using UserControlLight;

namespace StreetLightClient
{
/// 
/// 模拟路灯
/// 

public partial class MainForm : Form
{
private Socket socket;
public delegate void InvokeTxtDelegate(string txt);
public MainForm()
{
InitializeComponent();

try
{
//连接服务器
EndPoint serverAddress = new IPEndPoint(IPAddress.Parse(“127.0.0.1“) 2013);
socket = new Socket(serverAddress.AddressFamily SocketType.Stream ProtocolType.Tcp);
socket.Connect(serverAddress);

Thread listenThread=new Thread(new ThreadStart(ListenFromServer));
listenThread.Start();
}
catch (Exception)
{
MessageBox.Show(“无法连接主机!“);
Environment.Exit(1);
}
}

//用于侦听从服务器发过来的消息
private void ListenFromServer()
{
byte[] info=new byte[256];
socket.Receive(info);

AsyncCallback callBack = new AsyncCallback(ListenCallBack);
socket.BeginReceive(info0 256 SocketFlags.NonecallBackinfo);
}

//异步接收到服务器发过来的消息后的回调方法
private void ListenCallBack(IAsyncResult result)
{
byte[] info=(byte[])result.AsyncState;
string message = Encoding.ASCII.GetString(info);
string[] messages=message.Split(‘ ‘);
switch (messages[0])
{
case “SETNAME“:
messages[1].Replace(“$“““);
SetName(messages[1]);
break;
default:
break;
}
}

private void Timer1Tick(object sender EventArgs e)
{
if(lightControl.LightState == EnumLightState.On)
{
if(lightControl.LightPower>0)
{
//路灯处于打开状态,电量不断减少
lightControl.LightPower--;

//向服务器端发送消息
byte[] info = Encoding.ASCII.GetBytes(“SETPOWER “+lightControl.LightPower+“$“);
socket.Send(info);
}
else
{
lightControl.LightState= EnumLightState.Off;

//向服务器端发送消息
byte[] info = Encoding.ASCII.GetBytes(“SETLIGHTSTATE OFF$“);
socket.Send(info);
}
}
else if(lightControl.LightState  == EnumLightState.Charging)
{
if(lightControl.LightPower<10)
{
//路灯处于充电状态,电量不断增加
lightControl.LightPower++;

//向服务器端发送消息
byte[] info = Encoding.ASCII.GetBytes(“SETPOWER “+lightControl.LightPower+“$“);
socket.Send(info);
}
}
}

//程序退出时断开连接
private void MainFormFormClosing(object sender FormClosingEventArgs e)
{
socket.Disconnect(false);
}

//灯亮事件
private  void LightControlLightOnEvent()
{
byte[] info = Encoding.ASCII.GetBytes(“SETLIGHTSTATE ON$“);
socket.Send(info);
}

//灯灭事件
private void LightControlLightOffEvent()
{
byte[] info = Encoding.ASCII.GetBytes(“SETLIGH

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2013-08-05 23:52  StreetLight\
     目录           0  2013-08-05 23:54  StreetLight\bin\
     文件      286720  2013-07-29 22:00  StreetLight\bin\log4net.dll
     文件       36864  2013-08-05 23:54  StreetLight\bin\StreetLightClient.exe
     文件         173  2013-08-04 07:46  StreetLight\bin\StreetLightClient.exe.config
     文件       24064  2013-08-05 23:54  StreetLight\bin\StreetLightClient.pdb
     文件       24576  2013-08-05 23:54  StreetLight\bin\StreetLightControl.exe
     文件         726  2013-08-05 21:10  StreetLight\bin\StreetLightControl.exe.config
     文件       26112  2013-08-05 23:54  StreetLight\bin\StreetLightControl.pdb
     文件       30720  2013-08-03 08:46  StreetLight\bin\SuperSocket.Common.dll
     文件       91648  2013-08-03 08:46  StreetLight\bin\SuperSocket.Common.pdb
     文件       57322  2013-08-03 08:46  StreetLight\bin\SuperSocket.Common.xml
     文件       17920  2013-08-03 08:46  StreetLight\bin\SuperSocket.Facility.dll
     文件       50688  2013-08-03 08:46  StreetLight\bin\SuperSocket.Facility.pdb
     文件       30037  2013-08-03 08:46  StreetLight\bin\SuperSocket.Facility.xml
     文件      100864  2013-08-03 08:46  StreetLight\bin\SuperSocket.Socketbase.dll
     文件      208384  2013-08-03 08:46  StreetLight\bin\SuperSocket.Socketbase.pdb
     文件      231933  2013-08-03 08:46  StreetLight\bin\SuperSocket.Socketbase.xml
     文件       86528  2013-08-03 08:46  StreetLight\bin\SuperSocket.SocketEngine.dll
     文件      208384  2013-08-03 08:46  StreetLight\bin\SuperSocket.SocketEngine.pdb
     文件       51038  2013-08-03 08:46  StreetLight\bin\SuperSocket.SocketEngine.xml
     文件       26112  2013-08-05 23:54  StreetLight\bin\UserControlLight.dll
     文件       22016  2013-08-05 23:54  StreetLight\bin\UserControlLight.pdb
     文件        2137  2013-08-05 22:23  StreetLight\StreetLight.sln
     目录           0  2013-08-05 23:50  StreetLight\StreetLightClient\
     文件         173  2013-08-04 07:46  StreetLight\StreetLightClient\app.config
     目录           0  2013-08-04 22:34  StreetLight\StreetLightClient\bin\
     文件        3763  2013-08-05 23:50  StreetLight\StreetLightClient\LightClientForm.cs
     文件        2858  2013-08-05 21:13  StreetLight\StreetLightClient\LightClientForm.Designer.cs
     文件        6011  2013-08-05 21:13  StreetLight\StreetLightClient\LightClientForm.resx
     目录           0  2013-08-04 07:45  StreetLight\StreetLightClient\obj\
............此处省略107个文件信息

评论

共有 条评论