控制中心
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 Control : Form
{
public Control()
{
InitializeComponent();
}
//定义回调:解决跨线程访问问题
private delegate void SetTextValueCallBack(string strValue);
private delegate void SetTextValueCallBack2(string strValue);
private delegate void SetTextValueCallBack3(string strValue);
//定义接收客户端发送消息的回调
private delegate void ReceiveMsgCallBack(string strReceive);
private delegate void ReceiveMsgCallBack2(string strReceive);
//声明回调
private SetTextValueCallBack setCallBack;
private SetTextValueCallBack2 setCallBack2;
private SetTextValueCallBack3 setCallBack3;
//声明
private ReceiveMsgCallBack receiveCallBack;
private ReceiveMsgCallBack2 receiveCallBack2;
//用于通信的Socket
Socket socketProduct;
//用于监听的SOCKET
Socket socketWatch;
//将远程连接的客户端的IP地址和Socket存入集合中
Dictionary<string, Socket> dicSocket = new Dictionary<string, Socket>();
//创建监听连接的线程
Thread AcceptSocketThread;
//接收客户端发送消息的线程
Thread threadReceive;
//Thread threadReceive2;
//变量设置
public int sum_need = 0;
public int sum_suit = 0;
public int box_cap = 20;
public int have_del = 0;
public int have_box = 0;
public bool isStart = false;
//开始监听
private void Start_Monitor_Click(object sender, EventArgs e)
{
//当点击开始监听的时候 在服务器端创建一个负责监听IP地址和端口号的Socket
socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//获取ip地址
IPAddress ip = IPAddress.Parse(this.txt_IP.Text.Trim());
//创建端口号
IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(this.txt_Port.Text.Trim()));
//绑定IP地址和端口号
socketWatch.Bind(point);
this.txt_Control.AppendText("监听成功" + " \r \n");
//开始监听:设置最大可以同时连接多少个请求
socketWatch.Listen(10);
//实例化回调
setCallBack = new SetTextValueCallBack(SetTextValue);
setCallBack2 = new SetTextValueCallBack2(SetTextValue2);
setCallBack3 = new SetTextValueCallBack3(SetTextValue3);
receiveCallBack = new ReceiveMsgCallBack(ReceiveMsg);
receiveCallBack2 = new ReceiveMsgCallBack2(ReceiveMsg2);
//创建线程
AcceptSocketThread = new Thread(new ParameterizedThreadStart(StartListen));
AcceptSocketThread.IsBackground = true;
AcceptSocketThread.Start(socketWatch);
}
/// 等待生产车间的连接,并且创建与之通信用的Socket
private void StartListen(object obj)
{
Socket socketWatch = obj as Socket;
while (true)
{
//等待客户端的连接,并且创建一个用于通信的Socket
socketProduct = socketWatch.Accept();
//获取远程主机的ip地址和端口号
string strIp = socketProduct.RemoteEndPoint.ToString();
//Console.WriteLine(strIp);
dicSocket.Add(strIp, socketProduct);
//Console.WriteLine(dicSocket.First().Key);
//Console.WriteLine(dicSocket.Last().Key);
//this.cmb_Socket.Invoke(setCmbCallBack, strIp);
string strMsg = "远程主机:" + socketProduct.RemoteEndPoint + "连接成功";
//this.txt_Control.AppendText(strMsg);
//使用回调
txt_Control.Invoke(setCallBack3, strMsg);
//定义接收客户端消息的线程
Thread threadReceive = new Thread(new ParameterizedThreadStart(Receive));
threadReceive.IsBackground = true;
threadReceive.Start(socketProduct);
//Thread threadReceive2 = new Thread(new ParameterizedThreadStart(Receive2));
//threadReceive2.IsBackground = true;
//threadReceive2.Start(socketProduct);
}
}
/// 控制中心不停的接收生产车间发送的消息
private void Receive(object obj)
{
Socket socketSend = obj as Socket;
while (true)
{
//客户端连接成功后,服务器接收客户端发送的消息
byte[] buffer = new byte[2048];
//实际接收到的有效字节数
int count = socketSend.Receive(buffer);
if (count == 0)//count 表示客户端关闭,要退出循环
{
break;
}
else
{
if (socketSend.RemoteEndPoint.ToString() == dicSocket.Last().Key)
{
string str = Encoding.Default.GetString(buffer, 0, count);
if(str== "装配完成,正在配送")
{
txt_Delivery.Invoke(receiveCallBack2, str);
}
else
{
string strReceiveMsg = "接收:" + socketSend.RemoteEndPoint + "发送的消息:" + "已完成装配" + str + "箱";
txt_Delivery.Invoke(receiveCallBack2, strReceiveMsg);
}
}
//txt_Produce.Invoke(receiveCallBack, strReceiveMsg);
else if (socketSend.RemoteEndPoint.ToString() == dicSocket.First().Key)
{
string str = Encoding.Default.GetString(buffer, 0, count);
string strReceiveMsg = "接收:" + socketSend.RemoteEndPoint + "发送的消息:" +"已生产防护服"+ str+"套";
txt_Produce.Invoke(receiveCallBack, strReceiveMsg);
sum_suit = int.Parse(str);
if (sum_suit - have_del >= 20 || sum_suit == sum_need)
{
//double temp = sum_suit - have_del / box_cap;
//string strMsg = (Math.Ceiling(temp)).ToString();
int inc = 1;
have_del = have_del + 20;
have_box++;
string strMsg = inc.ToString();
//Console.WriteLine(strMsg);
byte[] buffer_del = Encoding.Default.GetBytes(strMsg);
List<byte> list = new List<byte>();
//Console.WriteLine(list);
list.Add(0);
list.AddRange(buffer_del);
//将泛型集合转换为数组
byte[] newBuffer = list.ToArray();
//获得用户选择的IP地址
string ip = dicSocket.Last().Key.ToString();
//Console.WriteLine(newBuffer);
dicSocket[ip].Send(newBuffer);
//通知配送车间任务结束
if(sum_suit == sum_need)
{
int flag = 8;
string strFlag = flag.ToString();
byte[] buffer_flag = Encoding.Default.GetBytes(strFlag);
List<byte> list_flag = new List<byte>();
//Console.WriteLine(list);
list_flag.Add(0);
list_flag.AddRange(buffer_flag);
//将泛型集合转换为数组
byte[] newBuffer_flag = list_flag.ToArray();
//Console.WriteLine(newBuffer);
dicSocket[ip].Send(newBuffer_flag);
}
}
}
else
{
Console.WriteLine("生产结束");
}
}
}
}
///// 控制中心不停的接收配送车间发送的消息
//private void Receive2(object obj)
//{
// Socket socketSend = obj as Socket;
// while (true)
// {
// //客户端连接成功后,服务器接收客户端发送的消息
// byte[] buffer = new byte[2048];
// //实际接收到的有效字节数
// int count = socketSend.Receive(buffer);
// if (count == 0)//count 表示客户端关闭,要退出循环
// {
// break;
// }
// else
// {
// //string strReceiveMsg1 = "这里是配送车间";
// //txt_Delivery.Invoke(receiveCallBack2, strReceiveMsg1);
// if (socketSend.RemoteEndPoint.ToString() == dicSocket.Last().Key)
// {
// string str = Encoding.Default.GetString(buffer, 0, count);
// string strReceiveMsg = "接收:" + socketSend.RemoteEndPoint + "发送的消息:" + str;
// txt_Delivery.Invoke(receiveCallBack2, strReceiveMsg);
// have_box = int.Parse(str);
// have_del = have_box * 20;
// threadReceive2.Interrupt();
// }
// }
// }
//}
/// 回调委托需要执行的方法
private void SetTextValue(string strValue)
{
this.txt_Produce.AppendText(strValue + " \r \n");
}
private void SetTextValue2(string strValue)
{
this.txt_Delivery.AppendText(strValue + " \r \n");
}
private void SetTextValue3(string strValue)
{
this.txt_Control.AppendText(strValue + " \r \n");
}
private void ReceiveMsg(string strMsg)
{
this.txt_Produce.AppendText(strMsg + " \r \n");
}
private void ReceiveMsg2(string strMsg)
{
this.txt_Delivery.AppendText(strMsg + " \r \n");
}
//服务器给客户端发送消息
private void btn_produce_Click(object sender, EventArgs e)
{
try
{
//string str_need = "123";
//byte[] buffer_need = Encoding.Default.GetBytes(str_need);
//dicSocket.First().Value.Send(buffer_need);
sum_need = 0;
sum_suit = 0;
box_cap = 20;
have_del = 0;
have_box = 0;
isStart = false;
//把生产需求发给生产车间
sum_need = int.Parse(this.txt_Need.Text.Trim());
string strNeed = this.txt_Need.Text.Trim();
byte[] buffer_need = Encoding.Default.GetBytes(strNeed);
List<byte> list_need = new List<byte>();
list_need.Add(0);
list_need.AddRange(buffer_need);
//将泛型集合转换为数组
byte[] newBuffer_nedd = list_need.ToArray();
//获得用户选择的IP地址
string ip1 = dicSocket.First().Key.ToString();
dicSocket[ip1].Send(newBuffer_nedd);
//double temp = sum_suit - have_del / box_cap;
//string strMsg = (Math.Ceiling(temp)).ToString();
//byte[] buffer = Encoding.Default.GetBytes(strMsg);
//List<byte> list = new List<byte>();
//list.Add(0);
//list.AddRange(buffer);
////将泛型集合转换为数组
//byte[] newBuffer = list.ToArray();
////获得用户选择的IP地址
//string ip = dicSocket.Last().Key.ToString();
//if (Math.Ceiling(temp) > 0)
//{
// dicSocket[ip].Send(newBuffer);
//}
}
catch (Exception ex)
{
MessageBox.Show("给客户端发送消息出错:" + ex.Message);
}
//socketSend.Send(buffer);
}
//停止监听
private void Stop_Monitor_Click(object sender, EventArgs e)
{
socketWatch.Close();
socketProduct.Close();
//终止线程
AcceptSocketThread.Abort();
threadReceive.Abort();
}
}
}
生产车间
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;
namespace 生产车间
{
public partial class 生产车间 : Form
{
private int startY = 0;
private int startY3 = 0;
public 生产车间()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
timer1.Enabled = false;//timer控件初始化
timer1.Interval = 50;
}
private static int sum_suit = 0;//已生产数量
private static int sum_need = 0;//工作需求
private static int isStart = 1;//是否开始
//public static string IP ="192.168.1.143" ;
//public static string IP_Port = "1111";
//定义回调
private delegate void SetTextCallBack(string strValue);
//声明
private SetTextCallBack setCallBack;
//定义接收服务端发送消息的回调
private delegate void ReceiveMsgCallBack(string strMsg);
//声明
private ReceiveMsgCallBack receiveCallBack;
//创建连接的Socket
Socket socketSend;
//创建接收客户端发送消息的线程
Thread threadReceive;
private void Begin_Click(object sender, EventArgs e)
{
try
{
socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse(this.txt_IP.Text.Trim());
socketSend.Connect(ip, Convert.ToInt32(this.txt_Port.Text.Trim()));
//实例化回调
setCallBack = new SetTextCallBack(SetValue);
receiveCallBack = new ReceiveMsgCallBack(SetValueB);
this.txt_Log.Invoke(setCallBack, "连接成功" + System.Environment.NewLine);
//开启一个新的线程不停的接收服务器发送消息的线程
threadReceive = new Thread(new ThreadStart(Receive));
//设置为后台线程
threadReceive.IsBackground = true;
threadReceive.Start();
//创建一个线程不停的生产
ThreadStart threadStart = new ThreadStart(Producing);
Thread thread = new Thread(threadStart);
thread.Start();
//创建一个线程不停的发送
ThreadStart threadStart1 = new ThreadStart(Sending);
Thread thread1 = new Thread(threadStart1);
thread1.Start();
timer1.Start();
//创建一个线程不停更新窗口
}
catch (Exception ex)
{
MessageBox.Show("连接服务端出错:" + ex.ToString());
}
}
public static void Producing()//这是不停生产的函数
{
while (true)
{
if (sum_suit < sum_need)
{
sum_suit++;
Thread.Sleep(1000);
//休眠一秒
// Console.WriteLine(sum_suit);
}
if(sum_suit==sum_need&&sum_suit>0)
{
sum_need = 0;
sum_suit = 0;
}
}
}
public void Sending()//这是不停发送的函数
{
try
{
// while (isStart == 1 && sum_need > sum_suit || isStart == 1 && sum_need == 0)
while (true)
{
int x = sum_suit;
if (isStart == 1 && sum_need >= x)
{
if(sum_suit!=0)
this.txt_Log.AppendText("本次生产防护服" + sum_suit + "套 \r \n" + System.Environment.NewLine);
if (sum_suit == sum_need && sum_need != 0)//当sum_suit等于sum_need的时候x加1
x = sum_suit + 1;
if (sum_suit > 0)
{
string strMsg;
byte[] buffer;
int receive;
// strMsg = sum_need.ToString();
strMsg = sum_suit.ToString();
buffer = new byte[2048];
buffer = Encoding.Default.GetBytes(strMsg);
receive = socketSend.Send(buffer);
Thread.Sleep(1000);
// int y = sum_suit-1;
// this.txt_Log.AppendText("本次生产防护服" + sum_suit + "套 \r \n"+ System.Environment.NewLine);
}
else
{
Thread.Sleep(1000);
}
// } while (isStart == 1 && sum_need+1 > sum_suit );
}
}
}
catch (Exception ex)
{
MessageBox.Show("发送消息出错:" + ex.Message);
}
//发送
}
/// 接收服务器发送的消息
private void Receive()
{
try
{
while (true)
{
byte[] buffer = new byte[2048];
//实际接收到的字节数
int r = socketSend.Receive(buffer);
if (r == 0)
{
break;
}
else
{
//判断发送的数据的类型
if (buffer[0] == 0)//表示发送的是文字消息
{
string str = Encoding.Default.GetString(buffer, 1, r - 1);
sum_need = int.Parse(str);
// this.txt_Log.Invoke(receiveCallBack, "接收远程服务器:" + socketSend.RemoteEndPoint + "发送的消息:" + str);
this.textBox.Invoke(receiveCallBack, str);
//break;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("接收服务端发送的消息出错:" + ex.ToString());
}
}
private void SetValue(string strValue)
{
this.txt_Log.AppendText(strValue + "\r \n");
}
private void SetValueB(string strValue)
{
this.textBox.AppendText(strValue + "\r \n");
}
private void Stop_Click(object sender, EventArgs e)
{
isStart = 0;
//关闭socket
socketSend.Close();
//终止线程
threadReceive.Abort();
timer1.Stop();
}
//动画定时
private void timer1_Tick(object sender, EventArgs e)
{
if (sum_need > 0&&sum_need>sum_suit)
{
pictureBox2.Visible = true;
pictureBox3.Visible = true;
int i = pictureBox2.Location.X + 10;//图片坐标移动
int j = pictureBox3.Location.X + 10;
if (i > 700)
{
i = 300;
}
if (j > 700)
{
j = 300;
}
startY = pictureBox2.Location.Y;
startY3 = pictureBox3.Location.Y;
pictureBox2.Location = new Point(i, startY);
pictureBox2.Refresh();//不断刷新
pictureBox3.Location = new Point(j, startY3);
pictureBox3.Refresh();//不断刷新
timer1.Start();
}
else
{
pictureBox2.Visible = false;
pictureBox3.Visible = false;
}
}
}
}
运输中心
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;
namespace 客户端
{
public partial class Form1 : Form
{
public static Form1 MainForm = null;
public Form1()
{
InitializeComponent();
MainForm = this;
}
private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
}
//定义回调
private delegate void SetTextCallBack(string strValue);
//声明
private SetTextCallBack setCallBack;
//定义接收服务端发送消息的回调
private delegate void ReceiveMsgCallBack(string strMsg);
//声明
private ReceiveMsgCallBack receiveCallBack;
//创建连接的Socket
Socket socketSend;
//创建接收客户端发送消息的线程
Thread threadReceive;
//已装箱数量(箱数)
int have_box=0;
//现在有的箱数
int now_box;
//连接
private void Connect_Click(object sender, EventArgs e)
{
try
{
socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse(this.txt_IP.Text.Trim());
socketSend.Connect(ip, Convert.ToInt32(this.txt_Port.Text.Trim()));
//实例化回调
setCallBack = new SetTextCallBack(SetValue);
receiveCallBack = new ReceiveMsgCallBack(SetValue);
this.txt_Log.Invoke(setCallBack, "连接成功");
//开启一个新的线程不停的接收服务器发送消息的线程
threadReceive = new Thread(new ThreadStart(Receive));
//设置为后台线程
threadReceive.IsBackground = true;
threadReceive.Start();
}
catch (Exception ex)
{
MessageBox.Show("连接服务端出错:" + ex.ToString());
}
}
/// 接口服务器发送的消息
private void Receive()
{
try
{
while (true)
{
byte[] buffer = new byte[2048];
//实际接收到的字节数
int r = socketSend.Receive(buffer);
if (r == 0)
{
break;
}
else
{
//判断发送的数据的类型
if (buffer[0] == 0)//表示发送的是文字消息
{
string str = Encoding.Default.GetString(buffer, 1, 2);
int now_box = int.Parse(str);
if (now_box==8)
{
string strMsg1 = "装配完成,正在配送";
byte[] buffer2 = new byte[2048];
buffer2 = Encoding.Default.GetBytes(strMsg1);
int receive = socketSend.Send(buffer2);
txt_Log.AppendText(System.Environment.NewLine+"装配完成,正在配送\n" );
}
else
{
//string temp = "接收远程服务器:" + socketSend.RemoteEndPoint + "发送的消息:" + "已生产" + str + "箱防护服";
this.txt_Log.Invoke(receiveCallBack, "接收远程服务器:" + socketSend.RemoteEndPoint + "发送的消息:" + "正在生产" + str );
// this.txt_Log.AppendText( "已生产" + str + "箱防护服" + System.Environment.NewLine);
int j = now_box;
for (int i = 0; i < j; i++)
{
now_box--;
have_box++;
this.txt_Log.Invoke(receiveCallBack, "箱防护服" + System.Environment.NewLine+"装配防护服中......");
pictureMove();
this.txt_Log.Invoke(receiveCallBack, "已装配" + have_box + "箱" + "," + "剩余" + now_box + "箱");
}
//发送已装配数量给服务端
string strMsg = have_box.ToString();
byte[] buffer1 = new byte[2048];
buffer1 = Encoding.Default.GetBytes(strMsg);
int receive = socketSend.Send(buffer1);
}
}
//表示发送的是文件
if (buffer[0] == 1)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.InitialDirectory = @"";
sfd.Title = "请选择要保存的文件";
sfd.Filter = "所有文件|*.*";
sfd.ShowDialog(this);
string strPath = sfd.FileName;
using (FileStream fsWrite = new FileStream(strPath, FileMode.OpenOrCreate, FileAccess.Write))
{
fsWrite.Write(buffer, 1, r - 1);
}
MessageBox.Show("保存文件成功");
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("接收服务端发送的消息出错:" + ex.ToString());
}
}
private void SetValue(string strValue)
{
this.txt_Log.AppendText(strValue + "\r \n");
}
//客户端给服务器发送消息
//private void Send_Click(object sender, EventArgs e)
//{
// try
// {
// string strMsg = this.txt_Msg.Text.Trim();
// byte[] buffer = new byte[2048];
// buffer = Encoding.Default.GetBytes(strMsg);
// int receive = socketSend.Send(buffer);
// }
// catch (Exception ex)
// {
// MessageBox.Show("发送消息出错:" + ex.Message);
// }
//}
//断开连接
private void CloseConnect_Click(object sender, EventArgs e)
{
//关闭socket
socketSend.Close();
//终止线程
threadReceive.Abort();
}
//public partial class Form1 : Form
//{
// public static Form1 mainFrm;
// public Form1()
// {
// mainFrm = this;
// }
//}
public static void pictureMove()
{
// Form1.MainForm.pictureBox1.Visible = true;
//Form1.MainForm.pictureBox2.Show();
for (int i = 0; i < 6; i++)
{
DateTime orinowTime = DateTime.Now;//记录延时开始的系统当前时间
while (orinowTime.AddSeconds(0.6).CompareTo(DateTime.Now) >= 0)
{
}
Form1.MainForm.pictureBox1.Left += 80;
}
Form1.MainForm.pictureBox1.Visible = false;
Form1.MainForm.pictureBox1.Left =81;
Form1.MainForm.pictureBox1.Visible = true;
Form1.MainForm.pictureBox2.Show();
DateTime orinowTim = DateTime.Now;//记录延时开始的系统当前时间
while (orinowTim.AddSeconds(0.5).CompareTo(DateTime.Now) >= 0)
{
//Form1.MainForm.pictureBox2.Visible = true;
}
Form1.MainForm.pictureBox2.Hide();
//Form1.MainForm.pictureBox2.Visible = true;
//DateTime orinowTim = DateTime.Now;//记录延时开始的系统当前时间
//while (orinowTim.AddSeconds(1).CompareTo(DateTime.Now) >= 0)
//{
// //Form1.MainForm.pictureBox2.Visible = true;
//}
//Form1.MainForm.pictureBox2.Visible = false;
}
private void button1_Click_1(object sender, EventArgs e)
{
pictureMove();
}
}
}

