急需做一个Web在线客服通讯的系统,希望高人指点!!!!!! 本帖最后由 lxcnn 于 2010-08-07 13:14:59 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Web在线客服通讯的系统?你起码说明下你的系统要求功能? 要求的功能是登陆网站的客户可以寻求在线咨询有专门的客服人员负责接待其实功能不是很多就是客服人员能看到在线的来访人员如来访人员寻求在线支持的话就在网上进行在线沟通但客户发送的问题将在所有的客服人员机器上显示这个系统复杂吗需要用TCP或者Socket吗还是遍历定时搜索数据库 需要及时消息呀,还是不要定时搜索数据库用socket吧 有相关的例子可以提供吗?我对Socket不是很了解..... 我有一套web版的socket 聊天程序。 利用flash做通讯桥,不刷数据库,纯的tcp连接,客户只需要装了flash,就可以聊天。 服务端是c# 异步socket实现的, 需要的话可以联系我 web在线客服系统,我以前做过。主要还是通讯的问题,web程序不能“推”,所以要找到一个让web主动推送信息的方法(不使用插件)。有一种方法叫flex你可以看看。就是页面发送一个请求(A),而服务器一直不返回,客户端以后的请求通过ajax实现,服务器收到请求后通过请求(A)向客户端返回(flush)但是不Response.End。这样客户端和服务器就能交互通讯了。 应该不会很复杂。客服人员机器上装得是Server端,来访人员是client端。所以但客户发送的问题将在所有的客服人员机器上显示这一条很容易。至于“就是客服人员能看到在线的来访人员如来访人员寻求在线支持的话就在网上进行在线沟通”开个Socket通讯就能完成。但是问题是如果有很多客服人员,总应该有一个职责划分吧。对应每一个具体的来访人员,究竟谁来回来问题呢? To:静黎明给你我的邮箱 发给我一份吧 谢谢TO:boxter110不是很明白 能不能来个简单的例子? 我的MSN:[email protected]:kingofbull(大力牛魔王)权限问题老板还没有具体的要求只是先把这个功能实现了就可以请问有例子吗? 简单的AJAXhttp://www.yy0752.com/chatroom/client/login.htmGuestguest To:swife请问能给我这个程序的源码看看吗? 是要这种功能吗?http://pp.shdzm.com/E-mail:[email protected] swife(2277112):能发一份给我吗?小弟的邮箱:[email protected]谢谢了!到http://community.csdn.net/Expert/topic/5303/5303502.xml?temp=.5290491,我给你分 TO:xjiiix()这个地址测试不了没有用户其实就是很简单的功能在线的对话还有在线人员的浏览但需要一对多的显示其实是不是只用刷新数据库显示最新消息即可? to:shitingzhao(>Think Ahead->Learn More->Solve Now->Case Study-) 在哪里了啊? swife(2277112)给我也一个吧,[email protected]非常感谢! to :swife(2277112)也发一份给我吧,很想学习! [email protected] 谢谢! using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Net;using System.Net.Sockets;namespace Client{ public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox txtIP; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.RichTextBox txtSendMsg; private System.Windows.Forms.Label label4; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button4; private System.Windows.Forms.RichTextBox txtRecvMsg; private System.Windows.Forms.TextBox txtPort; private System.Windows.Forms.Button button5; private System.ComponentModel.Container components = null; byte[] m_dataBuffer = new byte[10]; IAsyncResult result; public AsyncCallback pfnCallBack ; private System.Windows.Forms.Label status; private System.Windows.Forms.Label label5; public Socket clientSocket; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.txtIP = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.txtPort = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.txtSendMsg = new System.Windows.Forms.RichTextBox(); this.label4 = new System.Windows.Forms.Label(); this.status = new System.Windows.Forms.Label(); this.txtRecvMsg = new System.Windows.Forms.RichTextBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.label5 = new System.Windows.Forms.Label(); this.button5 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(16, 24); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(60, 17); this.label1.TabIndex = 0; this.label1.Text = "服务器IP:"; // // txtIP // this.txtIP.Location = new System.Drawing.Point(80, 24); this.txtIP.Name = "txtIP"; this.txtIP.Size = new System.Drawing.Size(160, 21); this.txtIP.TabIndex = 1; this.txtIP.Text = ""; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(16, 56); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(35, 17); this.label2.TabIndex = 2; this.label2.Text = "端口:"; // // txtPort // this.txtPort.Location = new System.Drawing.Point(80, 56); this.txtPort.Name = "txtPort"; this.txtPort.Size = new System.Drawing.Size(40, 21); this.txtPort.TabIndex = 3; this.txtPort.Text = ""; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(16, 96); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(110, 17); this.label3.TabIndex = 4; this.label3.Text = "发送信息给服务器:"; // // txtSendMsg // this.txtSendMsg.Location = new System.Drawing.Point(16, 120); this.txtSendMsg.Name = "txtSendMsg"; this.txtSendMsg.Size = new System.Drawing.Size(224, 88); this.txtSendMsg.TabIndex = 5; this.txtSendMsg.Text = ""; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(16, 248); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(60, 17); this.label4.TabIndex = 6; this.label4.Text = "连接状态:"; // // status // this.status.Location = new System.Drawing.Point(80, 248); this.status.Name = "status"; this.status.Size = new System.Drawing.Size(192, 23); this.status.TabIndex = 7; // // txtRecvMsg // this.txtRecvMsg.Location = new System.Drawing.Point(264, 80); this.txtRecvMsg.Name = "txtRecvMsg"; this.txtRecvMsg.ReadOnly = true; this.txtRecvMsg.Size = new System.Drawing.Size(224, 144); this.txtRecvMsg.TabIndex = 8; this.txtRecvMsg.Text = ""; // // button1 // this.button1.Location = new System.Drawing.Point(280, 16); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(88, 32); this.button1.TabIndex = 9; this.button1.Text = "连接"; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(384, 16); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(88, 32); this.button2.TabIndex = 10; this.button2.Text = "断开"; this.button2.Click += new System.EventHandler(this.button2_Click); // // button3 // this.button3.Location = new System.Drawing.Point(280, 232); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(88, 32); this.button3.TabIndex = 11; this.button3.Text = "清空信息"; this.button3.Click += new System.EventHandler(this.button3_Click); // // button4 // this.button4.Location = new System.Drawing.Point(384, 232); this.button4.Name = "button4"; this.button4.Size = new System.Drawing.Size(88, 32); this.button4.TabIndex = 12; this.button4.Text = "关闭"; this.button4.Click += new System.EventHandler(this.button4_Click); // // label5 // this.label5.AutoSize = true; this.label5.Location = new System.Drawing.Point(264, 64); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(134, 17); this.label5.TabIndex = 13; this.label5.Text = "收到服务器发来的信息:"; // // button5 // this.button5.Location = new System.Drawing.Point(16, 208); this.button5.Name = "button5"; this.button5.Size = new System.Drawing.Size(224, 32); this.button5.TabIndex = 14; this.button5.Text = "发送信息"; this.button5.Click += new System.EventHandler(this.button5_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(512, 277); this.Controls.Add(this.button5); this.Controls.Add(this.label5); this.Controls.Add(this.button4); this.Controls.Add(this.button3); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.txtRecvMsg); this.Controls.Add(this.status); this.Controls.Add(this.label4); this.Controls.Add(this.txtSendMsg); this.Controls.Add(this.label3); this.Controls.Add(this.txtPort); this.Controls.Add(this.label2); this.Controls.Add(this.txtIP); this.Controls.Add(this.label1); this.Name = "Form1"; this.Text = "Client"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion [STAThread] static void Main() { Application.Run(new Form1()); } // 连接按钮 private void button1_Click(object sender, System.EventArgs e) { // IP地址和端口号不能为空 if(txtIP.Text == "" || txtPort.Text == "") { MessageBox.Show("请先完整填写服务器IP地址和端口号!", "提示");//如果服务器端IP和端品已写,则这里可以将IP和端口直接写进代码. return; } try { // 创建Socket实例 clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // 得到服务器的IP地址 IPAddress ipAddress = IPAddress.Parse(txtIP.Text); Int32 port = Int32.Parse(txtPort.Text); // 创建远程终结点 IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); // 连接到远程服务器 clientSocket.Connect(remoteEP); if(clientSocket.Connected) { UpdateControls(true); WaitForData(); // 异步等待数据 } } catch(SocketException se) { MessageBox.Show (se.Message, "提示"); UpdateControls(false); } } // 等待数据 public void WaitForData() { try { if(pfnCallBack == null) { // 当连接上的客户有写的操作的时候,调用回调函数 pfnCallBack = new AsyncCallback(OnDataReceived); } SocketPacket socketPacket = new SocketPacket(); socketPacket.thisSocket = clientSocket; result = clientSocket.BeginReceive(socketPacket.dataBuffer, 0, socketPacket.dataBuffer.Length, SocketFlags.None, pfnCallBack, socketPacket); } catch(SocketException se) { MessageBox.Show(se.Message, "提示"); } } // 该类保存Socket以及发送给服务器的数据 public class SocketPacket { public System.Net.Sockets.Socket thisSocket; public byte[] dataBuffer = new byte[1024]; // 发给服务器的数据 } // 接收数据 public void OnDataReceived(IAsyncResult asyn) { try { SocketPacket theSockId = (SocketPacket)asyn.AsyncState ; // EndReceive完成BeginReceive异步调用,返回服务器写入流的字节数 int iRx = theSockId.thisSocket.EndReceive(asyn); // 加 1 是因为字符串以 '\0' 作为结束标志符 char[] chars = new char[iRx + 1]; // 用UTF8格式来将string信息转化成byte数组形式 System.Text.Decoder decoder = System.Text.Encoding.UTF8.GetDecoder(); int charLen = decoder.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0); System.String szData = new System.String(chars); // 将收到的信息显示在信息列表中 txtRecvMsg.Text = txtRecvMsg.Text + szData; // 等待数据 WaitForData(); } catch (ObjectDisposedException) { System.Diagnostics.Debugger.Log(0,"1","\nOnDataReceived: Socket已经关闭!\n"); } catch(SocketException se) { if(se.ErrorCode == 10054) { string msg = "服务器" + "停止服务!" + "\n"; txtRecvMsg.Text = txtRecvMsg.Text + msg; clientSocket.Close(); clientSocket = null; UpdateControls(false); } else { MessageBox.Show(se.Message, "提示"); } } } // 更新控件。连接和断开(发送信息)按钮的状态是互斥的 private void UpdateControls(bool connected) { button1.Enabled = !connected; button2.Enabled = connected; button5.Enabled = connected; if(connected) { status.Text = "已连接"; } else { status.Text = "无连接"; } } // 断开按钮 private void button2_Click(object sender, System.EventArgs e) { // 关闭Socket if(clientSocket != null) { clientSocket.Close(); clientSocket = null; UpdateControls(false); } } // 发送信息按钮 private void button5_Click(object sender, System.EventArgs e) { try { // 如果客户与服务器有连接,则允许发送信息 if(clientSocket.Connected) { string msg = txtSendMsg.Text + "\n"; // 用UTF8格式来将string信息转化成byte数组形式 byte[] byData = System.Text.Encoding.UTF8.GetBytes(msg); if(clientSocket != null) { // 发送数据 clientSocket.Send(byData); } } } catch(Exception se) { MessageBox.Show(se.Message, "提示"); } } // 清空按钮 private void button3_Click(object sender, System.EventArgs e) { txtRecvMsg.Clear(); // 清空信息列表 } // 关闭按钮 private void button4_Click(object sender, System.EventArgs e) { // 关闭Socket if(clientSocket != null) { clientSocket.Close(); clientSocket = null; } Close(); // 关闭窗体 } private void Form1_Load(object sender, System.EventArgs e) { UpdateControls(false); // 初始化时,只有连接按钮可用 } }} using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Net;using System.Net.Sockets;using System.Threading; namespace Server{ public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox txtIP; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Button button1; private System.Windows.Forms.Label label4; private System.Windows.Forms.Label label5; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button4; private System.Windows.Forms.Button button5; private System.Windows.Forms.RichTextBox txtSendMsg; private System.Windows.Forms.RichTextBox txtRecvMsg; private System.ComponentModel.Container components = null; public delegate void UpdateRichEditCallback(string text); public delegate void UpdateClientListCallback(); public AsyncCallback pfnWorkerCallBack ; private Socket mainSocket; // 记录客户的Socket private System.Collections.ArrayList workerSocketList = ArrayList.Synchronized(new System.Collections.ArrayList()); private System.Windows.Forms.TextBox txtPort; private System.Windows.Forms.ListBox clientList; private System.Windows.Forms.Label status; private System.Windows.Forms.Label label6; private int clientNum = 0; // 客户的编号,随着客户登录的先后,逐渐增大 public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.txtIP = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.txtPort = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.txtSendMsg = new System.Windows.Forms.RichTextBox(); this.button1 = new System.Windows.Forms.Button(); this.label4 = new System.Windows.Forms.Label(); this.clientList = new System.Windows.Forms.ListBox(); this.label5 = new System.Windows.Forms.Label(); this.status = new System.Windows.Forms.Label(); this.txtRecvMsg = new System.Windows.Forms.RichTextBox(); this.label6 = new System.Windows.Forms.Label(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.button5 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(16, 24); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(60, 17); this.label1.TabIndex = 0; this.label1.Text = "服务器IP:"; // // txtIP // this.txtIP.Location = new System.Drawing.Point(88, 24); this.txtIP.Name = "txtIP"; this.txtIP.ReadOnly = true; this.txtIP.Size = new System.Drawing.Size(152, 21); this.txtIP.TabIndex = 1; this.txtIP.Text = ""; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(16, 56); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(35, 17); this.label2.TabIndex = 2; this.label2.Text = "端口:"; // // txtPort // this.txtPort.Location = new System.Drawing.Point(88, 56); this.txtPort.Name = "txtPort"; this.txtPort.Size = new System.Drawing.Size(40, 21); this.txtPort.TabIndex = 3; this.txtPort.Text = ""; // // label3 // this.label3.Location = new System.Drawing.Point(16, 88); this.label3.Name = "label3"; this.label3.TabIndex = 4; this.label3.Text = "发送信息给客户:"; // // txtSendMsg // this.txtSendMsg.Location = new System.Drawing.Point(16, 112); this.txtSendMsg.Name = "txtSendMsg"; this.txtSendMsg.Size = new System.Drawing.Size(224, 72); this.txtSendMsg.TabIndex = 5; this.txtSendMsg.Text = ""; // // button1 // this.button1.Location = new System.Drawing.Point(16, 184); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(224, 32); this.button1.TabIndex = 6; this.button1.Text = "发送信息"; this.button1.Click += new System.EventHandler(this.button1_Click); // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(16, 224); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(122, 17); this.label4.TabIndex = 7; this.label4.Text = "连接上服务器的客户:"; // // clientList // this.clientList.ItemHeight = 12; this.clientList.Location = new System.Drawing.Point(16, 248); this.clientList.Name = "clientList"; this.clientList.Size = new System.Drawing.Size(224, 112); this.clientList.TabIndex = 8; // // label5 // this.label5.AutoSize = true; this.label5.Location = new System.Drawing.Point(16, 376); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(60, 17); this.label5.TabIndex = 9; this.label5.Text = "状态信息:"; // // status // this.status.Location = new System.Drawing.Point(88, 376); this.status.Name = "status"; this.status.Size = new System.Drawing.Size(192, 23); this.status.TabIndex = 10; // // txtRecvMsg // this.txtRecvMsg.Location = new System.Drawing.Point(256, 112); this.txtRecvMsg.Name = "txtRecvMsg"; this.txtRecvMsg.ReadOnly = true; this.txtRecvMsg.Size = new System.Drawing.Size(232, 248); this.txtRecvMsg.TabIndex = 11; this.txtRecvMsg.Text = ""; // // label6 // this.label6.AutoSize = true; this.label6.Location = new System.Drawing.Point(256, 88); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(122, 17); this.label6.TabIndex = 12; this.label6.Text = "收到客户发来的信息:"; // // button2 // this.button2.Location = new System.Drawing.Point(264, 24); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(104, 40); this.button2.TabIndex = 13; this.button2.Text = "启动服务"; this.button2.Click += new System.EventHandler(this.button2_Click); 我也想要一个,给后必重谢,一定给你加分,谢谢邮箱:[email protected] 我也想要一个,非常感谢!coldestice#hotmail.com,谢谢! 我以为想要一个 谢谢! 邮箱:[email protected] 请发送给我一份 [email protected]给后加分 谢谢~~!!! 请教个类的问题, 不好意思! 使用FileInfo的delete时,总报The process cannot access,because it is being used fileinfo 简单的问题,如何获取ListBox里的item? 一个简单连接数据库问题 如何批量插入数据 如何把图片存储到ACCESS数据库里? 求教:POST传递多个参数的方法 关于水晶报表的错误 请问有没有朋友用C#开发过与硬件连接读取数据的系统呢? 数据显示小问题!! 桌面程序中文本框怎么限制只能输入数字? 急!dataGridView上列标题怎么修改
你起码说明下你的系统要求功能?
有专门的客服人员负责接待
其实功能不是很多
就是客服人员能看到在线的来访人员
如来访人员寻求在线支持的话
就在网上进行在线沟通
但客户发送的问题将在所有的客服人员机器上显示
这个系统复杂吗
需要用TCP或者Socket吗
还是遍历定时搜索数据库
用socket吧
我对Socket不是很了解.....
客户只需要装了flash,就可以聊天。 服务端是c# 异步socket实现的, 需要的话可以联系我
有一种方法叫flex你可以看看。就是页面发送一个请求(A),而服务器一直不返回,客户端以后的请求通过ajax实现,服务器收到请求后通过请求(A)向客户端返回(flush)但是不Response.End。这样客户端和服务器就能交互通讯了。
客服人员机器上装得是Server端,来访人员是client端。
所以但客户发送的问题将在所有的客服人员机器上显示这一条很容易。至于
“就是客服人员能看到在线的来访人员
如来访人员寻求在线支持的话
就在网上进行在线沟通”
开个Socket通讯就能完成。但是问题是如果有很多客服人员,总应该有一个职责划分吧。
对应每一个具体的来访人员,究竟谁来回来问题呢?
只是先把这个功能实现了就可以
请问有例子吗?
http://www.yy0752.com/chatroom/client/login.htm
Guest
guest
请问能给我这个程序的源码看看吗?
http://pp.shdzm.com/
E-mail:[email protected]
这个地址测试不了没有用户
其实就是很简单的功能
在线的对话还有在线人员的浏览
但需要一对多的显示
其实是不是只用刷新数据库显示最新消息即可?
给我也一个吧,[email protected]非常感谢!
也发一份给我吧,很想学习!
[email protected]
谢谢!
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;namespace Client
{ public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtIP;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.RichTextBox txtSendMsg;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.RichTextBox txtRecvMsg;
private System.Windows.Forms.TextBox txtPort;
private System.Windows.Forms.Button button5;
private System.ComponentModel.Container components = null; byte[] m_dataBuffer = new byte[10];
IAsyncResult result;
public AsyncCallback pfnCallBack ;
private System.Windows.Forms.Label status;
private System.Windows.Forms.Label label5;
public Socket clientSocket;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.txtIP = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.txtPort = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.txtSendMsg = new System.Windows.Forms.RichTextBox();
this.label4 = new System.Windows.Forms.Label();
this.status = new System.Windows.Forms.Label();
this.txtRecvMsg = new System.Windows.Forms.RichTextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.label5 = new System.Windows.Forms.Label();
this.button5 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(16, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(60, 17);
this.label1.TabIndex = 0;
this.label1.Text = "服务器IP:";
//
// txtIP
//
this.txtIP.Location = new System.Drawing.Point(80, 24);
this.txtIP.Name = "txtIP";
this.txtIP.Size = new System.Drawing.Size(160, 21);
this.txtIP.TabIndex = 1;
this.txtIP.Text = "";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(16, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 17);
this.label2.TabIndex = 2;
this.label2.Text = "端口:";
//
// txtPort
//
this.txtPort.Location = new System.Drawing.Point(80, 56);
this.txtPort.Name = "txtPort";
this.txtPort.Size = new System.Drawing.Size(40, 21);
this.txtPort.TabIndex = 3;
this.txtPort.Text = "";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(16, 96);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(110, 17);
this.label3.TabIndex = 4;
this.label3.Text = "发送信息给服务器:";
//
// txtSendMsg
//
this.txtSendMsg.Location = new System.Drawing.Point(16, 120);
this.txtSendMsg.Name = "txtSendMsg";
this.txtSendMsg.Size = new System.Drawing.Size(224, 88);
this.txtSendMsg.TabIndex = 5;
this.txtSendMsg.Text = "";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(16, 248);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(60, 17);
this.label4.TabIndex = 6;
this.label4.Text = "连接状态:";
//
// status
//
this.status.Location = new System.Drawing.Point(80, 248);
this.status.Name = "status";
this.status.Size = new System.Drawing.Size(192, 23);
this.status.TabIndex = 7;
//
// txtRecvMsg
//
this.txtRecvMsg.Location = new System.Drawing.Point(264, 80);
this.txtRecvMsg.Name = "txtRecvMsg";
this.txtRecvMsg.ReadOnly = true;
this.txtRecvMsg.Size = new System.Drawing.Size(224, 144);
this.txtRecvMsg.TabIndex = 8;
this.txtRecvMsg.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(280, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 32);
this.button1.TabIndex = 9;
this.button1.Text = "连接";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(384, 16);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(88, 32);
this.button2.TabIndex = 10;
this.button2.Text = "断开";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(280, 232);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(88, 32);
this.button3.TabIndex = 11;
this.button3.Text = "清空信息";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(384, 232);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(88, 32);
this.button4.TabIndex = 12;
this.button4.Text = "关闭";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(264, 64);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(134, 17);
this.label5.TabIndex = 13;
this.label5.Text = "收到服务器发来的信息:";
//
// button5
//
this.button5.Location = new System.Drawing.Point(16, 208);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(224, 32);
this.button5.TabIndex = 14;
this.button5.Text = "发送信息";
this.button5.Click += new System.EventHandler(this.button5_Click);
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(512, 277);
this.Controls.Add(this.button5);
this.Controls.Add(this.label5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.txtRecvMsg);
this.Controls.Add(this.status);
this.Controls.Add(this.label4);
this.Controls.Add(this.txtSendMsg);
this.Controls.Add(this.label3);
this.Controls.Add(this.txtPort);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtIP);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Client";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion [STAThread]
static void Main()
{
Application.Run(new Form1());
} // 连接按钮
private void button1_Click(object sender, System.EventArgs e)
{
// IP地址和端口号不能为空
if(txtIP.Text == "" || txtPort.Text == "")
{
MessageBox.Show("请先完整填写服务器IP地址和端口号!", "提示");//如果服务器端IP和端品已写,则这里可以将IP和端口直接写进代码.
return;
}
try
{
// 创建Socket实例
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// 得到服务器的IP地址
IPAddress ipAddress = IPAddress.Parse(txtIP.Text);
Int32 port = Int32.Parse(txtPort.Text);
// 创建远程终结点
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// 连接到远程服务器
clientSocket.Connect(remoteEP);
if(clientSocket.Connected)
{
UpdateControls(true);
WaitForData(); // 异步等待数据
}
}
catch(SocketException se)
{
MessageBox.Show (se.Message, "提示");
UpdateControls(false);
}
}
// 等待数据
public void WaitForData()
{
try
{
if(pfnCallBack == null)
{
// 当连接上的客户有写的操作的时候,调用回调函数
pfnCallBack = new AsyncCallback(OnDataReceived);
}
SocketPacket socketPacket = new SocketPacket();
socketPacket.thisSocket = clientSocket;
result = clientSocket.BeginReceive(socketPacket.dataBuffer, 0, socketPacket.dataBuffer.Length,
SocketFlags.None, pfnCallBack, socketPacket);
}
catch(SocketException se)
{
MessageBox.Show(se.Message, "提示");
}
}
// 该类保存Socket以及发送给服务器的数据
public class SocketPacket
{
public System.Net.Sockets.Socket thisSocket;
public byte[] dataBuffer = new byte[1024]; // 发给服务器的数据
} // 接收数据
public void OnDataReceived(IAsyncResult asyn)
{
try
{
SocketPacket theSockId = (SocketPacket)asyn.AsyncState ;
// EndReceive完成BeginReceive异步调用,返回服务器写入流的字节数
int iRx = theSockId.thisSocket.EndReceive(asyn);
// 加 1 是因为字符串以 '\0' 作为结束标志符
char[] chars = new char[iRx + 1];
// 用UTF8格式来将string信息转化成byte数组形式
System.Text.Decoder decoder = System.Text.Encoding.UTF8.GetDecoder();
int charLen = decoder.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
// 将收到的信息显示在信息列表中
txtRecvMsg.Text = txtRecvMsg.Text + szData;
// 等待数据
WaitForData();
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0,"1","\nOnDataReceived: Socket已经关闭!\n");
}
catch(SocketException se)
{
if(se.ErrorCode == 10054)
{
string msg = "服务器" + "停止服务!" + "\n";
txtRecvMsg.Text = txtRecvMsg.Text + msg;
clientSocket.Close();
clientSocket = null;
UpdateControls(false);
}
else
{
MessageBox.Show(se.Message, "提示");
}
}
} // 更新控件。连接和断开(发送信息)按钮的状态是互斥的
private void UpdateControls(bool connected)
{
button1.Enabled = !connected;
button2.Enabled = connected;
button5.Enabled = connected;
if(connected)
{
status.Text = "已连接";
}
else
{
status.Text = "无连接";
}
} // 断开按钮
private void button2_Click(object sender, System.EventArgs e)
{
// 关闭Socket
if(clientSocket != null)
{
clientSocket.Close();
clientSocket = null;
UpdateControls(false);
}
} // 发送信息按钮
private void button5_Click(object sender, System.EventArgs e)
{
try
{
// 如果客户与服务器有连接,则允许发送信息
if(clientSocket.Connected)
{
string msg = txtSendMsg.Text + "\n";
// 用UTF8格式来将string信息转化成byte数组形式
byte[] byData = System.Text.Encoding.UTF8.GetBytes(msg);
if(clientSocket != null)
{
// 发送数据
clientSocket.Send(byData);
}
}
}
catch(Exception se)
{
MessageBox.Show(se.Message, "提示");
}
}
// 清空按钮
private void button3_Click(object sender, System.EventArgs e)
{
txtRecvMsg.Clear(); // 清空信息列表
} // 关闭按钮
private void button4_Click(object sender, System.EventArgs e)
{
// 关闭Socket
if(clientSocket != null)
{
clientSocket.Close();
clientSocket = null;
}
Close(); // 关闭窗体
} private void Form1_Load(object sender, System.EventArgs e)
{
UpdateControls(false); // 初始化时,只有连接按钮可用
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Threading; namespace Server
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtIP;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.RichTextBox txtSendMsg;
private System.Windows.Forms.RichTextBox txtRecvMsg;
private System.ComponentModel.Container components = null; public delegate void UpdateRichEditCallback(string text);
public delegate void UpdateClientListCallback();
public AsyncCallback pfnWorkerCallBack ;
private Socket mainSocket; // 记录客户的Socket
private System.Collections.ArrayList workerSocketList =
ArrayList.Synchronized(new System.Collections.ArrayList());
private System.Windows.Forms.TextBox txtPort;
private System.Windows.Forms.ListBox clientList;
private System.Windows.Forms.Label status;
private System.Windows.Forms.Label label6; private int clientNum = 0; // 客户的编号,随着客户登录的先后,逐渐增大
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.txtIP = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.txtPort = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.txtSendMsg = new System.Windows.Forms.RichTextBox();
this.button1 = new System.Windows.Forms.Button();
this.label4 = new System.Windows.Forms.Label();
this.clientList = new System.Windows.Forms.ListBox();
this.label5 = new System.Windows.Forms.Label();
this.status = new System.Windows.Forms.Label();
this.txtRecvMsg = new System.Windows.Forms.RichTextBox();
this.label6 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(16, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(60, 17);
this.label1.TabIndex = 0;
this.label1.Text = "服务器IP:";
//
// txtIP
//
this.txtIP.Location = new System.Drawing.Point(88, 24);
this.txtIP.Name = "txtIP";
this.txtIP.ReadOnly = true;
this.txtIP.Size = new System.Drawing.Size(152, 21);
this.txtIP.TabIndex = 1;
this.txtIP.Text = "";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(16, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 17);
this.label2.TabIndex = 2;
this.label2.Text = "端口:";
//
// txtPort
//
this.txtPort.Location = new System.Drawing.Point(88, 56);
this.txtPort.Name = "txtPort";
this.txtPort.Size = new System.Drawing.Size(40, 21);
this.txtPort.TabIndex = 3;
this.txtPort.Text = "";
//
// label3
//
this.label3.Location = new System.Drawing.Point(16, 88);
this.label3.Name = "label3";
this.label3.TabIndex = 4;
this.label3.Text = "发送信息给客户:";
//
// txtSendMsg
//
this.txtSendMsg.Location = new System.Drawing.Point(16, 112);
this.txtSendMsg.Name = "txtSendMsg";
this.txtSendMsg.Size = new System.Drawing.Size(224, 72);
this.txtSendMsg.TabIndex = 5;
this.txtSendMsg.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 184);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(224, 32);
this.button1.TabIndex = 6;
this.button1.Text = "发送信息";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(16, 224);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(122, 17);
this.label4.TabIndex = 7;
this.label4.Text = "连接上服务器的客户:";
//
// clientList
//
this.clientList.ItemHeight = 12;
this.clientList.Location = new System.Drawing.Point(16, 248);
this.clientList.Name = "clientList";
this.clientList.Size = new System.Drawing.Size(224, 112);
this.clientList.TabIndex = 8;
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(16, 376);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(60, 17);
this.label5.TabIndex = 9;
this.label5.Text = "状态信息:";
//
// status
//
this.status.Location = new System.Drawing.Point(88, 376);
this.status.Name = "status";
this.status.Size = new System.Drawing.Size(192, 23);
this.status.TabIndex = 10;
//
// txtRecvMsg
//
this.txtRecvMsg.Location = new System.Drawing.Point(256, 112);
this.txtRecvMsg.Name = "txtRecvMsg";
this.txtRecvMsg.ReadOnly = true;
this.txtRecvMsg.Size = new System.Drawing.Size(232, 248);
this.txtRecvMsg.TabIndex = 11;
this.txtRecvMsg.Text = "";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(256, 88);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(122, 17);
this.label6.TabIndex = 12;
this.label6.Text = "收到客户发来的信息:";
//
// button2
//
this.button2.Location = new System.Drawing.Point(264, 24);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(104, 40);
this.button2.TabIndex = 13;
this.button2.Text = "启动服务";
this.button2.Click += new System.EventHandler(this.button2_Click);
邮箱:[email protected]
coldestice#hotmail.com,谢谢!