1using System;
2using System.Drawing;
3using System.Collections;
4using System.ComponentModel;
5using System.Windows.Forms;
6using System.Data;
7using System.Net;
8using System.Net.Sockets;
9using System.Threading;
10
11namespace 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 窗体设计器生成的代码
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
2using System.Drawing;
3using System.Collections;
4using System.ComponentModel;
5using System.Windows.Forms;
6using System.Data;
7using System.Net;
8using System.Net.Sockets;
9using System.Threading;
10
11namespace 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 窗体设计器生成的代码
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