充电桩管理系统php源码,充电桩通讯(调试⼯具源码)【实例简介】
【实例截图】
【核⼼代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 充电桩测试软件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label10_Click(object sender, EventArgs e)
{
}
TcpListener listener;
delegate void SetTextCallBack(TextBox txt, string text);
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(textBox25.Text), Convert.ToInt32(textBox1.Text)); socket.Bind(endPoint);
socket.Listen(10);
Thread thread = new Thread(Listen);
thread.IsBackground = true;php文件管理系统mvc 源码
thread.Start(socket);
}
public void SetControlTextInfo(string msg)
{
TextBox txtMsgInfo = new TextBox();
txtMsgInfo = textBox2;
foreach (Control item in this.tabControl1.Controls)
{
if(item is TabPage)
{
TabPage tcItem = (TabPage)item;
foreach (var tbItem in tcItem.Controls)
{
if (tbItem is TextBox)
{
TextBox txtItem = (TextBox)tbItem;
if (txtItem.Tag != null)
{
if (msg.Contains(txtItem.Tag.ToString())&& txtItem.Tag.ToString()!="")
{
txtMsgInfo = txtItem;
int startIndex = msg.IndexOf(txtItem.Tag.ToString()) 6;
msg = msg.Substring(startIndex, (msg.Length - 6) - startIndex);
StringBuilder sb = new StringBuilder();
string[] strInfo = msg.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
for (int i = strInfo.Length - 1; i >= 0; i--)
{
sb.Append(strInfo[i]);
}
msg = sb.ToString();
}
}
}
}
}
}
if (txtMsgInfo == textBox2)
{
SetText(textBox2, socketSend.RemoteEndPoint.ToString() ":" Environment.NewLine msg Environment.NewLine);//RemoteEndPoint获取socketSend终端的ip地址和端⼝号信息
// if(txtMsgInfo.Text.Contains(""))
}
else
{
SetText(txtMsgInfo, msg);
}
}
Socket socketSend;
Dictionary SocketDic = new Dictionary();//这个是为了 实现服务器端对多个客户端进⾏收发消息 名字对应连接void Recive(object o)//监听等待进⼊的客户端
{
socketSend = o as Socket; //线程要⽤object 然后转换成需要的类型
while (true)
{
byte[] buffer = new byte[1024 * 1024 * 2];
int r = socketSend.Receive(buffer);
if (r == 0)
break;
StringBuilder strBuider = new StringBuilder();
for (int index = 0; index < r; index )
{
strBuider.Append(((int)buffer[index]).ToString("X2") " ");
}
string msg = strBuider.ToString();// Encoding.Default.GetString(buffer, 0, r);
// textBox26.Text = Environment.NewLine msg;
// msg = Convert.ToString(msg, 16);
if(msg.Contains("68 04 43 00 00 00"))
{
SendMsg("68 04 83 00 00 00");
}
if (msg.Contains("68 01 02"))
{
SendMsg(msg);
//byte[] bt = Encoding.Default.GetBytes(msg);
/
/int count = socketSend.Send(bt);
//if (count > 0)
//{
// // int jihuoCount = socketSend.Send(bt_JiHuo);
// //if (jihuoCount > 0)
// //{
// // string strDateTime = TimeToCP56Time2a(DateTime.Now);
// // byte[] bt_DateTime = Encoding.Default.GetBytes(strDateTime); // // int countTime = socketSend.Send(bt_DateTime);
// //}
//}
}
SetControlTextInfo(msg);
}
}
void Listen(object o) //接受客户端传信息
{
try
{
Socket socketWatch = o as Socket;
while (true)
{
socketSend = socketWatch.Accept();
SetText(textBox2,socketSend.RemoteEndPoint.ToString() ":" " ok"); string name = socketSend.RemoteEndPoint.ToString();
SocketDic.Add(name, socketSend);//键值对传值
Thread thread = new Thread(Recive);
foreach (var item in SocketDic)
{
if(item.Key.Contains("192.168.1.79"))
{
}
}
thread.IsBackground = true;
thread.Start(socketSend);
}
}
catch

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。