C#学习进阶一(网络-TCP实例) (0分)

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();
}
}
}
 
客户端:
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 test132
{
/// <summary>
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.RichTextBox richTextBox1;
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 string hostip;
private string hostname;
private IPEndPoint server;
private IPHostEntry ff;
private TcpClient client;
private System.Windows.Forms.RichTextBox richTextBox2;
private NetworkStream netStream;
public Form1()
{
InitializeComponent();
hostname =Dns.GetHostName();
ff =Dns.Resolve(hostname);
hostip =ff.AddressList[0].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.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.richTextBox1 = 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.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(104, 16);
this.label1.TabIndex = 0;
this.label1.Text = "主机地址:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(72, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(152, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "192.168.122.13";
//
// label2
//
this.label2.Location = new System.Drawing.Point(232, 16);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(48, 23);
this.label2.TabIndex = 2;
this.label2.Text = "端口:";
this.label2.Click += new System.EventHandler(this.label2_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(272, 8);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(80, 21);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "8080";
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(8, 144);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(344, 88);
this.richTextBox1.TabIndex = 4;
this.richTextBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 240);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(80, 24);
this.button1.TabIndex = 5;
this.button1.Text = "请求连接";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(144, 240);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(80, 24);
this.button2.TabIndex = 6;
this.button2.Text = "发送";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(264, 240);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(80, 24);
this.button3.TabIndex = 7;
this.button3.Text = "断开连接";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// richTextBox2
//
this.richTextBox2.Location = new System.Drawing.Point(8, 40);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(344, 96);
this.richTextBox2.TabIndex = 8;
this.richTextBox2.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(360, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.richTextBox2,
this.button3,
this.button2,
this.button1,
this.richTextBox1,
this.textBox2,
this.label2,
this.textBox1,
this.label1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void label2_Click(object sender, System.EventArgs e)
{

}
private void button1_Click(object sender, System.EventArgs e)
{
try
{
server =new IPEndPoint(IPAddress.Parse(this.textBox1.Text),Int32.Parse(this.textBox2.Text));
client =new TcpClient();
client.Connect(server);
this.Text ="连接到服务器:"+server.Address.ToString();
Thread thread =new Thread(new ThreadStart(rec));
thread.Start();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void rec()
{
while(true)
{
netStream =client.GetStream();
byte[] msgByte =new byte[64];
netStream.Read(msgByte,0,msgByte.Length);
string msgstr =System.Text.Encoding.BigEndianUnicode.GetString(msgByte);
this.richTextBox2.AppendText(msgstr+"/r/n");
}
}
private void button2_Click(object sender, System.EventArgs e)
{
netStream =client.GetStream();
string send =this.hostname+"("+this.hostip+")说:"+this.richTextBox1.Text;
byte[] msgByte =System.Text.Encoding.BigEndianUnicode.GetBytes(send.
ToCharArray());
netStream.Write(msgByte,0,msgByte.Length);
netStream.Flush();
}
private void button3_Click(object sender, System.EventArgs e)
{
try
{
client.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
 
炫耀吗?
好像没有delphi的源程序好看
 
呵呵,小虫子,对不起,不是炫耀,是看见大富翁上的C#论坛太冷清了,希望有多几个
朋友可以来大富翁c#论坛凑凑热闹!
 
接受答案了.
 
顶部