K
kals
Unregistered / Unconfirmed
GUEST, unregistred user!
如果各位大富翁有反应的话以后陆续上贴!
一个点对点聊天程序,VS.NET 1.0调试通过
服务器端
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 test131
{
/// <summary>
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
/// <summary>
/// </summary>
private System.ComponentModel.Container components = null;
private TcpListener listener;
private Socket socket;
private NetworkStream netStream;
private NetworkStream netStreamWriter;
private string hostname;
private string hostip;
private IPHostEntry host;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Panel panel3;
private System.Windows.Forms.RichTextBox richTextBox2;
public Form1()
{
InitializeComponent();
hostname =Dns.GetHostName();
host =new IPHostEntry();
host =Dns.Resolve(hostname);
IPAddress hostIP =new IPAddress(host.AddressList[0].Address);
hostip =hostIP.ToString();
}
/// <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>
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.panel2 = new System.Windows.Forms.Panel();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.panel3 = new System.Windows.Forms.Panel();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.panel3.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {this.button3,this.button2, this.button1});
this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panel1.Location = new System.Drawing.Point(0, 233);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(292, 40);
this.panel1.TabIndex = 0;
//
// button3
//
this.button3.Location = new System.Drawing.Point(200, 8);
this.button3.Name = "button3";
this.button3.TabIndex = 2;
this.button3.Text = "停止";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(104, 8);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "发送";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 8);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "监听";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// panel2
//
this.panel2.Controls.AddRange(new System.Windows.Forms.Control[] {this.richTextBox1});
this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panel2.Location = new System.Drawing.Point(0, 133);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(292, 100);
this.panel2.TabIndex = 1;
//
// richTextBox1
//
this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(292, 100);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// panel3
//
this.panel3.Controls.AddRange(new System.Windows.Forms.Control[] {this.richTextBox2});
this.panel3.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel3.Name = "panel3";
this.panel3.Size = new System.Drawing.Size(292, 133);
this.panel3.TabIndex = 2;
//
// richTextBox2
//
this.richTextBox2.Dock = System.Windows.Forms.DockStyle.Fill;
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(292, 133);
this.richTextBox2.TabIndex = 0;
this.richTextBox2.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.panel3,this.panel2,this.panel1});
this.Name = "Form1";
this.Text = "Form1";
this.panel1.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.panel3.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
listener =new TcpListener(8080);
listener.Start();
Thread thread =new Thread(new ThreadStart(rec));
thread.Start();
}
private void rec()
{
while(true)
{
socket =listener.AcceptSocket();
if (socket.Connected)
{
Thread thread =new Thread(new ThreadStart(read));
this.Text ="客户端已连接";
thread.Start();
}
}
}
private void read()
{
while(true)
{
byte[] msgByte =new byte[64];
netStream =new NetworkStream(socket);
netStream.Read(msgByte,0,msgByte.Length);
string msgstr =System.Text.Encoding.BigEndianUnicode.GetString(msgByte);
this.richTextBox2.AppendText(msgstr+"/r/n");
}
}
private void button3_Click(object sender, System.EventArgs e)
{
try
{
socket.Close();
listener.Stop();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button2_Click(object sender, System.EventArgs e)
{
byte[] msgByte =new byte[64];
string msgstr =hostname+"("+hostip+")说:"+this.richTextBox1.Text+"/r/n";
msgByte =System.Text.Encoding.BigEndianUnicode.GetBytes(msgstr.ToCharArray());
netStreamWriter =new NetworkStream(socket);
netStreamWriter.Write(msgByte,0,msgByte.Length);
netStreamWriter.Flush();
}
}
}
一个点对点聊天程序,VS.NET 1.0调试通过
服务器端
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 test131
{
/// <summary>
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
/// <summary>
/// </summary>
private System.ComponentModel.Container components = null;
private TcpListener listener;
private Socket socket;
private NetworkStream netStream;
private NetworkStream netStreamWriter;
private string hostname;
private string hostip;
private IPHostEntry host;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Panel panel3;
private System.Windows.Forms.RichTextBox richTextBox2;
public Form1()
{
InitializeComponent();
hostname =Dns.GetHostName();
host =new IPHostEntry();
host =Dns.Resolve(hostname);
IPAddress hostIP =new IPAddress(host.AddressList[0].Address);
hostip =hostIP.ToString();
}
/// <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>
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.panel2 = new System.Windows.Forms.Panel();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.panel3 = new System.Windows.Forms.Panel();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.panel3.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {this.button3,this.button2, this.button1});
this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panel1.Location = new System.Drawing.Point(0, 233);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(292, 40);
this.panel1.TabIndex = 0;
//
// button3
//
this.button3.Location = new System.Drawing.Point(200, 8);
this.button3.Name = "button3";
this.button3.TabIndex = 2;
this.button3.Text = "停止";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(104, 8);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "发送";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 8);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "监听";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// panel2
//
this.panel2.Controls.AddRange(new System.Windows.Forms.Control[] {this.richTextBox1});
this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panel2.Location = new System.Drawing.Point(0, 133);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(292, 100);
this.panel2.TabIndex = 1;
//
// richTextBox1
//
this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(292, 100);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// panel3
//
this.panel3.Controls.AddRange(new System.Windows.Forms.Control[] {this.richTextBox2});
this.panel3.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel3.Name = "panel3";
this.panel3.Size = new System.Drawing.Size(292, 133);
this.panel3.TabIndex = 2;
//
// richTextBox2
//
this.richTextBox2.Dock = System.Windows.Forms.DockStyle.Fill;
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(292, 133);
this.richTextBox2.TabIndex = 0;
this.richTextBox2.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.panel3,this.panel2,this.panel1});
this.Name = "Form1";
this.Text = "Form1";
this.panel1.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.panel3.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
listener =new TcpListener(8080);
listener.Start();
Thread thread =new Thread(new ThreadStart(rec));
thread.Start();
}
private void rec()
{
while(true)
{
socket =listener.AcceptSocket();
if (socket.Connected)
{
Thread thread =new Thread(new ThreadStart(read));
this.Text ="客户端已连接";
thread.Start();
}
}
}
private void read()
{
while(true)
{
byte[] msgByte =new byte[64];
netStream =new NetworkStream(socket);
netStream.Read(msgByte,0,msgByte.Length);
string msgstr =System.Text.Encoding.BigEndianUnicode.GetString(msgByte);
this.richTextBox2.AppendText(msgstr+"/r/n");
}
}
private void button3_Click(object sender, System.EventArgs e)
{
try
{
socket.Close();
listener.Stop();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button2_Click(object sender, System.EventArgs e)
{
byte[] msgByte =new byte[64];
string msgstr =hostname+"("+hostip+")说:"+this.richTextBox1.Text+"/r/n";
msgByte =System.Text.Encoding.BigEndianUnicode.GetBytes(msgstr.ToCharArray());
netStreamWriter =new NetworkStream(socket);
netStreamWriter.Write(msgByte,0,msgByte.Length);
netStreamWriter.Flush();
}
}
}