using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
public
delegate void DGShowMsg(string strMsg);
namespace
Server
{
public
partial
class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
TextBox.CheckForIllegalCrossThreadCalls = false;
txtIP.Text = Dns.
GetHostByName
(Dns.GetHostName()).AddressList[0].ToString();
}
[DllImport(
"kernel32"
)]
public
static
extern void GetSystemTime(ref SYSTEMTIME_INFO stinfo);
[StructLayout(LayoutKind.Sequential)]
public
struct SYSTEMTIME_INFO
{
public
ushort wYear;
public
ushort wMonth;
public
ushort wDayOfWeek;
public
ushort wDay;
public
ushort wHour;
public
ushort wMinute;
public
ushort wSecond;
public
ushort wMilliseconds;
}
Socket sokWatch = null;
Thread threadWatch = null;
private
void btnStartListen_Click(object sender, EventArgs e)
{
sokWatch =
new
Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress address = IPAddress.Parse(txtIP.Text.Trim());
IPEndPoint endpoint =
new
IPEndPoint(address, int.Parse(comboBox1.Text.Trim()));
sokWatch.Bind(endpoint);
sokWatch.Listen(20);
threadWatch =
new
Thread(StartWatch);
threadWatch.IsBackground = true;
threadWatch.Start();
label4.Text =
"启动服务器成功......"
;
}
Dictionary<string, ConnectionClient> dictConn =
new
Dictionary<string, ConnectionClient>();
bool isWatch = true;
#region 1.被线程调用 监听连接端口
void StartWatch()
{
string recode;
while
(isWatch)
{
Socket sokMsg = sokWatch.Accept();
ConnectionClient connection =
new
ConnectionClient(sokMsg, ShowMsg, RemoveClientConnection);
dictConn.Add(sokMsg.RemoteEndPoint.ToString(), connection);
cboClient.Items.Add(sokMsg.RemoteEndPoint.ToString());
MessageBox.Show(
"有一个客户端新添加!"
);
recode = sokMsg.RemoteEndPoint.ToString();
SYSTEMTIME_INFO StInfo; StInfo =
new
SYSTEMTIME_INFO();
GetSystemTime(ref StInfo);
recode +=
"子计算机在"
+StInfo.wYear.ToString() +
"年"
+ StInfo.wMonth.ToString() +
"月"
+ StInfo.wDay.ToString() +
"日"
;
recode += (StInfo.wHour + 8).ToString() +
"点"
+ StInfo.wMinute.ToString() +
"分"
+ StInfo.wSecond.ToString() +
"秒"
+
"连接服务"
;
StreamWriter m_sw =
new
StreamWriter(System.Windows.Forms.Application.StartupPath + @
"\file.DAT"
, true);
m_sw.WriteLine(recode);
m_sw.WriteLine(
"------------------------------------------------------------------"
);
m_sw.Close();
dictConn[sokMsg.RemoteEndPoint.ToString()].SendTrue();
}
}
#endregion
#region 发送消息 到指定的客户端 -btnSend_Click
private
void btnSend_Click(object sender, EventArgs e)
{
string time;
string connectionSokKey = cboClient.Text;
if
(!string.IsNullOrEmpty(connectionSokKey))
{
dictConn[connectionSokKey].Send(txtInput.Text.Trim());
SYSTEMTIME_INFO StInfo; StInfo =
new
SYSTEMTIME_INFO();
GetSystemTime(ref StInfo);
time = StInfo.wYear.ToString() +
"/"
+ StInfo.wMonth.ToString() +
"/"
+ StInfo.wDay.ToString() +
" "
;
time += (StInfo.wHour + 8).ToString() +
":"
+ StInfo.wMinute.ToString();
richTextBox1.AppendText(time +
"rn"
);
richTextBox1.AppendText(
"对"
+ cboClient.Text +
"说:"
+ txtInput.Text.Trim() +
"rn"
);
txtInput.Text =
""
;
}
else
{
MessageBox.Show(
"请选择要发送的子计算机~~"
);
}
}
#endregion
private
void btnShack_Click(object sender, EventArgs e)
{
string connectionSokKey = cboClient.Text;
if
(!string.IsNullOrEmpty(connectionSokKey))
{
dictConn[connectionSokKey].SendShake();
}
else
{
MessageBox.Show(
"请选择要发送的子计算机~~"
);
}
}
private
void btnShackAll_Click(object sender, EventArgs e)
{
foreach
(ConnectionClient conn in dictConn.Values)
{
conn.SendShake();
}
}
#region 2 移除与指定客户端的连接 +void RemoveClientConnection(string key)
public
void RemoveClientConnection(string key)
{
if
(dictConn.ContainsKey(key))
{
dictConn.Remove(key);
MessageBox.Show(key +
"断开连接"
);
cboClient.Items.Remove(key);
}
}
#endregion
private
void btnChooseFile_Click(object sender, EventArgs e)
{
OpenFileDialog ofd =
new
OpenFileDialog();
if
(ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtFilePath.Text = ofd.FileName;
}
}
private
void btnSendFile_Click(object sender, EventArgs e)
{
string key = cboClient.Text;
if
(!string.IsNullOrEmpty(key))
{
dictConn[key].SendFile(txtFilePath.Text.Trim());
}
else
{
MessageBox.Show(
"请选择要发送的子计算机~"
);
}
}
#region 向文本框显示消息 -void ShowMsg(string msgStr)
public
void ShowMsg(string msgStr)
{
txtShow1.AppendText(msgStr +
"rn"
);
}
#endregion
private
void btnSendMsgAll_Click(object sender, EventArgs e)
{
string time;
foreach
(ConnectionClient conn in dictConn.Values)
{
conn.Send(txtInput.Text.Trim());
}
SYSTEMTIME_INFO StInfo; StInfo =
new
SYSTEMTIME_INFO();
GetSystemTime(ref StInfo);
time = StInfo.wYear.ToString() +
"/"
+ StInfo.wMonth.ToString() +
"/"
+ StInfo.wDay.ToString() +
" "
;
time += (StInfo.wHour + 8).ToString() +
":"
+ StInfo.wMinute.ToString();
richTextBox1.AppendText(time +
"rn"
);
richTextBox1.AppendText(
"群发消息:"
+ txtInput.Text.Trim() +
"rn"
);
txtInput.Text =
""
;
}
private
void button1_Click(object sender, EventArgs e)
{
foreach
(ConnectionClient conn in dictConn.Values)
{
conn.SendFile(txtFilePath.Text.Trim());
}
}
private
void button2_Click(object sender, EventArgs e)
{
string connectionSokKey = cboClient.Text;
if
(!string.IsNullOrEmpty(connectionSokKey))
{
dictConn[connectionSokKey].guanji();
}
else
{
MessageBox.Show(
"请选择要发送的子计算机~~"
);
}
}
private
void button3_Click(object sender, EventArgs e)
{
string connectionSokKey = cboClient.Text;
if
(!string.IsNullOrEmpty(connectionSokKey))
{
dictConn[connectionSokKey].chongqi();
}
else
{
MessageBox.Show(
"请选择要发送的子计算机~~"
);
}
}
private
void button4_Click(object sender, EventArgs e)
{
string connectionSokKey = cboClient.Text;
if
(!string.IsNullOrEmpty(connectionSokKey))
{
dictConn[connectionSokKey].zhuxiao();
}
else
{
MessageBox.Show(
"请选择要发送的子计算机~~"
);
}
}
private
void button5_Click(object sender, EventArgs e)
{
string connectionSokKey = cboClient.Text;
if
(!string.IsNullOrEmpty(connectionSokKey))
{
dictConn[connectionSokKey].jieping();
}
else
{
MessageBox.Show(
"请选择要发送的子计算机~~"
);
}
}
}
public
class
ConnectionClient
{
Socket sokMsg;
DGShowMsg dgShowMsg;
DGShowMsg dgRemoveConnection;
Thread threadMsg;
#region 构造函数
public
ConnectionClient(Socket sokMsg, DGShowMsg dgShowMsg, DGShowMsg dgRemoveConnection)
{
this.sokMsg = sokMsg;
this.dgShowMsg = dgShowMsg;
this.dgRemoveConnection = dgRemoveConnection;
this.threadMsg =
new
Thread(RecMsg);
this.threadMsg.IsBackground = true;
this.threadMsg.Start();
}
#endregion
bool isRec = true;
#region 02负责监听客户端发送来的消息
void RecMsg()
{
while
(isRec)
{
try
{
byte[] arrMsg =
new
byte[1024 * 1024 * 1];
int length = sokMsg.Receive(arrMsg);
if
(arrMsg[0] == 1)
{
SaveFileDialog sfd =
new
SaveFileDialog();
sfd.Filter =
"文本文件(.txt)|*.txt|所有文件(*.*)|*.*"
;
if
(sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
using (FileStream fs =
new
FileStream(sfd.FileName, FileMode.OpenOrCreate))
{
fs.Write(arrMsg, 1, length - 1);
MessageBox.Show(
"文件保存成功!"
);
}
}
}
else
{
string strMsg = sokMsg.RemoteEndPoint.ToString()+
"说:"
+
"rn"
+System.Text.Encoding.UTF8.GetString(arrMsg, 0, length);
dgShowMsg(strMsg);
}
}
catch
(Exception ex)
{
isRec = false;
dgRemoveConnection(sokMsg.RemoteEndPoint.ToString());
}
}
}
#endregion
#region 03向客户端发送消息
public
void Send(string strMsg)
{
byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(strMsg);
byte[] arrMsgFinal =
new
byte[arrMsg.Length + 1];
arrMsgFinal[0] = 0;
arrMsg.CopyTo(arrMsgFinal, 1);
sokMsg.Send(arrMsgFinal);
}
#endregion
#region 04向客户端发送文件数据 +void SendFile(string strPath)
public
void SendFile(string strPath)
{
using (FileStream fs =
new
FileStream(strPath, FileMode.OpenOrCreate))
{
byte[] arrFile =
new
byte[1024 * 1024 * 2];
int length = fs.Read(arrFile, 0, arrFile.Length);
byte[] arrFileFina =
new
byte[length + 1];
arrFileFina[0] = 1;
Buffer.BlockCopy(arrFile, 0, arrFileFina, 1, length);
sokMsg.Send(arrFileFina);
}
}
#endregion
#region 05向客户端发送闪屏
public
void SendShake()
{
byte[] arrMsgFinal =
new
byte[1];
arrMsgFinal[0] = 2;
sokMsg.Send(arrMsgFinal);
}
#endregion
#region 06关闭与客户端连接
public
void CloseConnection()
{
isRec = false;
}
#endregion
#region 07向客户端发送连接成功提示
public
void SendTrue()
{
byte[] arrMsgFinal =
new
byte[1];
arrMsgFinal[0] = 3;
sokMsg.Send(arrMsgFinal);
}
#endregion
#region 08向客户端发送关机命令
public
void guanji()
{
byte[] arrMsgFinal =
new
byte[1];
arrMsgFinal[0] = 4;
sokMsg.Send(arrMsgFinal);
}
#endregion
#region 09向客户端发送重启命令
public
void chongqi()
{
byte[] arrMsgFinal =
new
byte[1];
arrMsgFinal[0] = 5;
sokMsg.Send(arrMsgFinal);
}
#endregion
#region 10向客户端发送待机命令
public
void zhuxiao()
{
byte[] arrMsgFinal =
new
byte[1];
arrMsgFinal[0] = 6;
sokMsg.Send(arrMsgFinal);
}
#endregion
#region 11向客户端发送截屏命令
public
void jieping()
{
byte[] arrMsgFinal =
new
byte[1];
arrMsgFinal[0] = 7;
sokMsg.Send(arrMsgFinal);
}
#endregion
}
}