Frm_Client.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 EapFileUploadClient
  15. {
  16. public partial class Frm_Client : Form
  17. {
  18. Thread threadClient = null; // 创建用于接收服务端消息的 线程;
  19. Socket sockClient = null;
  20. delegate void myInvoke(string msg);//定义一个自己的委托
  21. public Frm_Client()
  22. {
  23. InitializeComponent();
  24. }
  25. private void btnConnect_Click(object sender, EventArgs e)
  26. {
  27. if (!ShutDownSocket(sockClient))
  28. {
  29. ShowMsg("初始化socket发生错误,请重试!");
  30. return;
  31. }
  32. IPAddress ip = IPAddress.Parse(txtIp.Text.Trim());
  33. IPEndPoint endPoint = new IPEndPoint(ip, int.Parse(txtPort.Text.Trim()));
  34. sockClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  35. try
  36. {
  37. ShowMsg("与服务器连接中……");
  38. sockClient.Connect(endPoint);
  39. }
  40. catch (SocketException se)
  41. {
  42. //MessageBox.Show(se.Message);
  43. ShowMsg(se.Message);
  44. return;
  45. //this.Close();
  46. }
  47. ShowMsg("与服务器连接成功!!!");
  48. threadClient = new Thread(RecMsg);
  49. threadClient.IsBackground = true;
  50. threadClient.Start();
  51. }
  52. string fileSavePath = "D:\\SocketClientFile\\";
  53. void RecMsg()
  54. {
  55. myInvoke myinvoke = new myInvoke(ShowMsg);
  56. while (true)
  57. {
  58. // 定义一个2M的缓存区;
  59. byte[] arrMsgRec = new byte[1024 * 1024 * 2];
  60. // 将接受到的数据存入到输入 arrMsgRec中;
  61. int length = -1;
  62. try
  63. {
  64. length = sockClient.Receive(arrMsgRec); // 接收数据,并返回数据的长度;
  65. }
  66. catch (SocketException se)
  67. {
  68. this.Invoke(myinvoke, "异常;" + se.Message);
  69. return;
  70. }
  71. catch (Exception e)
  72. {
  73. this.Invoke(myinvoke, "异常;" + e.Message);
  74. return;
  75. }
  76. if (arrMsgRec[0] == 0) // 表示接收到的是消息数据;
  77. {
  78. string strMsg = System.Text.Encoding.UTF8.GetString(arrMsgRec, 1, length - 1);// 将接受到的字节数据转化成字符串;
  79. if (!strMsg.Contains("-->"))
  80. {
  81. fileSavePath = fileSavePath + strMsg.Split(':')[1].Replace("\r\n", "");
  82. }
  83. this.Invoke(myinvoke, strMsg);
  84. }
  85. if (arrMsgRec[0] == 1) // 表示接收到的是文件数据;
  86. {
  87. try
  88. {
  89. // 创建文件流,然后根据路径创建文件;
  90. using (FileStream fs = new FileStream(fileSavePath, FileMode.Create))
  91. {
  92. fs.Write(arrMsgRec, 1, length - 1);
  93. this.Invoke(myinvoke, "文件保存成功:" + fileSavePath);
  94. }
  95. }
  96. catch (Exception aaa)
  97. {
  98. this.Invoke(new Action(() => { MessageBox.Show(aaa.Message); }));
  99. }
  100. }
  101. }
  102. }
  103. void ShowMsg(string str)
  104. {
  105. txtMsg.Items.Add(str + "\r\n");
  106. }
  107. private void btnSendMsg_Click(object sender, EventArgs e)
  108. {
  109. string strMsg = txtName.Text.Trim() + "\r\n" + " -->" + txtSendMsg.Text.Trim() + "\r\n";
  110. byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(strMsg);
  111. byte[] arrSendMsg = new byte[arrMsg.Length + 1];//多一位,只是为了存放一个标识位.
  112. arrSendMsg[0] = 0; // 用来表示发送的是消息数据
  113. Buffer.BlockCopy(arrMsg, 0, arrSendMsg, 1, arrMsg.Length);
  114. try
  115. {
  116. sockClient.Send(arrSendMsg); // 发送消息;
  117. }
  118. catch (Exception ex)
  119. {
  120. ShowMsg(ex.Message);
  121. }
  122. ShowMsg(strMsg);
  123. txtSendMsg.Clear();
  124. }
  125. private void btnSelectFile_Click(object sender, EventArgs e)
  126. {
  127. OpenFileDialog ofd = new OpenFileDialog();
  128. ofd.InitialDirectory = "D:\\";
  129. if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  130. {
  131. txtSelectFile.Text = ofd.FileName;
  132. }
  133. }
  134. private void btnSendFile_Click(object sender, EventArgs e)
  135. {
  136. if (string.IsNullOrEmpty(txtSelectFile.Text))
  137. {
  138. MessageBox.Show("请选择要发送的文件!!!");
  139. }
  140. else
  141. {
  142. // 用文件流打开用户要发送的文件;
  143. using (FileStream fs = new FileStream(txtSelectFile.Text, FileMode.Open))
  144. {
  145. //在发送文件以前先给好友发送这个文件的名字+扩展名,方便后面的保存操作;
  146. string fileName = System.IO.Path.GetFileName(txtSelectFile.Text);
  147. string fileExtension = System.IO.Path.GetExtension(txtSelectFile.Text);
  148. string strMsg = "我给你发送的文件为:" + fileName + "\r\n";
  149. byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(strMsg);
  150. byte[] arrSendMsg = new byte[arrMsg.Length + 1];
  151. arrSendMsg[0] = 0; // 用来表示发送的是消息数据
  152. Buffer.BlockCopy(arrMsg, 0, arrSendMsg, 1, arrMsg.Length);
  153. sockClient.Send(arrSendMsg); // 发送消息;
  154. byte[] arrFile = new byte[1024 * 1024 * 2];
  155. int length = fs.Read(arrFile, 0, arrFile.Length); // 将文件中的数据读到arrFile数组中;
  156. byte[] arrFileSend = new byte[length + 1];
  157. arrFileSend[0] = 1; // 用来表示发送的是文件数据;
  158. Buffer.BlockCopy(arrFile, 0, arrFileSend, 1, length);
  159. // 还有一个 CopyTo的方法,但是在这里不适合; 当然还可以用for循环自己转化;
  160. sockClient.Send(arrFileSend);// 发送数据到服务端;
  161. txtSelectFile.Clear();
  162. }
  163. }
  164. }
  165. //关闭连接
  166. public bool ShutDownSocket(Socket sk)
  167. {
  168. try
  169. {
  170. if (sk == null)
  171. {
  172. return true;
  173. }
  174. else
  175. {
  176. sk.Close();//关闭Socket
  177. sk = null;
  178. return true;
  179. }
  180. }
  181. catch (Exception ex)
  182. {
  183. sk = null;
  184. return false;
  185. }
  186. }
  187. }
  188. }