服务器端软件:
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.IO;
using
System.Text;
using
System.Threading;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
namespace
TalkServer
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 服务器端软件。
/// </summary>
public class Form1 : System.Windows.Forms.Form
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.RichTextBox richTextBox2;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
![](/Images/OutliningIndicators/InBlock.gif)
public Form1()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
![](/Images/OutliningIndicators/InBlock.gif)
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if( disposing )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (components != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Windows Form Designer generated code#region Windows Form Designer generated code
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button1 = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.groupBox1.SuspendLayout();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[]
{
this.button1});
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(450, 56);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = " 服务器 ";
//
// button1
//
this.button1.Location = new System.Drawing.Point(176, 24);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "启动";
this.button1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// panel1
//
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.panel1.Controls.AddRange(new System.Windows.Forms.Control[]
{
this.richTextBox2,
this.richTextBox1});
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(0, 56);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(450, 266);
this.panel1.TabIndex = 1;
//
// richTextBox2
//
this.richTextBox2.Dock = System.Windows.Forms.DockStyle.Fill;
this.richTextBox2.Location = new System.Drawing.Point(0, 208);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedBoth;
this.richTextBox2.Size = new System.Drawing.Size(450, 58);
this.richTextBox2.TabIndex = 0;
this.richTextBox2.Text = "";
this.richTextBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.richTextBox2_KeyPress);
//
// richTextBox1
//
this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.ReadOnly = true;
this.richTextBox1.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedBoth;
this.richTextBox1.Size = new System.Drawing.Size(450, 208);
this.richTextBox1.TabIndex = 10;
this.richTextBox1.TabStop = false;
this.richTextBox1.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
this.ClientSize = new System.Drawing.Size(450, 322);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.Controls.AddRange(new System.Windows.Forms.Control[]
{
this.panel1,
this.groupBox1});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
![](/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Application.Run(new Form1());
}
// 定义私有变量。
public bool m_bConnected = false;
public IPEndPoint p = null;
public Socket s = null;
public NetworkStream ns = null;
public TextReader r = null;
public TextWriter w = null;
public Thread t = null;
public void ThreadListen()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Socket recv = null;
recv = s.Accept();
if(recv != null)
m_bConnected = true;
ns = new NetworkStream(recv);
r = new StreamReader(ns);
w = new StreamWriter(ns);
while(m_bConnected)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string tmp;
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tmp = r.ReadLine();
if(tmp.Length != 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lock(this)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
richTextBox1.Text = "他说:"+tmp+"\n"+richTextBox1.Text;
}
}
}
catch
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show("连接断开!!");
}
}
recv.Shutdown(SocketShutdown.Both);
recv.Close();
s.Shutdown(SocketShutdown.Both);
s.Close();
}
private void button1_Click(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
p = new IPEndPoint(IPAddress.Any, 34567);
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Blocking = true;
s.Bind(p);
s.Listen(0);
t = new Thread(new ThreadStart(this.ThreadListen));
t.Start();
button1.Enabled = false;
}
private void richTextBox2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
byte[] data;
if(e.KeyChar == (char)13)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(m_bConnected)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lock(this)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
richTextBox1.Text = "我说:"+richTextBox2.Text+richTextBox1.Text;
w.WriteLine(richTextBox2.Text);
w.Flush();
}
}
catch
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show("连接断开!!");
}
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show("对不起,还没有与对方连接,不能通信!");
}
richTextBox2.Text = "";
richTextBox2.Focus();
}
}
}
}
客户端软件:
using
System;
using
System.Drawing;
using
System.Collections;
using
System.ComponentModel;
using
System.Windows.Forms;
using
System.Data;
//
添加新的命名空间。
using
System.IO;
using
System.Text;
using
System.Net;
using
System.Net.Sockets;
using
System.Threading;
![](/Images/OutliningIndicators/None.gif)
namespace
TalkClient
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 客户端软件。
/// </summary>
public class Form1 : System.Windows.Forms.Form
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.RichTextBox richTextBox2;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
![](/Images/OutliningIndicators/InBlock.gif)
public Form1()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Windows 窗体设计器支持所必需的
InitializeComponent();
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if( disposing )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (components != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Windows Form Designer generated code#region Windows Form Designer generated code
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button1 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.panel1 = new System.Windows.Forms.Panel();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.groupBox1.SuspendLayout();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[]
{
this.button1,
this.label2,
this.label1,
this.textBox2,
this.textBox1});
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(440, 48);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = " 客户端 ";
//
// button1
//
this.button1.Location = new System.Drawing.Point(328, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 23);
this.button1.TabIndex = 4;
this.button1.Text = "连接";
this.button1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(216, 24);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(44, 18);
this.label2.TabIndex = 3;
this.label2.Text = "端口:";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(8, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(75, 18);
this.label1.TabIndex = 2;
this.label1.Text = "服务器IP:";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(264, 16);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(48, 25);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "34567";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(88, 17);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(120, 25);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "127.0.0.1";
//
// panel1
//
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.panel1.Controls.AddRange(new System.Windows.Forms.Control[]
{
this.richTextBox2,
this.richTextBox1});
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(0, 48);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(440, 280);
this.panel1.TabIndex = 1;
//
// richTextBox2
//
this.richTextBox2.Dock = System.Windows.Forms.DockStyle.Fill;
this.richTextBox2.Location = new System.Drawing.Point(0, 224);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedBoth;
this.richTextBox2.Size = new System.Drawing.Size(440, 56);
this.richTextBox2.TabIndex = 0;
this.richTextBox2.Text = "";
this.richTextBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.richTextBox2_KeyPress);
//
// richTextBox1
//
this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.ReadOnly = true;
this.richTextBox1.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedBoth;
this.richTextBox1.Size = new System.Drawing.Size(440, 224);
this.richTextBox1.TabIndex = 10;
this.richTextBox1.TabStop = false;
this.richTextBox1.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
this.ClientSize = new System.Drawing.Size(440, 328);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.Controls.AddRange(new System.Windows.Forms.Control[]
{
this.panel1,
this.groupBox1});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
![](/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Application.Run(new Form1());
}
// 定义私有变量。
public bool m_bConnected = false;
public Thread t = null;
public IPEndPoint p = null;
public Socket s = null;
public NetworkStream ns = null;
public TextReader r = null;
public TextWriter w = null;
public void ThreadListen()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string tmp;
while(m_bConnected)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tmp = r.ReadLine();
if(tmp.Length != 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lock(this)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
richTextBox1.Text = "他说:"+tmp+"\n"+richTextBox1.Text;
}
}
}
catch
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show("连接断开!!");
}
}
s.Shutdown(SocketShutdown.Both);
s.Close();
}
private void button1_Click(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
p = new IPEndPoint(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text));
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(p);
if(s.Connected)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ns = new NetworkStream(s);
r = new StreamReader(ns);
w = new StreamWriter(ns);
t = new Thread(new ThreadStart(this.ThreadListen));
t.Start();
m_bConnected = true;
button1.Enabled = false;
}
}
private void richTextBox2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(e.KeyChar == (char)13)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(m_bConnected)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lock(this)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
richTextBox1.Text = "我说:"+richTextBox2.Text+richTextBox1.Text;
w.WriteLine(richTextBox2.Text);
w.Flush();
}
catch
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show("连接断开!!");
}
}
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show("对不起,还没有与对方连接,不能通信!");
}
richTextBox2.Text = "";
richTextBox2.Focus();
}
}
}
}
转载于:https://www.cnblogs.com/gofficer/archive/2007/10/09/918219.html