Frm_Server.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace EapFileUploadServer
  15. {
  16. public partial class Frm_Server : Form
  17. {
  18. Thread threadWatch = null; // 负责监听客户端连接请求的 线程;
  19. Socket socketWatch = null;
  20. delegate void myInvoke(string msg);//定义一个委托
  21. Dictionary<string, Socket> dict = new Dictionary<string, Socket>();
  22. Dictionary<string, Thread> dictThread = new Dictionary<string, Thread>();
  23. public Frm_Server()
  24. {
  25. InitializeComponent();
  26. //System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
  27. }
  28. private void btnBeginListen_Click(object sender, EventArgs e)
  29. {
  30. // 创建负责监听的套接字,注意其中的参数;
  31. socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  32. // 获得文本框中的IP对象;
  33. IPAddress address = IPAddress.Parse(txtIp.Text.Trim());
  34. // 创建包含ip和端口号的网络节点对象;
  35. IPEndPoint endPoint = new IPEndPoint(address, int.Parse(txtPort.Text.Trim()));
  36. try
  37. {
  38. // 将负责监听的套接字绑定到唯一的ip和端口上;
  39. socketWatch.Bind(endPoint);
  40. }
  41. catch (SocketException se)
  42. {
  43. MessageBox.Show("异常:" + se.Message);
  44. return;
  45. }
  46. // 设置监听队列的长度;
  47. socketWatch.Listen(10);
  48. // 创建负责监听的线程;
  49. threadWatch = new Thread(WatchConnecting);
  50. threadWatch.IsBackground = true;
  51. threadWatch.Start();
  52. ShowMsg("服务器启动监听成功!");
  53. }
  54. /// <summary>
  55. /// 监听客户端请求的方法;
  56. /// </summary>
  57. void WatchConnecting()
  58. {
  59. myInvoke myinvoke = new myInvoke(ShowMsg);
  60. while (true) // 持续不断的监听客户端的连接请求;
  61. {
  62. // 开始监听客户端连接请求,Accept方法会阻断当前的线程;
  63. Socket sokConnection = socketWatch.Accept(); // 一旦监听到一个客户端的请求,就返回一个与该客户端通信的 套接字;
  64. // 向列表控件中添加客户端的IP信息;
  65. lbOnline.Invoke(new Action(() => { lbOnline.Items.Add(sokConnection.RemoteEndPoint.ToString()); }));
  66. // 将与客户端连接的 套接字 对象添加到集合中;
  67. dict.Add(sokConnection.RemoteEndPoint.ToString(), sokConnection);
  68. //ShowMsg("客户端连接成功!");
  69. this.Invoke(myinvoke, "客户端连接成功!");
  70. Thread thr = new Thread(RecMsg);
  71. thr.IsBackground = true;
  72. thr.Start(sokConnection);
  73. dictThread.Add(sokConnection.RemoteEndPoint.ToString(), thr); // 将新建的线程 添加 到线程的集合中去。
  74. }
  75. }
  76. //string fileSavePath = Directory.GetCurrentDirectory() + "\\SocketServerFile\\";
  77. string fileSavePath = "D:\\SocketServerFile\\";
  78. void RecMsg(object sokConnectionparn)
  79. {
  80. myInvoke myinvoke = new myInvoke(ShowMsg);
  81. Socket sokClient = sokConnectionparn as Socket;
  82. while (true)
  83. {
  84. // 定义一个2M的缓存区;
  85. byte[] arrMsgRec = new byte[1024 * 1024 * 2];
  86. // 将接受到的数据存入到输入 arrMsgRec中;
  87. int length = -1;
  88. try
  89. {
  90. length = sokClient.Receive(arrMsgRec); // 接收数据,并返回数据的长度;
  91. //ShowMsg("返回值:" + length);
  92. }
  93. catch (SocketException se)
  94. {
  95. this.Invoke(myinvoke, "异常:" + se.Message);
  96. // 从 通信套接字 集合中删除被中断连接的通信套接字;
  97. dict.Remove(sokClient.RemoteEndPoint.ToString());
  98. // 从通信线程集合中删除被中断连接的通信线程对象;
  99. dictThread.Remove(sokClient.RemoteEndPoint.ToString());
  100. // 从列表中移除被中断的连接IP -- 委托主线程更新
  101. lbOnline.Invoke(new Action(() => { lbOnline.Items.Remove(sokClient.RemoteEndPoint.ToString()); }));
  102. sokClient.Close();
  103. break;
  104. }
  105. catch (Exception e)
  106. {
  107. this.Invoke(myinvoke, "异常:" + e.Message);
  108. // 从 通信套接字 集合中删除被中断连接的通信套接字;
  109. dict.Remove(sokClient.RemoteEndPoint.ToString());
  110. // 从通信线程集合中删除被中断连接的通信线程对象;
  111. dictThread.Remove(sokClient.RemoteEndPoint.ToString());
  112. // 从列表中移除被中断的连接IP
  113. lbOnline.Invoke(new Action(() => { lbOnline.Items.Remove(sokClient.RemoteEndPoint.ToString()); }));
  114. sokClient.Close();
  115. break;
  116. }
  117. if (arrMsgRec[0] == 0) // 表示接收到的是数据;
  118. {
  119. string strMsg = System.Text.Encoding.UTF8.GetString(arrMsgRec, 1, length - 1);// 将接受到的字节数据转化成字符串;
  120. if (!strMsg.Contains("-->"))
  121. {
  122. fileSavePath = fileSavePath + strMsg.Split(':')[1].Replace("\r\n", "");
  123. }
  124. this.Invoke(myinvoke, strMsg);
  125. //sokClient.Close();
  126. }
  127. if (arrMsgRec[0] == 1) // 表示接收到的是文件;
  128. {
  129. using (FileStream fs = new FileStream(fileSavePath, FileMode.Create))
  130. {
  131. fs.Write(arrMsgRec, 1, length - 1);
  132. this.Invoke(myinvoke, "文件保存成功:" + fileSavePath);
  133. }
  134. //sokClient.Close();
  135. }
  136. }
  137. }
  138. void ShowMsg(string str)
  139. {
  140. txtMsg.Items.Add(str + "\r\n");
  141. }
  142. private void btnSendToAll_Click(object sender, EventArgs e)
  143. {
  144. string strMsg = "服务器" + "\r\n" + " -->" + txtMsgSend.Text.Trim() + "\r\n";
  145. byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(strMsg); // 将要发送的字符串转换成Utf-8字节数组;
  146. byte[] arrSendMsg = new byte[arrMsg.Length + 1]; // 上次写的时候把这一段给弄掉了,实在是抱歉哈~ 用来标识发送是数据而不是文件,如果没有这一段的客户端就接收不到消息了~~~
  147. arrSendMsg[0] = 0; // 表示发送的是消息数据
  148. Buffer.BlockCopy(arrMsg, 0, arrSendMsg, 1, arrMsg.Length);
  149. foreach (Socket s in dict.Values)
  150. {
  151. s.Send(arrSendMsg);
  152. //s.Close();
  153. }
  154. ShowMsg(strMsg);
  155. txtMsgSend.Clear();
  156. ShowMsg(" 群发完毕~~~");
  157. }
  158. private void btnSend_Click(object sender, EventArgs e)
  159. {
  160. string strMsg = "服务器" + "\r\n" + " -->" + txtMsgSend.Text.Trim() + "\r\n";
  161. byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(strMsg); // 将要发送的字符串转换成Utf-8字节数组;
  162. byte[] arrSendMsg = new byte[arrMsg.Length + 1];
  163. arrSendMsg[0] = 0; // 表示发送的是消息数据
  164. Buffer.BlockCopy(arrMsg, 0, arrSendMsg, 1, arrMsg.Length);
  165. string strKey = "";
  166. strKey = lbOnline.Text.Trim();
  167. if (string.IsNullOrEmpty(strKey)) // 判断是不是选择了发送的对象;
  168. {
  169. MessageBox.Show("请选择你要发送的好友!!!");
  170. }
  171. else
  172. {
  173. dict[strKey].Send(arrSendMsg);// 解决了 sokConnection是局部变量,不能再本函数中引用的问题;
  174. ShowMsg(strMsg);
  175. txtMsgSend.Clear();
  176. //dict[strKey].Close();
  177. }
  178. }
  179. private void btnSelectFile_Click(object sender, EventArgs e)
  180. {
  181. OpenFileDialog ofd = new OpenFileDialog();
  182. ofd.InitialDirectory = "D:\\";
  183. if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  184. {
  185. txtSelectFile.Text = ofd.FileName;
  186. }
  187. }
  188. private void btnSendFile_Click(object sender, EventArgs e)
  189. {
  190. if (string.IsNullOrEmpty(txtSelectFile.Text))
  191. {
  192. MessageBox.Show("请选择你要发送的文件!!!");
  193. }
  194. else
  195. {
  196. // 用文件流打开用户要发送的文件;
  197. using (FileStream fs = new FileStream(txtSelectFile.Text, FileMode.Open))
  198. {
  199. string fileName = System.IO.Path.GetFileName(txtSelectFile.Text);
  200. string fileExtension = System.IO.Path.GetExtension(txtSelectFile.Text);
  201. string strMsg = "我给你发送的文件为:" + fileName + fileExtension + "\r\n";
  202. byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(strMsg); // 将要发送的字符串转换成Utf-8字节数组;
  203. byte[] arrSendMsg = new byte[arrMsg.Length + 1];
  204. arrSendMsg[0] = 0; // 表示发送的是消息数据
  205. Buffer.BlockCopy(arrMsg, 0, arrSendMsg, 1, arrMsg.Length);
  206. //bool fff = true;
  207. string strKey = "";
  208. strKey = lbOnline.Text.Trim();
  209. if (string.IsNullOrEmpty(strKey)) // 判断是不是选择了发送的对象;
  210. {
  211. MessageBox.Show("请选择你要发送的好友!!!");
  212. }
  213. else
  214. {
  215. dict[strKey].Send(arrSendMsg);// 解决了 sokConnection是局部变量,不能再本函数中引用的问题;
  216. byte[] arrFile = new byte[1024 * 1024 * 2];
  217. int length = fs.Read(arrFile, 0, arrFile.Length); // 将文件中的数据读到arrFile数组中;
  218. byte[] arrFileSend = new byte[length + 1];
  219. arrFileSend[0] = 1; // 用来表示发送的是文件数据;
  220. Buffer.BlockCopy(arrFile, 0, arrFileSend, 1, length);
  221. // 还有一个 CopyTo的方法,但是在这里不适合; 当然还可以用for循环自己转化;
  222. // sockClient.Send(arrFileSend);// 发送数据到服务端;
  223. dict[strKey].Send(arrFileSend);// 解决了 sokConnection是局部变量,不能再本函数中引用的问题;
  224. txtSelectFile.Clear();
  225. //dict[strKey].Close();
  226. }
  227. }
  228. }
  229. txtSelectFile.Clear();
  230. }
  231. }
  232. }