p2p程序,在我的本机上中文字体发送正常,可用两台局狱网上的机器同时运行通信是,却互相收到的是乱码.为什么?.......................
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;using System.Threading;
using System.Net.Sockets;
using System.IO;namespace qq
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.GroupBox groupBox1; private Thread listenThread;
private bool listen=false;
private TcpListener tcpListener;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Button button4; /// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.label3 = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.button4 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
// 
// textBox1
// 
this.textBox1.Location = new System.Drawing.Point(104, 16);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(120, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "192.168.0.2";
// 
// textBox2
// 
this.textBox2.Location = new System.Drawing.Point(16, 32);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(80, 21);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "兵哥哥";
// 
// richTextBox1
// 
this.richTextBox1.Location = new System.Drawing.Point(176, 256);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(352, 24);
this.richTextBox1.TabIndex = 2;
this.richTextBox1.Text = "你好";
// 
// richTextBox2
// 
this.richTextBox2.BackColor = System.Drawing.SystemColors.Info;
this.richTextBox2.Location = new System.Drawing.Point(16, 64);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(544, 160);
this.richTextBox2.TabIndex = 3;
this.richTextBox2.Text = "";
// 
// label1
// 
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 23);
this.label1.TabIndex = 4;
this.label1.Text = "目标地址IP";
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(264, 16);
this.button1.Name = "button1";
this.button1.TabIndex = 5;
this.button1.Text = "开始监听";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// button2
// 
this.button2.Location = new System.Drawing.Point(376, 16);
this.button2.Name = "button2";
this.button2.TabIndex = 6;
this.button2.Text = "停止监听";
this.button2.Click += new System.EventHandler(this.button2_Click);
// 
// button3
// 
this.button3.Location = new System.Drawing.Point(568, 256);
this.button3.Name = "button3";
this.button3.TabIndex = 7;
this.button3.Text = "发送";
this.button3.Click += new System.EventHandler(this.button3_Click);
// 
// statusBar1
// 
this.statusBar1.Location = new System.Drawing.Point(0, 327);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(656, 22);
this.statusBar1.TabIndex = 9;
this.statusBar1.Text = "statusBar1";
// 
// label3
// 
this.label3.Location = new System.Drawing.Point(120, 32);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(40, 23);
this.label3.TabIndex = 10;
this.label3.Text = "说";
// 
// groupBox1
// 
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.textBox2);
this.groupBox1.Location = new System.Drawing.Point(8, 224);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(552, 72);
this.groupBox1.TabIndex = 11;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "发言区";
// 
// groupBox2
// 
this.groupBox2.Controls.Add(this.button4);
this.groupBox2.Location = new System.Drawing.Point(8, 0);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(592, 48);
this.groupBox2.TabIndex = 12;
this.groupBox2.TabStop = false;
// 
// button4
// 
this.button4.Location = new System.Drawing.Point(488, 17);
this.button4.Name = "button4";
this.button4.TabIndex = 0;
this.button4.Text = "退出系统";
this.button4.Click += new System.EventHandler(this.button4_Click);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(656, 349);
this.ControlBox = false;
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Controls.Add(this.richTextBox2);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.groupBox2);
this.Name = "Form1";
this.Text = "聊天程序(天大43--127 兵哥哥开发)";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false); }
#endregion

解决方案 »

  1.   

    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e)
    {
    listenThread =new Thread(new ThreadStart(StartListen));
    listenThread.Start();
    } private void StartListen()
    {
    try
    { tcpListener=new TcpListener(8888);
    tcpListener.Start();
    statusBar1.Text="兵哥哥说:正在监听.....,你可以互相聊天了";
    this.listen=true;
    while(listen)
    {
    Socket s=tcpListener.AcceptSocket();
    Byte[] stream=new byte[100];
    int i=s.Receive(stream);
    string message=System.Text.Encoding.UTF8.GetString(stream);
    this.richTextBox2.AppendText(message);
    }
    }
    catch(Exception ex)
    { statusBar1.Text="兵哥哥说:停止监听....";
    }
    } private void button2_Click(object sender, System.EventArgs e)
    {
    if (this.listen)
    {
    this.listen=false;
    this.tcpListener.Stop();
    this.listenThread.Abort();
    }
    statusBar1.Text="兵哥哥说:停止监听....";
    } private void button3_Click(object sender, System.EventArgs e)
    {
    try
    {
    string msg="<"+this.textBox2.Text+"> 说:"+this.richTextBox1.Text+"\n";
    TcpClient tcpClient=new TcpClient(textBox1.Text,8888);
    NetworkStream tcpStream=tcpClient.GetStream();
    StreamWriter streamW=new StreamWriter(tcpStream);
    streamW.Write(msg);
    streamW.Flush();
    tcpStream.Close();
    this.richTextBox2.AppendText(msg);
    //this.richTextBox1.Clear(); }
    catch(Exception ex)
    { statusBar1.Text="兵哥哥说:目标计算机拒绝请求,你是否输入对方的正确IP,并点击了--开始监听--按纽";
    }
    } private void button4_Click(object sender, System.EventArgs e)
    {
    if (this.listen)
    {
    this.listen=false;
    this.tcpListener.Stop();
    this.listenThread.Abort();
    }
    Application.Exit();
    }
    }
    }
      

  2.   

    在 webConfig中加入 <globalization requestEncoding="gb2312" responseEncoding="gb2312"/>
      

  3.   

    用这个构造函数: public StreamWriter(Stream, Encoding); 
    StreamWriter streamW=new StreamWriter(tcpStream);
    改成:StreamWriter streamW=new StreamWriter(tcpStream, Encoding.UTF8);
      

  4.   

    多谢大侠无私帮助,i moved so much!