前几天在网上看到关于使用socket 编写聊天程序的一个例子,学习了一下,网上的例子是VB.NET的,自己改写成了C#的 大同小异,只作为记录 :
发送端
================================
using
System;
using
System.Drawing;
using
System.Collections;
using
System.ComponentModel;
using
System.Windows.Forms;
namespace
CSClientTest
{
///<summary>
/// sendform
的摘要说明。
///</summary>
public class sendform : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
///<summary>
///
必需的设计器变量。
///</summary>
private System.ComponentModel.Container components = null;
public sendform()
{
//
// 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.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(96, 144);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(200, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(320, 144);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "sender";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(608, 112);
this.label1.TabIndex = 2;
//
// sendform
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(616, 238);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "sendform";
this.Text = "sendform";
this.Load += new System.EventHandler(this.sendform_Load);
this.ResumeLayout(false);
}
#endregion
private void sendform_Load(object sender, System.EventArgs e)
{
}
private void button1_Click(object sender, System.EventArgs e)
{
//
定义一个socket对象
System.Net.Sockets.Socket socket= new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,System.Net.Sockets.SocketType.Stream,System.Net.Sockets.ProtocolType.Tcp);
//
定义一个字节数组
byte[] b = new byte[1024];
//
从文本框中获得数据转换为字节数组后存入b
b=System.Text.Encoding.UTF8.GetBytes(textBox1.Text);
//
定义目的端的IP和端口
System.Net.IPEndPoint ep = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"),1022);
try
{
//
连接到目的端
socket.Connect(ep);
//
用socket的send方法发送数据,该方法返回发送的数据的字节数
label1.Text="
数据已发送,总共:"+socket.Send(b).ToString()+"字节";
//
禁止并关闭socket
socket.Shutdown(System.Net.Sockets.SocketShutdown.Both);
socket.Close();
}
catch(System.Exception ex)
{
label1.Text=ex.Message.ToString();
}
}
}
}
接收端
================================
using
System;
using
System.Drawing;
using
System.Collections;
using
System.ComponentModel;
using
System.Windows.Forms;
namespace
CSClientTest
{
///<summary>
/// receive
的摘要说明。
///</summary>
public class receive : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
///<summary>
///
必需的设计器变量。
///</summary>
private System.ComponentModel.Container components = null;
//
定义一个全局的socket以便监听和接收数据
System.Net.Sockets.Socket socket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,System.Net.Sockets.SocketType.Stream,System.Net.Sockets.ProtocolType.Tcp);
public receive()
{
//
// 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.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(448, 184);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "
接受";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(224, 184);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(208, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// receive
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(664, 266);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "receive";
this.Text = "receive";
this.Load += new System.EventHandler(this.receive_Load);
this.ResumeLayout(false);
}
#endregion
private void receive_Load(object sender, System.EventArgs e)
{
//
定义本地接收端IP和端口
System.Net.IPEndPoint ep = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"),1022);
//socket
绑定本地接收端
socket.Bind(ep);
//
监听,监听挂起10
socket.Listen(10);
}
private void button1_Click(object sender, System.EventArgs e)
{
try
{
if(socket.Blocking)
{
//
为新建连接创建新的 Socket
System.Net.Sockets.Socket socket1=socket.Accept();
//
定义一个字节数组
byte[] b = new byte[1024];
//
将接收到的字节数组存入b中
socket1.Receive(b);
//
将B中的数据转换为字符串后显示到textbox
textBox1.Text = System.Text.Encoding.UTF8.GetString(b);
//
禁止并关闭这个新的socket连接
socket1.Shutdown(System.Net.Sockets.SocketShutdown.Both);
socket1.Close();
}
}
catch
{
textBox1.Text = "
无数据";
}
}
}
}