1
using System;
2
using System.Drawing;
3
using System.Collections;
4
using System.ComponentModel;
5
using System.Windows.Forms;
6
using System.Data;
7
using System.Net;
8
using System.Net.Sockets;
9
using System.Threading;
10
11
namespace TCLient
12

{
13
/**//// <summary>
14
/// Form1 的摘要说明。
15
/// </summary>
16
public class Form1 : System.Windows.Forms.Form
17
{
18
private System.Windows.Forms.TextBox txt;
19
private System.Windows.Forms.StatusBar statBar;
20
private System.Windows.Forms.TextBox txtIP;
21
private System.Windows.Forms.TextBox sendText;
22
private System.Windows.Forms.Button btnSend;
23
private System.Windows.Forms.Button btnStart;
24
private System.Net.Sockets.Socket client;
25
private byte[] buffer;
26
/**//// <summary>
27
/// 必需的设计器变量。
28
/// </summary>
29
private System.ComponentModel.Container components = null;
30
31
public Form1()
32
{
33
//
34
// Windows 窗体设计器支持所必需的
35
//
36
InitializeComponent();
37
38
//
39
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
40
//
41
}
42
43
/**//// <summary>
44
/// 清理所有正在使用的资源。
45
/// </summary>
46
protected override void Dispose( bool disposing )
47
{
48
if( disposing )
49
{
50
if (components != null)
51
{
52
components.Dispose();
53
}
54
}
55
base.Dispose( disposing );
56
}
57
58
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
59
/**//// <summary>
60
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
61
/// 此方法的内容。
62
/// </summary>
63
private void InitializeComponent()
64
{
65
this.txt = new System.Windows.Forms.TextBox();
66
this.statBar = new System.Windows.Forms.StatusBar();
67
this.txtIP = new System.Windows.Forms.TextBox();
68
this.sendText = new System.Windows.Forms.TextBox();
69
this.btnSend = new System.Windows.Forms.Button();
70
this.btnStart = new System.Windows.Forms.Button();
71
this.SuspendLayout();
72
//
73
// txt
74
//
75
this.txt.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
76
| System.Windows.Forms.AnchorStyles.Left)
77
| System.Windows.Forms.AnchorStyles.Right)));
78
this.txt.Location = new System.Drawing.Point(0, 32);
79
this.txt.Multiline = true;
80
this.txt.Name = "txt";
81
this.txt.Size = new System.Drawing.Size(584, 208);
82
this.txt.TabIndex = 0;
83
this.txt.Text = "";
84
//
85
// statBar
86
//
87
this.statBar.Location = new System.Drawing.Point(0, 244);
88
this.statBar.Name = "statBar";
89
this.statBar.Size = new System.Drawing.Size(584, 22);
90
this.statBar.TabIndex = 1;
91
//
92
// txtIP
93
//
94
this.txtIP.Location = new System.Drawing.Point(8, 8);
95
this.txtIP.Name = "txtIP";
96
this.txtIP.Size = new System.Drawing.Size(96, 21);
97
this.txtIP.TabIndex = 2;
98
this.txtIP.Text = "192.168.0.14";
99
//
100
// sendText
101
//
102
this.sendText.Location = new System.Drawing.Point(104, 8);
103
this.sendText.Name = "sendText";
104
this.sendText.Size = new System.Drawing.Size(272, 21);
105
this.sendText.TabIndex = 3;
106
this.sendText.Text = "";
107
//
108
// btnSend
109
//
110
this.btnSend.Location = new System.Drawing.Point(392, 8);
111
this.btnSend.Name = "btnSend";
112
this.btnSend.TabIndex = 4;
113
this.btnSend.Text = "发送";
114
this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
115
//
116
// btnStart
117
//
118
this.btnStart.Location = new System.Drawing.Point(488, 8);
119
this.btnStart.Name = "btnStart";
120
this.btnStart.TabIndex = 4;
121
this.btnStart.Text = "开始";
122
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
123
//
124
// Form1
125
//
126
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
127
this.ClientSize = new System.Drawing.Size(584, 266);
128
this.Controls.Add(this.btnSend);
129
this.Controls.Add(this.sendText);
130
this.Controls.Add(this.txtIP);
131
this.Controls.Add(this.statBar);
132
this.Controls.Add(this.txt);
133
this.Controls.Add(this.btnStart);
134
this.Name = "Form1";
135
this.Text = "客户端";
136
this.Load += new System.EventHandler(this.Form1_Load);
137
this.ResumeLayout(false);
138
139
}
140
#endregion
141
142
/**//// <summary>
143
/// 应用程序的主入口点。
144
/// </summary>
145
[STAThread]
146
static void Main()
147
{
148
Application.Run(new Form1());
149
}
150
151
private void Form1_Load(object sender, System.EventArgs e)
152
{
153
this.client = new Socket( AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp );
154
this.statBar.Text = "初始化完毕
";
155
this.buffer = new byte[ 1024 ];
156
}
157
158
private void btnStart_Click(object sender, System.EventArgs e)
159
{
160
IPEndPoint ipep = new IPEndPoint( IPAddress.Parse( this.txtIP.Text ),9000 );
161
this.client.BeginConnect( (EndPoint)ipep,new AsyncCallback(this.ConnectCallback ),this.client );
162
}
163
164
private void ConnectCallback( System.IAsyncResult iar )
165
{
166
try
167
{
168
Socket server = (Socket)iar.AsyncState;
169
this.client.EndConnect( iar );
170
this.txt.Text += "\r\n已连接至服务器。";
171
this.statBar.Text = "连接到服务器:" + server.RemoteEndPoint.ToString();
172
this.client.BeginReceive( this.buffer,0,this.buffer.Length,SocketFlags.None,new AsyncCallback( this.ReceiveCallback ),this.client );
173
174
}
175
catch( Exception ex )
176
{
177
this.txt.Text += "\r\n" + ex.ToString();
178
}
179
}
180
181
private void ReceiveCallback( System.IAsyncResult iar )
182
{
183
try
184
{
185
Socket remote = (Socket)iar.AsyncState;
186
int recv = remote.EndReceive( iar );
187
string tmp = System.Text.Encoding.Default.GetString( this.buffer,0,recv );
188
this.txt.Text += "\r\n从服务器端接收到:" + tmp;
189
}
190
catch( Exception ex )
191
{
192
}
193
}
194
195
private void Send()
196
{
197
byte[] tmp = System.Text.Encoding.Default.GetBytes( this.sendText.Text );
198
this.client.BeginSend( tmp,0,tmp.Length,SocketFlags.None,new AsyncCallback(this.SendCallback),this.client );
199
}
200
201
private void SendCallback( System.IAsyncResult iar )
202
{
203
try
204
{
205
Socket remote = (Socket)iar.AsyncState;
206
int s = remote.EndSend( iar );
207
remote.BeginReceive( this.buffer,0,this.buffer.Length,SocketFlags.None,new AsyncCallback(this.ReceiveCallback),remote );
208
}
209
catch( Exception ex )
210
{
211
this.txt.Text += "\r\n" + ex.ToString();
212
}
213
}
214
215
private void btnSend_Click(object sender, System.EventArgs e)
216
{
217
this.Send();
218
}
219
}
220
}
221
posted on 2006-08-24 16:39
王员外 阅读(156)
评论(0) 编辑 收藏