C# TCP 基于socket实现多个客户端与服务端 数据与文件的传输


原文地址:http://blog.csdn.net/chwei_cson/article/details/7737766

该实例是在原博主基础上进行修改,选择保存路径改成在程序路径下的RecFile文件下,并且把子线程修改控件值的任务交给主线程修改(用委托).

服务端:

 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 TCPServer  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() + "\\RecFile\\";  77 void RecMsg(object sokConnectionparn)  78 {  79 myInvoke myinvoke = new myInvoke(ShowMsg);  80 Socket sokClient = sokConnectionparn as Socket;  81 while (true)  82 {  83 // 定义一个2M的缓存区;
 84 byte[] arrMsgRec = new byte[1024 * 1024 * 2];  85 // 将接受到的数据存入到输入 arrMsgRec中;
 86 int length = -1;  87 try
 88 {  89 length = sokClient.Receive(arrMsgRec); // 接收数据,并返回数据的长度;
 90 }  91 catch (SocketException se)  92 {  93 this.Invoke(myinvoke, "异常:" + se.Message);  94 // 从 通信套接字 集合中删除被中断连接的通信套接字;
 95 dict.Remove(sokClient.RemoteEndPoint.ToString());  96 // 从通信线程集合中删除被中断连接的通信线程对象;
 97 dictThread.Remove(sokClient.RemoteEndPoint.ToString());  98 // 从列表中移除被中断的连接IP -- 委托主线程更新
 99 lbOnline.Invoke(new Action(() => { lbOnline.Items.Remove(sokClient.RemoteEndPoint.ToString()); })); 100 break; 101 } 102 catch (Exception e) 103 { 104 this.Invoke(myinvoke, "异常:" + e.Message); 105 // 从 通信套接字 集合中删除被中断连接的通信套接字;
106 dict.Remove(sokClient.RemoteEndPoint.ToString()); 107 // 从通信线程集合中删除被中断连接的通信线程对象;
108 dictThread.Remove(sokClient.RemoteEndPoint.ToString()); 109 // 从列表中移除被中断的连接IP
110 lbOnline.Invoke(new Action(() => { lbOnline.Items.Remove(sokClient.RemoteEndPoint.ToString()); })); 111 break; 112 } 113 if (arrMsgRec[0] == 0) // 表示接收到的是数据;
114 { 115 string strMsg = System.Text.Encoding.UTF8.GetString(arrMsgRec, 1, length - 1);// 将接受到的字节数据转化成字符串;
116 if (!strMsg.Contains("-->")) 117 { 118 fileSavePath = fileSavePath + strMsg.Split(':')[1].Replace("\r\n", ""); 119 } 120 this.Invoke(myinvoke, strMsg); 121 } 122 if (arrMsgRec[0] == 1) // 表示接收到的是文件;
123 { 124 using (FileStream fs = new FileStream(fileSavePath, FileMode.Create)) 125 { 126 fs.Write(arrMsgRec, 1, length - 1); 127 this.Invoke(myinvoke, "文件保存成功:" + fileSavePath); 128 } 129 } 130 } 131 } 132 void ShowMsg(string str) 133 { 134 txtMsg.Items.Add(str + "\r\n"); 135 } 136 private void btnSendToAll_Click(object sender, EventArgs e) 137 { 138 string strMsg = "服务器" + "\r\n" + " -->" + txtMsgSend.Text.Trim() + "\r\n"; 139 byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(strMsg); // 将要发送的字符串转换成Utf-8字节数组;
140 byte[] arrSendMsg = new byte[arrMsg.Length + 1]; // 上次写的时候把这一段给弄掉了,实在是抱歉哈~ 用来标识发送是数据而不是文件,如果没有这一段的客户端就接收不到消息了~~~
141 arrSendMsg[0] = 0; // 表示发送的是消息数据
142 Buffer.BlockCopy(arrMsg, 0, arrSendMsg, 1, arrMsg.Length); 143  
144 
145 foreach (Socket s in dict.Values) 146 { 147 s.Send(arrMsg); 148 } 149 ShowMsg(strMsg); 150 txtMsgSend.Clear(); 151 ShowMsg(" 群发完毕~~~"); 152 } 153 private void btnSend_Click(object sender, EventArgs e) 154 { 155 string strMsg = "服务器" + "\r\n" + " -->" + txtMsgSend.Text.Trim() + "\r\n"; 156 byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(strMsg); // 将要发送的字符串转换成Utf-8字节数组;
157 byte[] arrSendMsg = new byte[arrMsg.Length + 1]; 158 arrSendMsg[0] = 0; // 表示发送的是消息数据
159 Buffer.BlockCopy(arrMsg, 0, arrSendMsg, 1, arrMsg.Length); 160 string strKey = ""; 161 strKey = lbOnline.Text.Trim(); 162 if (string.IsNullOrEmpty(strKey)) // 判断是不是选择了发送的对象;
163 { 164 MessageBox.Show("请选择你要发送的好友!!!"); 165 } 166 else
167 { 168 dict[strKey].Send(arrSendMsg);// 解决了 sokConnection是局部变量,不能再本函数中引用的问题;
169 ShowMsg(strMsg); 170 txtMsgSend.Clear(); 171 } 172 } 173 private void btnSelectFile_Click(object sender, EventArgs e) 174 { 175 OpenFileDialog ofd = new OpenFileDialog(); 176 ofd.InitialDirectory = "D:\\"; 177 if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) 178 { 179 txtSelectFile.Text = ofd.FileName; 180 } 181 } 182 private void btnSendFile_Click(object sender, EventArgs e) 183 { 184 if (string.IsNullOrEmpty(txtSelectFile.Text)) 185 { 186 MessageBox.Show("请选择你要发送的文件!!!"); 187 } 188 else
189 { 190 // 用文件流打开用户要发送的文件;
191 using (FileStream fs = new FileStream(txtSelectFile.Text, FileMode.Open)) 192 { 193 string fileName = System.IO.Path.GetFileName(txtSelectFile.Text); 194 string fileExtension = System.IO.Path.GetExtension(txtSelectFile.Text); 195 string strMsg = "我给你发送的文件为:" + fileName + fileExtension + "\r\n"; 196 byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(strMsg); // 将要发送的字符串转换成Utf-8字节数组;
197 byte[] arrSendMsg = new byte[arrMsg.Length + 1]; 198 arrSendMsg[0] = 0; // 表示发送的是消息数据
199 Buffer.BlockCopy(arrMsg, 0, arrSendMsg, 1, arrMsg.Length); 200 //bool fff = true;
201 string strKey = ""; 202 strKey = lbOnline.Text.Trim(); 203 if (string.IsNullOrEmpty(strKey)) // 判断是不是选择了发送的对象;
204 { 205 MessageBox.Show("请选择你要发送的好友!!!"); 206 } 207 else
208 { 209 dict[strKey].Send(arrSendMsg);// 解决了 sokConnection是局部变量,不能再本函数中引用的问题;
210 byte[] arrFile = new byte[1024 * 1024 * 2]; 211 int length = fs.Read(arrFile, 0, arrFile.Length); // 将文件中的数据读到arrFile数组中;
212 byte[] arrFileSend = new byte[length + 1]; 213 arrFileSend[0] = 1; // 用来表示发送的是文件数据;
214 Buffer.BlockCopy(arrFile, 0, arrFileSend, 1, length); 215 // 还有一个 CopyTo的方法,但是在这里不适合; 当然还可以用for循环自己转化; 216 // sockClient.Send(arrFileSend);// 发送数据到服务端;
217 dict[strKey].Send(arrFileSend);// 解决了 sokConnection是局部变量,不能再本函数中引用的问题;
218 txtSelectFile.Clear(); 219 } 220 } 221 } 222 txtSelectFile.Clear(); 223 } 224 } 225 }

 

客户端:

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

     


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM