这是我作的,本来想实现点对点的聊天程序, 只实现了简单的消息发送,贴出来,请大家指正!!
服务器端: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 Tcp编程
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private TcpListener listener;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button button3;
private int port;
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.Label label4;
private Socket fromrequest;
//private TcpListener listener;
private bool control ;
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 Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.label2 = new System.Windows.Forms.Label();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.button3 = new System.Windows.Forms.Button();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.label4 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 16);
this.label1.TabIndex = 0;
this.label1.Text = "监听端口";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(104, 32);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(136, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 280);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "开始监听";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(192, 280);
this.button2.Name = "button2";
this.button2.TabIndex = 3;
this.button2.Text = "停止监听";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 327);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(392, 22);
this.statusBar1.TabIndex = 4;
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 168);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 16);
this.label2.TabIndex = 5;
this.label2.Text = "发送消息";
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(104, 160);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(232, 96);
this.richTextBox1.TabIndex = 6;
this.richTextBox1.Text = "";
//
// button3
//
this.button3.Location = new System.Drawing.Point(112, 280);
this.button3.Name = "button3";
this.button3.TabIndex = 7;
this.button3.Text = "发送消息";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// richTextBox2
//
this.richTextBox2.Location = new System.Drawing.Point(104, 72);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(232, 72);
this.richTextBox2.TabIndex = 17;
this.richTextBox2.Text = "";
//
// label4
//
this.label4.Location = new System.Drawing.Point(24, 80);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(72, 23);
this.label4.TabIndex = 16;
this.label4.Text = "接受信息";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(392, 349);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.richTextBox2,
this.label4,
this.button3,
this.richTextBox1,
this.label2,
this.statusBar1,
this.button2,
this.button1,
this.textBox1,
this.label1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void button1_Click(object sender, System.EventArgs e)
{
try
{
port=Int32.Parse(textBox1.Text );
if(textBox1.Text==null)
throw(new ArgumentNullException());
}
catch
{
MessageBox.Show ("你的输入格式不对,请从新输入!");
}
try
{
listener= new TcpListener(port);
listener.Start();
statusBar1.Text="开始监听-----";
// sock=listener.AcceptSocket();
// if(sock.Connected )
// {
// statusBar1.Text="与客户建立连接!!";
// }
Thread thread1=new Thread(new ThreadStart(targetstart));
thread1.Start();
}
catch(Exception ee)
{
MessageBox.Show (ee.Message );
}
}
private void targetstart()
{
fromrequest=listener.AcceptSocket();
if(fromrequest.Connected )
{
statusBar1.Text="与客户建立连接!!";
while(!control)
{
byte[] by=new byte[640];
int i=fromrequest.Receive(by,by.Length,System.Net.Sockets.SocketFlags.None);
string ss=System.Text.Encoding.BigEndianUnicode.GetString(by);
richTextBox2.AppendText(ss+"\r\n");
}
}
}
private void button2_Click(object sender, System.EventArgs e)
{
try
{
control=true;
listener.Stop();
statusBar1.Text ="停止监台听!---";
}
catch(Exception ee)
{
MessageBox.Show ("监听没有开始,关闭无效"+ee.Message );
}
}
private void button3_Click(object sender, System.EventArgs e)
{
string str=richTextBox1.Text +"\r\n";
byte[] sendclient=System.Text.Encoding.BigEndianUnicode.GetBytes(str);
fromrequest.Send(sendclient,sendclient.Length,SocketFlags.None);//(sendclient,sendclient.Length ,0);
}
}
}