我的这个程序不能发送中文,发中文是时就是“?”,而发英文时是完好的。网友说是编码应设为UTF8,可我不会,哪位朋友能替我修改一下?万分感谢!
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 ChatServer
{
public class ClientSeverForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox txtHost;
private System.Windows.Forms.TextBox txtPort;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.ComboBox CurUserList;
private System.Windows.Forms.ListBox lstInfo;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
//该服务器默认的监听的端口号
static int port=1234;
private TcpListener listener;
private Socket tmpSocket;
//服务器可以支持的最多的客户端的连接数
static int MaxNum=100;
//clients数组保存当前在线用户的Client对象
static ArrayList clients=new ArrayList(); public ClientSeverForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtHost = new System.Windows.Forms.TextBox();
this.txtPort = new System.Windows.Forms.TextBox();
this.btnStart = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.CurUserList = new System.Windows.Forms.ComboBox();
this.label3 = new System.Windows.Forms.Label();
this.lstInfo = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 16);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "主机号:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label2
//
this.label2.Location = new System.Drawing.Point(0, 48);
this.label2.Name = "label2";
this.label2.TabIndex = 1;
this.label2.Text = "端口号:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// txtHost
//
this.txtHost.Location = new System.Drawing.Point(120, 16);
this.txtHost.Name = "txtHost";
this.txtHost.TabIndex = 2;
this.txtHost.Text = "";
//
// txtPort
//
this.txtPort.Location = new System.Drawing.Point(120, 48);
this.txtPort.Name = "txtPort";
this.txtPort.ReadOnly = true;
this.txtPort.TabIndex = 3;
this.txtPort.Text = "";
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(232, 16);
this.btnStart.Name = "btnStart";
this.btnStart.TabIndex = 4;
this.btnStart.Text = "启动";
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(232, 64);
this.btnExit.Name = "btnExit";
this.btnExit.TabIndex = 5;
this.btnExit.Text = "退出";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// CurUserList
//
this.CurUserList.Location = new System.Drawing.Point(112, 96);
this.CurUserList.Name = "CurUserList";
this.CurUserList.Size = new System.Drawing.Size(121, 20);
this.CurUserList.TabIndex = 6;
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 96);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(96, 23);
this.label3.TabIndex = 8;
this.label3.Text = "当前在线用户:";
//
// lstInfo
//
this.lstInfo.ItemHeight = 12;
this.lstInfo.Location = new System.Drawing.Point(0, 128);
this.lstInfo.Name = "lstInfo";
this.lstInfo.Size = new System.Drawing.Size(304, 136);
this.lstInfo.TabIndex = 9;
//
// ClientSeverForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(312, 273);
this.Controls.Add(this.lstInfo);
this.Controls.Add(this.label3);
this.Controls.Add(this.CurUserList);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.txtPort);
this.Controls.Add(this.txtHost);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "ClientSeverForm";
this.Text = "ChatServer";
this.Load += new System.EventHandler(this.ClientSeverForm_Load);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new ClientSeverForm());
}
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 ChatServer
{
public class ClientSeverForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox txtHost;
private System.Windows.Forms.TextBox txtPort;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.ComboBox CurUserList;
private System.Windows.Forms.ListBox lstInfo;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
//该服务器默认的监听的端口号
static int port=1234;
private TcpListener listener;
private Socket tmpSocket;
//服务器可以支持的最多的客户端的连接数
static int MaxNum=100;
//clients数组保存当前在线用户的Client对象
static ArrayList clients=new ArrayList(); public ClientSeverForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtHost = new System.Windows.Forms.TextBox();
this.txtPort = new System.Windows.Forms.TextBox();
this.btnStart = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.CurUserList = new System.Windows.Forms.ComboBox();
this.label3 = new System.Windows.Forms.Label();
this.lstInfo = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 16);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "主机号:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label2
//
this.label2.Location = new System.Drawing.Point(0, 48);
this.label2.Name = "label2";
this.label2.TabIndex = 1;
this.label2.Text = "端口号:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// txtHost
//
this.txtHost.Location = new System.Drawing.Point(120, 16);
this.txtHost.Name = "txtHost";
this.txtHost.TabIndex = 2;
this.txtHost.Text = "";
//
// txtPort
//
this.txtPort.Location = new System.Drawing.Point(120, 48);
this.txtPort.Name = "txtPort";
this.txtPort.ReadOnly = true;
this.txtPort.TabIndex = 3;
this.txtPort.Text = "";
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(232, 16);
this.btnStart.Name = "btnStart";
this.btnStart.TabIndex = 4;
this.btnStart.Text = "启动";
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(232, 64);
this.btnExit.Name = "btnExit";
this.btnExit.TabIndex = 5;
this.btnExit.Text = "退出";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// CurUserList
//
this.CurUserList.Location = new System.Drawing.Point(112, 96);
this.CurUserList.Name = "CurUserList";
this.CurUserList.Size = new System.Drawing.Size(121, 20);
this.CurUserList.TabIndex = 6;
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 96);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(96, 23);
this.label3.TabIndex = 8;
this.label3.Text = "当前在线用户:";
//
// lstInfo
//
this.lstInfo.ItemHeight = 12;
this.lstInfo.Location = new System.Drawing.Point(0, 128);
this.lstInfo.Name = "lstInfo";
this.lstInfo.Size = new System.Drawing.Size(304, 136);
this.lstInfo.TabIndex = 9;
//
// ClientSeverForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(312, 273);
this.Controls.Add(this.lstInfo);
this.Controls.Add(this.label3);
this.Controls.Add(this.CurUserList);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.txtPort);
this.Controls.Add(this.txtHost);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "ClientSeverForm";
this.Text = "ChatServer";
this.Load += new System.EventHandler(this.ClientSeverForm_Load);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new ClientSeverForm());
}
下面的程序上接着上面的
private void btnStart_Click(object sender, System.EventArgs e)
{
txtPort.Text=port.ToString();
try
{
IPAddress ipAdd=Dns.Resolve(Dns.GetHostName()).AddressList[0];
txtHost.Text=ipAdd.ToString();
//创建服务器套接字
listener=new TcpListener(ipAdd,port);
//开始监听服务器端口
listener.Start();
lstInfo.Items.Add("服务器已经启动,正在监听"+txtHost.Text+":"+txtPort.Text);
//启动一个新的线程,执行方法this.StartListen,以便在一个独立的进程
//中执行确认与客户端连接的操作
Thread thread=new Thread(new ThreadStart(this.StartListen));
thread.Start();
btnStart.Enabled=false;
}
catch(Exception ex)
{
lstInfo.Items.Add(ex.Message.ToString());
} }
private void StartListen()
{
while(true)
{
try
{
//当接收到一个客户端请求时,确认与客户端的连接
Socket socket=listener.AcceptSocket();
//用tmpSocket保存发出请求的客户端实例
tmpSocket=socket;
if(clients.Count>=MaxNum)
{
tmpSocket.Close();
}
else
{
//启动一个新的线程,执行方法this.ServiceClient,处理用户相应的请求
Thread clientService=new Thread(new ThreadStart(this.ServiceClient));
clientService.Start();
}
}
catch(Exception ex)
{
lstInfo.Items.Add(ex.Message.ToString());
}
}
}
private void ServiceClient()
{
byte[] buff=new byte[1024];
Socket clientSocket=tmpSocket;
bool keepConnect=true;
//用循环来不断地与客户端进行交互,直到客户端发出“EXIT”命令,将keepConnect置为false,退出循环,
//关闭连接,并中止当前线程
while(keepConnect)
{
//接收数据并存入buff数组中
clientSocket.Receive(buff);
//将字符数组转化为字符串
string clientCommand=System.Text.Encoding.ASCII.GetString(buff);
string[] tokens=clientCommand.Split(new Char[]{'|'});
//tokens[0]中保存了命令标志符(CONN或CHAT或PRIV或EXIT)
if(tokens[0]=="CONN")
{
//此时接收到的命令格式为:命令标志符(CONN)|发送者的用户名|,tokens[1]中保存了
//发送者的用户名
Client _client=new Client(tokens[1],clientSocket);
clients.Add(_client);
lstInfo.Items.Add(tokens[1]+" "+"has joined");
//将刚连接的用户名加入到当前在线用户列表中
CurUserList.Items.Add(tokens[1]);
//对每一个当前在线的用户发送JOIN消息命令和LIST消息命令,以此来更新客户端的当前在线
//用户列表
for(int i=0;i<clients.Count;i++)
{
Client client=(Client)clients[i];
//向客户端发送JOIN命令,以此来提示有新的客户进入聊天室
SendToClient(client,"JOIN|"+tokens[1]+"|");
Thread.Sleep(100);
string msgUsers="LIST|"+GetUserList();
//向客户端发送LIST命令,以此来更新客户端的当前在线用户列表
SendToClient(client,msgUsers);
}
}
if(tokens[0]=="CHAT")
{
//此时接收到的命令的格式为:命令标志符(CHAT)|发送者的用户名:发送内容|
//向所有当前在线的用户转发此信息
for(int i=0;i<clients.Count;i++)
{
Client client=(Client)clients[i];
//将“发送者的用户名:发送内容”转发给用户
SendToClient(client,tokens[1]);
}
}
if(tokens[0]=="PRIV")
{
//此时接收到的命令格式为:命令标志符(PRIV)|发送者的用户名|接收者的用户名|发送内容|
//tokens[1]中保存了发送者的用户名
string sender=tokens[1];
//tokens[2]中保存了接收者的用户名
string receiver=tokens[2];
//tokens[3]中保存了发送的内容
string content=tokens[3];
string message=sender+" send to "+receiver+": "+content;
//仅将信息转发给发送者和接收者
for(int i=0;i<clients.Count;i++)
{
Client client=(Client)clients[i];
if(client.Name .CompareTo(tokens[2])==0)
SendToClient(client,message);
if(client.Name.CompareTo(tokens[1])==0)
SendToClient(client,message);
}
}
if(tokens[0]=="EXIT")
{
//此时接收到的命令的格式为:命令标志符(EXIT)|发送者的用户名
//向所有当前在线的用户发送该用户已离开的信息
for(int i=0;i<clients.Count;i++)
{
Client client=(Client)clients[i];
string message=tokens[1]+"has gone!";
SendToClient(client,message);
if(client.Name.CompareTo(tokens[1])==0)
{
//将该用户对应的Client对象从clients数组中删除
clients.RemoveAt(i);
//将该用户名从当前在线用户列表中删除
CurUserList.Items.Remove(client.Name);
//向客户端发送QUIT命令,以此来关闭客户端程序
message="QUIT|";
SendToClient(client,message);
}
}
//向所有当前在线用户发送LIST命令,以此来更新客户端的当前在线用户列表
for(int i=0;i<clients.Count;i++)
{
Client client=(Client)clients[i];
string message="LIST|"+GetUserList();
SendToClient(client,message);
}
lstInfo.Items.Add(tokens[1]+"has gone!");
//断开与该用户的连接
clientSocket.Close();
keepConnect=false;
}
}
}
private void SendToClient(Client client,string msg)
{
System.Byte[] message=System.Text.Encoding.ASCII.GetBytes(msg.ToCharArray());
client.clientSocket.Send(message,message.Length,0);
}
private string GetUserList()
{
string Rtn="";
for(int i=0;i<clients.Count;i++)
{
Client client=(Client)clients[i];
Rtn=Rtn+client.Name+"|";
}
return Rtn;
}
public class Client
{
string name;
Socket clSocket;
public Client(string _name,Socket _socket)
{
name=_name;
clSocket=_socket;
}
public string Name
{
get
{
return name;
}
set
{
name=value;
}
}
public Socket clientSocket
{
get
{
return clSocket;
}
set
{
clSocket=value;
}
}
}
跟后台代码没太大关系
虽然后台也可以 不过没必要
在ASPX也的头上 PAGE节指定ENCODING=UTF8
ref:http://www.dormforce.net/Blog/steven/articles/5506.aspx
System.Text.Encoding.ASCII.GetBytes
的代码改为
System .Text .Encoding .UTF8.GetBytes
试下吧
哪位高手在,赶快回复我,我就结贴送分 了
给分把完整的发送下载邮件代码 包你成功我调试通过了
记得给分