客户端:
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 clienttcp
{
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.RichTextBox richTextBox1;
private TcpClient client;
private bool control= false;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.Button button3;
private NetworkStream stream;
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.statusBar1 = new System.Windows.Forms.StatusBar();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.label5 = new System.Windows.Forms.Label();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 351);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(368, 22);
this.statusBar1.TabIndex = 9;
//
// button2
//
this.button2.Location = new System.Drawing.Point(216, 320);
this.button2.Name = "button2";
this.button2.TabIndex = 8;
this.button2.Text = "断开连接";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 320);
this.button1.Name = "button1";
this.button1.TabIndex = 7;
this.button1.Text = "连接";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(104, 16);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 6;
this.textBox1.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 16);
this.label1.TabIndex = 5;
this.label1.Text = "服务器名称";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 112);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 16);
this.label2.TabIndex = 10;
this.label2.Text = "端口";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(104, 104);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 11;
this.textBox2.Text = "";
//
// label3
//
this.label3.Location = new System.Drawing.Point(16, 56);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(72, 23);
this.label3.TabIndex = 12;
this.label3.Text = "服务器IP";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(104, 56);
this.textBox3.Name = "textBox3";
this.textBox3.TabIndex = 13;
this.textBox3.Text = "";
//
// label4
//
this.label4.Location = new System.Drawing.Point(16, 152);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(80, 23);
this.label4.TabIndex = 14;
this.label4.Text = "接受信息";
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(104, 136);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(232, 72);
this.richTextBox1.TabIndex = 15;
this.richTextBox1.Text = "";
//
// label5
//
this.label5.Location = new System.Drawing.Point(16, 232);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(80, 23);
this.label5.TabIndex = 16;
this.label5.Text = "发送信息";
//
// richTextBox2
//
this.richTextBox2.Location = new System.Drawing.Point(104, 224);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(232, 80);
this.richTextBox2.TabIndex = 17;
this.richTextBox2.Text = "";
//
// button3
//
this.button3.Location = new System.Drawing.Point(128, 320);
this.button3.Name = "button3";
this.button3.TabIndex = 18;
this.button3.Text = "发送";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(368, 373);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button3,
this.richTextBox2,
this.label5,
this.richTextBox1,
this.label4,
this.textBox3,
this.label3,
this.textBox2,
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)
{
//new TcpClient()
}
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 clienttcp
{
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.RichTextBox richTextBox1;
private TcpClient client;
private bool control= false;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.Button button3;
private NetworkStream stream;
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.statusBar1 = new System.Windows.Forms.StatusBar();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.label5 = new System.Windows.Forms.Label();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 351);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(368, 22);
this.statusBar1.TabIndex = 9;
//
// button2
//
this.button2.Location = new System.Drawing.Point(216, 320);
this.button2.Name = "button2";
this.button2.TabIndex = 8;
this.button2.Text = "断开连接";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 320);
this.button1.Name = "button1";
this.button1.TabIndex = 7;
this.button1.Text = "连接";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(104, 16);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 6;
this.textBox1.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 16);
this.label1.TabIndex = 5;
this.label1.Text = "服务器名称";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 112);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 16);
this.label2.TabIndex = 10;
this.label2.Text = "端口";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(104, 104);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 11;
this.textBox2.Text = "";
//
// label3
//
this.label3.Location = new System.Drawing.Point(16, 56);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(72, 23);
this.label3.TabIndex = 12;
this.label3.Text = "服务器IP";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(104, 56);
this.textBox3.Name = "textBox3";
this.textBox3.TabIndex = 13;
this.textBox3.Text = "";
//
// label4
//
this.label4.Location = new System.Drawing.Point(16, 152);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(80, 23);
this.label4.TabIndex = 14;
this.label4.Text = "接受信息";
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(104, 136);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(232, 72);
this.richTextBox1.TabIndex = 15;
this.richTextBox1.Text = "";
//
// label5
//
this.label5.Location = new System.Drawing.Point(16, 232);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(80, 23);
this.label5.TabIndex = 16;
this.label5.Text = "发送信息";
//
// richTextBox2
//
this.richTextBox2.Location = new System.Drawing.Point(104, 224);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(232, 80);
this.richTextBox2.TabIndex = 17;
this.richTextBox2.Text = "";
//
// button3
//
this.button3.Location = new System.Drawing.Point(128, 320);
this.button3.Name = "button3";
this.button3.TabIndex = 18;
this.button3.Text = "发送";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(368, 373);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button3,
this.richTextBox2,
this.label5,
this.richTextBox1,
this.label4,
this.textBox3,
this.label3,
this.textBox2,
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)
{
//new TcpClient()
}
private void button1_Click(object sender, System.EventArgs e)
{
int port=0;
client=new TcpClient();
IPAddress myIP=IPAddress.Parse("127.0.0.1");
try
{
myIP=IPAddress.Parse(textBox2.Text); }
catch
{
MessageBox.Show ("请输入正确的ip格式!"); }
try
{
port=Int32.Parse(textBox2.Text); }
catch
{
MessageBox.Show ("请输入整数!"); }
try
{
if(textBox3.Text!="")
{
client.Connect(textBox3.Text,port );
statusBar1.Text ="与服务器建立连接关系!";
Thread thread1=new Thread(new ThreadStart(target));
thread1.Start();
}
else if(textBox1.Text!="")
{
client.Connect(textBox1.Text.Trim() ,port );
statusBar1.Text ="与服务器建立连接关系!";
Thread thread1=new Thread(new ThreadStart(target));
thread1.Start();
}
}
catch(Exception ee)
{
MessageBox.Show (ee.Message ); }
}
private void target()
{
while(!control)
{
stream=client.GetStream();
byte[] fromclient=new byte[640];
int i=stream.Read(fromclient,0,640);
string str=System.Text.Encoding.BigEndianUnicode.GetString(fromclient);
richTextBox1.AppendText(str+"\r\n");
} } private void button2_Click(object sender, System.EventArgs e)
{
try
{
client.Close();
statusBar1.Text ="与服务器建立断开连接关系!";
}
catch(Exception ee)
{
MessageBox.Show ("连接没有开始,关闭无效"+ee.Message ); }
}
private void button3_Click(object sender, System.EventArgs e)
{
NetworkStream fromclient=client.GetStream();
string flush=richTextBox2.Text +"\r\n";
byte[] by=System.Text.Encoding.BigEndianUnicode.GetBytes(flush.ToCharArray());
fromclient.Write(by,0,by.Length );
fromclient.Flush();
}
}
}
这是地址:(自己对自己也可以:P)www.eastagri.com/temp/ptp.zip
使用有问题,界面作得不错我刚学网络编程,大家多多指点。
可能的话,希望学习一下;[email protected]
有什么问题呀?我想知道呢!改进改进~~~~我也是刚刚学C#才4天,很多都不知道,大家以后多帮帮我哦!