FrmTcpClient:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets;
using System.Text;
namespace TcpClient_ZT
{
public class FrmTcpClient : System.Windows.Forms.Form
{
private const int PORT = 7777;
private const string HOST_HOME = "127.0.0.1";
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.TextBox txtContent;
private System.Windows.Forms.Button btnSend;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public FrmTcpClient()
{
//
// 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.btnSend = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.txtContent = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// btnSend
//
this.btnSend.Location = new System.Drawing.Point(360, 4);
this.btnSend.Name = "btnSend";
this.btnSend.Size = new System.Drawing.Size(136, 23);
this.btnSend.TabIndex = 0;
this.btnSend.Text = "发送";
this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(8, 32);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(488, 232);
this.richTextBox1.TabIndex = 1;
this.richTextBox1.Text = "";
//
// txtContent
//
this.txtContent.Location = new System.Drawing.Point(8, 6);
this.txtContent.Name = "txtContent";
this.txtContent.Size = new System.Drawing.Size(344, 21);
this.txtContent.TabIndex = 2;
this.txtContent.Text = "";
//
// FrmTcpClient
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(504, 269);
this.Controls.Add(this.txtContent);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.btnSend);
this.Name = "FrmTcpClient";
this.Text = "TcpClient";
this.Load += new System.EventHandler(this.FrmTcpClient_Load);
this.ResumeLayout(false);
}
#endregion
private void FrmTcpClient_Load(object sender, System.EventArgs e)
{
}
private void btnSend_Click(object sender, System.EventArgs e)
{
TcpClient client = new TcpClient(HOST_HOME,PORT);
NetworkStream stream = client.GetStream();
byte[] data = UTF8Encoding.UTF8.GetBytes(txtContent.Text);
stream.Write(data,0,data.Length);
string responseData = String.Empty;
byte[] dataReturn = new byte[1024];
int numberOfBytesRead = 0;
do
{
numberOfBytesRead = stream.Read(dataReturn, 0, dataReturn.Length);
responseData = String.Concat(responseData, Encoding.ASCII.GetString(dataReturn, 0, numberOfBytesRead));
}
while(stream.DataAvailable);
this.richTextBox1.Text += responseData+"["+DateTime.Now.ToString()+"]"+"\r\n";
client.Close();
}
}
}
FrmTcpServer:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Text;
using System.Net;
using System.Threading;
namespace TcpClient_ZT
{
/// <summary>
/// FrmTcpServer 的摘要说明。
/// </summary>
public class FrmTcpServer : System.Windows.Forms.Form
{
private const string HOST_HOME = "127.0.0.1";
private const int PORT = 7777;
private System.Windows.Forms.RichTextBox richTextBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public FrmTcpServer()
{
//
// 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.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(3, 5);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(560, 272);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// FrmTcpServer
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(568, 277);
this.Controls.Add(this.richTextBox1);
this.Name = "FrmTcpServer";
this.Text = "FrmTcpServer";
this.Load += new System.EventHandler(this.FrmTcpServer_Load);
this.ResumeLayout(false);
}
#endregion
private void FrmTcpServer_Load(object sender, System.EventArgs e)
{
Thread threadServer = new Thread(new ThreadStart(receiveData));
threadServer.IsBackground = true;
threadServer.Start();
}
private void receiveData()
{
bool done = false;
TcpListener listener = new TcpListener(Dns.Resolve(HOST_HOME).AddressList[0],PORT);
listener.Start();
while(!done)
{
TcpClient client = listener.AcceptTcpClient();
NetworkStream stream = client.GetStream();
byte[] data = new byte[1024];
string result = string.Empty;
int numberOfBytesRead = 0;
do
{
numberOfBytesRead = stream.Read(data, 0, data.Length);
result = String.Concat(result, Encoding.ASCII.GetString(data, 0, numberOfBytesRead));
}
while(stream.DataAvailable);
this.richTextBox1.Text += result;
byte[] bytes = UTF8Encoding.UTF8.GetBytes("Server "+Dns.Resolve(System.Environment.MachineName).AddressList[0].ToString()+" already receive data.["+result+"]");
stream.Write(bytes,0,bytes.Length);
client.Close();
stream.Close();
}
}
}
}
浙公网安备 33010602011771号