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.Text;
8
using System.Net;
9
using System.Net.Sockets;
10
using System.Threading;
11
12
namespace TServer
13

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