关于客户端和服务端聊天室的思路和代码(主要参考传智播客讲课)
2012-04-23 20:58
495 查看
服务端的代码:
客户端代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Net; using System.Threading; using System.IO; namespace ItcastSocketTest { /// <summary> /// 基本思路一、关于套接字二、关于线程 /// 1.1、客户端要本地的ip和一个socket相关联--通过socketWatch.Bind(endpoint);完成 /// 1.2、完成关联之后就开始用用“第一线程watchTread” 监听 /// 1.3、监听到客户端之后,服务端会再创建一个负责与客户端通信的socket----socketConnetion /// 2.1、客户端与服务端的通信都通过socketConnetion来实现 包括send receive /// 2.2、当服务当接收信息时,要创建一个新的“第二线程recieveTread”,防止冲突 /// 为什么发送不用创建线程 /// 因为发送消息的时间服务器不用对自己监听 当客户端发送消息时, 服务端要对其监听 /// </summary> public partial class Form1 : Form { public Form1() { InitializeComponent(); TextBox.CheckForIllegalCrossThreadCalls = false; } Thread watchTread=null;//创建一个线程,防止线程“死等”事件的发生 Thread recieveTread = null;//创建一个接收线程, Socket socketWatch=null;//创建一个套接字用来获取服务器的ip和端口 Socket socketConnetion = null;//链接成功后返回一个套接字 Dictionary<string,Socket> dtSocket=new Dictionary<string,Socket>();//创建一个字段,用来实现服务端向多个客户端发送信息 Dictionary<string, Thread> dtThread = new Dictionary<string, Thread>();//创建一个字段,用来实现服务端向多个客户端发送信息 /// <summary> /// 启动服务端 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnBeginServer_Click(object sender, EventArgs e) { //创建服务器线程 负责监听的套接字 socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress address = IPAddress.Parse(txtIP.Text.ToString()); IPEndPoint endpoint = new IPEndPoint(address, int.Parse(txtPort.Text.Trim())); //========================================================= socketWatch.Bind(endpoint);//将负责监听的套接字绑定到指定的ip和端口上 socketWatch.Listen(10); //======================================================= //创建监听线程,防止UI线程与监听线程冲突 watchTread = new Thread(WatchConnection); watchTread.IsBackground = true; watchTread.Start(); //======================================================== //创建客户端向客户端发送消息进程 //clientToClientThread = new Thread(recieveAndSend); //clientToClientThread.IsBackground = false; //clientToClientThread.Start(); MsgShow("服务器监听成功!"); } /// <summary> /// 链接服务端 /// </summary> void WatchConnection() { while(true) { socketConnetion = socketWatch.Accept();//一旦监听成功将返回一个链接套接字 string strClient = socketConnetion.RemoteEndPoint.ToString();//获取远程客户端的ip和port信息 lbClientList.Items.Add(strClient); dtSocket.Add(strClient, socketConnetion); //======================================================= //创建接收消息进程 recieveTread = new Thread(recievInfo);//当服务端接收消息时会和ui线程冲突,新建线程 recieveTread.IsBackground = false; recieveTread.Start(socketConnetion); dtThread.Add(strClient,recieveTread); MsgShow("连接服务器成功!!" + strClient); } } #region 接受消息委托--recievInfo(Object socketpara) /// <summary> /// 接收消息 /// </summary> void recievInfo(Object socketpara) { Socket socketRecieve = socketpara as Socket; while (true) { Byte[] arrMsgRec = new byte[1024 * 1024 * 2]; //用来储存接收服务端的二进制数据 //接收到文件的长度 int length = socketRecieve.Receive(arrMsgRec);//完成了接受传过来的二进制数据,并且返回一个整形数值 if (arrMsgRec[0] == 0) { //把接收到的消息转化成string类型 string strMsgRec = System.Text.Encoding.UTF8.GetString(arrMsgRec, 1, length); MsgShow(strMsgRec); } else if (arrMsgRec[0] == 1) { SaveFileDialog sfd = new SaveFileDialog(); if (sfd.ShowDialog(this) == DialogResult.OK) { string fileSavePath = sfd.FileName; using (FileStream fs = new FileStream(fileSavePath, FileMode.Create)) { fs.Write(arrMsgRec, 1, length - 1); MsgShow("文件已经保存在:" + fileSavePath); } } } } } #endregion void MsgShow(string str) { txtMsg.AppendText(str + "\r\n"); } private void btnSend_Click(object sender, EventArgs e) { string strMsg=txtSend.Text.Trim(); byte[] arrMsg=System.Text.Encoding.UTF8.GetBytes(strMsg); //==================================================================== //找到选中客户端,让字典中的key值与选中客户端的名字相同,则向他发送消息 string strClient = lbClientList.Text; dtSocket[strClient].Send(arrMsg); MsgShow(strMsg + "已发送!"); } private void btnSendToAll_Click(object sender, EventArgs e) { string strMsg = txtSend.Text.Trim(); byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(strMsg); foreach (Socket s in dtSocket.Values) { s.Send(arrMsg); } MsgShow("群发完毕~~"); } } }
客户端代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Net; using System.Threading; using System.IO; namespace ChatClient { public partial class FClient : Form { public FClient() { InitializeComponent(); //防止出现不运行的txtMsg线程操作 TextBox.CheckForIllegalCrossThreadCalls = false; } Socket socketClient; private void BtnConntionServer_Click(object sender, EventArgs e) { socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress address = IPAddress.Parse(txtIP.Text.ToString()); IPEndPoint endpoint = new IPEndPoint(address, int.Parse(txtPort.Text.Trim())); socketClient.Connect(endpoint); Thread recThread = new Thread(RecMsg); recThread.IsBackground = false;//防止在关闭程序后另外一个线程还在进行 recThread.Start();//出现一个实例,一定要让其start 说明是线程可以开始了即就绪了 } void RecMsg() { while(true) { Byte[] arrMsgRec = new byte[1024 * 1024 * 2]; //用来储存接收服务端的二进制数据 //接收到文件的长度 int length=socketClient.Receive(arrMsgRec); //把接收到的消息转化成string类型 string strMsgRec = System.Text.Encoding.UTF8.GetString(arrMsgRec, 0, length); MsgShow(strMsgRec); } } void MsgShow(string str) { txtMsg.AppendText(str + "\r\n"); } private void btnSend_Click(object sender, EventArgs e) {//先与主机建立链接,然后让主机产生一个负责通讯的套接字,最终由客户端send,服务端recieve就可以 //socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //IPAddress address = IPAddress.Parse(txtIP.Text.ToString()); //IPEndPoint endpoint = new IPEndPoint(address, int.Parse(txtPort.Text.Trim())); //socketClient.Connect(endpoint); string msg = this.txtC2S.Text.Trim(); byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(msg); byte[] arrMsgSend = new byte[arrMsg.Length + 1]; arrMsgSend[0] = 0; Buffer.BlockCopy(arrMsg, 0, arrMsgSend, 1, arrMsg.Length); socketClient.Send(arrMsgSend); MsgShow("已经将"+msg+"发送到服务端"); } private void btnChoiceFile_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) { using(FileStream fs=new FileStream(txtFilePath.Text,FileMode.Open)) { byte[] arrFile = new byte[1024 * 1024 * 2]; int length = fs.Read(arrFile, 0, arrFile.Length); byte[] arrFileSend = new byte[length + 1]; arrFileSend[0] = 1; Buffer.BlockCopy(arrFile, 0, arrFileSend,1, length ); socketClient.Send(arrFileSend); MsgShow("传送成功!"); } } } }
相关文章推荐
- OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版。 本文对OAuth 2.0的设计思路和运行流程,做一个简明通俗的解释,主要参考材料为R
- RPC服务端+客户端 参考代码
- linux epoll机制对TCP 客户端和服务端的监听C代码通用框架实现
- 关于socket 实现聊天室时客户端导致readline出现阻塞解决办法
- ios消息推送客户端和服务端实现带代码(java)
- 关于架构客户端的思路之UI篇(未写完)
- android客户端和java服务端之间用socket来传输图片(示例代码)
- asp.net服务端代码中获取客户端代码计算名称
- cxf整合Spring框架进行服务端开发,并且通过cxf生成客户端代码进行调用
- 关于极光推送C#服务端代码的一些问题
- java语言实现杨辉三角的主要思路和代码
- 使用Qt将一系列图片通过网络发送到客户端动态显示的参考代码(修改一下可以用作远程网络监控)。
- 关于WCF REST(webHttpBinding ) 用法:服务端写法及客户端调用
- Android 客户端从服务端获取json数据并解析的实现代码
- Axis2C:由WSDL自动生成服务端及客户端 C代码
- TCP 客户端 服务端详细代码
- webservice代码编写主要包括服务器端发布和客户端调用。
- cxf 方式三 是java客户端调用.net服务端 好使代码
- 一起谈.NET技术,浅谈思路严谨的用户在线状态控制【附部分C#参考代码】
- cxf 服务端生成客户端的代码