Donet网络编程备课笔记
一 System.Net和System.Net.Socket命名空间介绍
二 WebRequest、WebResponse、WebProxy、HttpWebRequest、HttpWebResponse类介绍
三 举例(见MSDN同步HttpWebRequest示例)
四 TcpClient介绍以及举例(见例2)
五 TcpListener介绍以及举例(见例子3)
六 UdpClient介绍以及举例(见例子4)
七 Socket介绍以及举例(见MSDN同步套接字示例)
八 代码部分:
////////////////
例(2)WinForm程序
////////////////
private void button1_Click(object sender, System.EventArgs e)
{
string server="teachera";
TcpClient tcpclient=new TcpClient();
//连接
tcpclient.Connect("teachera",80);
NetworkStream ns=tcpclient.GetStream();
StreamReader sr=new StreamReader(ns,Encoding.Default);
StreamWriter sw=new StreamWriter(ns);
//发送数据
string s = "GET / HTTP/1.1\r\nHost: " + server +
"\r\nConnection: Close\r\n\r\n";
sw.Write(s);
sw.Flush();
//接收数据
textBox1.Text=sr.ReadToEnd();
tcpclient.Close();
}
////////////////
例(3)客户端 WinForm程序
////////////////
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets;
using System.IO;
using System.Text;
namespace TcpClientTest
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtServer;
private System.Windows.Forms.Button btnConnect;
private System.Windows.Forms.TextBox txtPort2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button btnSend2;
private System.Windows.Forms.TextBox txtSend2;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox txtReceive2;
private System.Windows.Forms.Button btnReceive2;
private System.ComponentModel.Container components = null;
public Form1()
{
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.btnConnect = new System.Windows.Forms.Button();
this.txtPort2 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtServer = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.txtSend2 = new System.Windows.Forms.TextBox();
this.btnSend2 = new System.Windows.Forms.Button();
this.label4 = new System.Windows.Forms.Label();
this.txtReceive2 = new System.Windows.Forms.TextBox();
this.btnReceive2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnConnect
//
this.btnConnect.Location = new System.Drawing.Point(480, 8);
this.btnConnect.Name = "btnConnect";
this.btnConnect.Size = new System.Drawing.Size(72, 24);
this.btnConnect.TabIndex = 5;
this.btnConnect.Text = "连接";
this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
//
// txtPort2
//
this.txtPort2.Location = new System.Drawing.Point(360, 8);
this.txtPort2.Name = "txtPort2";
this.txtPort2.Size = new System.Drawing.Size(112, 21);
this.txtPort2.TabIndex = 4;
this.txtPort2.Text = "1000";
//
// label1
//
this.label1.Location = new System.Drawing.Point(280, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 24);
this.label1.TabIndex = 3;
this.label1.Text = "端口号";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 8);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 16);
this.label2.TabIndex = 6;
this.label2.Text = "机器名";
//
// txtServer
//
this.txtServer.Location = new System.Drawing.Point(96, 8);
this.txtServer.Name = "txtServer";
this.txtServer.Size = new System.Drawing.Size(176, 21);
this.txtServer.TabIndex = 7;
this.txtServer.Text = "teachera";
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 40);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(80, 24);
this.label3.TabIndex = 8;
this.label3.Text = "待发送数据";
//
// txtSend2
//
this.txtSend2.Location = new System.Drawing.Point(96, 40);
this.txtSend2.Name = "txtSend2";
this.txtSend2.Size = new System.Drawing.Size(376, 21);
this.txtSend2.TabIndex = 9;
this.txtSend2.Text = "Hello, I am Client!";
//
// btnSend2
//
this.btnSend2.Location = new System.Drawing.Point(480, 40);
this.btnSend2.Name = "btnSend2";
this.btnSend2.Size = new System.Drawing.Size(72, 24);
this.btnSend2.TabIndex = 10;
this.btnSend2.Text = "发送";
this.btnSend2.Click += new System.EventHandler(this.btnSend2_Click);
//
// label4
//
this.label4.Location = new System.Drawing.Point(8, 72);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(80, 24);
this.label4.TabIndex = 11;
this.label4.Text = "接收到数据";
//
// txtReceive2
//
this.txtReceive2.Location = new System.Drawing.Point(96, 72);
this.txtReceive2.Name = "txtReceive2";
this.txtReceive2.Size = new System.Drawing.Size(376, 21);
this.txtReceive2.TabIndex = 12;
this.txtReceive2.Text = "";
//
// btnReceive2
//
this.btnReceive2.Location = new System.Drawing.Point(480, 72);
this.btnReceive2.Name = "btnReceive2";
this.btnReceive2.Size = new System.Drawing.Size(72, 24);
this.btnReceive2.TabIndex = 13;
this.btnReceive2.Text = "接收";
this.btnReceive2.Click += new System.EventHandler(this.btnReceive2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(568, 381);
this.Controls.Add(this.btnReceive2);
this.Controls.Add(this.txtReceive2);
this.Controls.Add(this.label4);
this.Controls.Add(this.btnSend2);
this.Controls.Add(this.txtSend2);
this.Controls.Add(this.label3);
this.Controls.Add(this.txtServer);
this.Controls.Add(this.label2);
this.Controls.Add(this.btnConnect);
this.Controls.Add(this.txtPort2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "客户端 ";
this.ResumeLayout(false);
}
#endregion
TcpClient tcpclient;
NetworkStream ns;
StreamReader sr;
StreamWriter sw;
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
//连接
private void btnConnect_Click(object sender, System.EventArgs e)
{
tcpclient=new TcpClient();
tcpclient.Connect(txtServer.Text,int.Parse(txtPort2.Text));
ns=tcpclient.GetStream();
sr=new StreamReader(ns);
sw=new StreamWriter(ns);
}
//客户端发送数据
private void btnSend2_Click(object sender, System.EventArgs e)
{
sw.WriteLine(txtSend2.Text);
sw.Flush();
}
//客户端接收
private void btnReceive2_Click(object sender, System.EventArgs e)
{
txtReceive2.Text=sr.ReadLine();
}
}
}
////////////////
例(3)服务器端,WinForm程序
////////////////
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Net;
namespace TcpServerTest
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtPort1;
private System.Windows.Forms.Button btnListen;
private System.Windows.Forms.Button btnAccecp;
private System.Windows.Forms.Button btnReceive1;
private System.Windows.Forms.TextBox txtReceive1;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button btnSend1;
private System.Windows.Forms.TextBox txtSend1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.ListBox listBox1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.txtPort1 = new System.Windows.Forms.TextBox();
this.btnListen = new System.Windows.Forms.Button();
this.btnAccecp = new System.Windows.Forms.Button();
this.btnReceive1 = new System.Windows.Forms.Button();
this.txtReceive1 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.btnSend1 = new System.Windows.Forms.Button();
this.txtSend1 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 24);
this.label1.TabIndex = 0;
this.label1.Text = "端口号";
//
// txtPort1
//
this.txtPort1.Location = new System.Drawing.Point(96, 8);
this.txtPort1.Name = "txtPort1";
this.txtPort1.Size = new System.Drawing.Size(112, 21);
this.txtPort1.TabIndex = 1;
this.txtPort1.Text = "1000";
//
// btnListen
//
this.btnListen.Location = new System.Drawing.Point(224, 8);
this.btnListen.Name = "btnListen";
this.btnListen.Size = new System.Drawing.Size(72, 24);
this.btnListen.TabIndex = 2;
this.btnListen.Text = "侦听";
this.btnListen.Click += new System.EventHandler(this.btnListen_Click);
//
// btnAccecp
//
this.btnAccecp.Location = new System.Drawing.Point(312, 8);
this.btnAccecp.Name = "btnAccecp";
this.btnAccecp.Size = new System.Drawing.Size(64, 24);
this.btnAccecp.TabIndex = 3;
this.btnAccecp.Text = "接受连接 ";
this.btnAccecp.Click += new System.EventHandler(this.btnAccecp_Click);
//
// btnReceive1
//
this.btnReceive1.Location = new System.Drawing.Point(480, 72);
this.btnReceive1.Name = "btnReceive1";
this.btnReceive1.Size = new System.Drawing.Size(72, 24);
this.btnReceive1.TabIndex = 19;
this.btnReceive1.Text = "接收";
this.btnReceive1.Click += new System.EventHandler(this.btnReceive1_Click);
//
// txtReceive1
//
this.txtReceive1.Location = new System.Drawing.Point(96, 72);
this.txtReceive1.Name = "txtReceive1";
this.txtReceive1.Size = new System.Drawing.Size(376, 21);
this.txtReceive1.TabIndex = 18;
this.txtReceive1.Text = "";
//
// label4
//
this.label4.Location = new System.Drawing.Point(8, 72);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(80, 24);
this.label4.TabIndex = 17;
this.label4.Text = "接收到数据";
//
// btnSend1
//
this.btnSend1.Location = new System.Drawing.Point(480, 40);
this.btnSend1.Name = "btnSend1";
this.btnSend1.Size = new System.Drawing.Size(72, 24);
this.btnSend1.TabIndex = 16;
this.btnSend1.Text = "发送";
this.btnSend1.Click += new System.EventHandler(this.btnSend1_Click);
//
// txtSend1
//
this.txtSend1.Location = new System.Drawing.Point(96, 40);
this.txtSend1.Name = "txtSend1";
this.txtSend1.Size = new System.Drawing.Size(376, 21);
this.txtSend1.TabIndex = 15;
this.txtSend1.Text = "Hello, I am Server!";
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 40);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(80, 24);
this.label3.TabIndex = 14;
this.label3.Text = "待发送数据";
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(24, 120);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(536, 136);
this.listBox1.TabIndex = 20;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(608, 273);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.btnReceive1);
this.Controls.Add(this.txtReceive1);
this.Controls.Add(this.label4);
this.Controls.Add(this.btnSend1);
this.Controls.Add(this.txtSend1);
this.Controls.Add(this.label3);
this.Controls.Add(this.btnAccecp);
this.Controls.Add(this.btnListen);
this.Controls.Add(this.txtPort1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "服务器端";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
TcpListener tcplistener;
Socket socketForClient;
NetworkStream ns;
StreamReader sr;
StreamWriter sw;
//侦听
private void btnListen_Click(object sender, System.EventArgs e)
{
IPHostEntry hostInfo = Dns.GetHostByName("teachera");
tcplistener=new TcpListener(hostInfo.AddressList[0],int.Parse(txtPort1.Text));
tcplistener.Start();
listBox1.Items.Add("开始侦听。。。");
}
//接受连接
private void btnAccecp_Click(object sender, System.EventArgs e)
{
socketForClient=tcplistener.AcceptSocket();
ns=new NetworkStream(socketForClient);
sr=new StreamReader(ns);
sw=new StreamWriter(ns);
listBox1.Items.Add("已建立连接。。。");
}
//服务器接收数据
private void btnReceive1_Click(object sender, System.EventArgs e)
{
string line=sr.ReadLine();
txtReceive1.Text=line;
listBox1.Items.Add("接收数据成功。。。");
}
//服务器发送数据
private void btnSend1_Click(object sender, System.EventArgs e)
{
sw.WriteLine(txtSend1.Text);
sw.Flush();
}
}
}
////////////////
例(4)客户端,WinForm程序
////////////////
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets;
using System.IO;
using System.Text;
namespace TcpClientTest
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtServer;
private System.Windows.Forms.TextBox txtPort2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button btnSend2;
private System.Windows.Forms.TextBox txtSend2;
private System.Windows.Forms.TextBox txtPort;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtServer = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.txtSend2 = new System.Windows.Forms.TextBox();
this.btnSend2 = new System.Windows.Forms.Button();
this.txtPort = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(280, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 24);
this.label1.TabIndex = 3;
this.label1.Text = "端口号";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 8);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 16);
this.label2.TabIndex = 6;
this.label2.Text = "机器名";
//
// txtServer
//
this.txtServer.Location = new System.Drawing.Point(96, 8);
this.txtServer.Name = "txtServer";
this.txtServer.Size = new System.Drawing.Size(176, 21);
this.txtServer.TabIndex = 7;
this.txtServer.Text = "teachera";
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 40);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(80, 24);
this.label3.TabIndex = 8;
this.label3.Text = "待发送数据";
//
// txtSend2
//
this.txtSend2.Location = new System.Drawing.Point(96, 40);
this.txtSend2.Name = "txtSend2";
this.txtSend2.Size = new System.Drawing.Size(376, 21);
this.txtSend2.TabIndex = 9;
this.txtSend2.Text = "Hello, I am Client!";
//
// btnSend2
//
this.btnSend2.Location = new System.Drawing.Point(480, 40);
this.btnSend2.Name = "btnSend2";
this.btnSend2.Size = new System.Drawing.Size(72, 24);
this.btnSend2.TabIndex = 10;
this.btnSend2.Text = "发送";
this.btnSend2.Click += new System.EventHandler(this.btnSend2_Click);
//
// txtPort
//
this.txtPort.Location = new System.Drawing.Point(368, 8);
this.txtPort.Name = "txtPort";
this.txtPort.Size = new System.Drawing.Size(72, 21);
this.txtPort.TabIndex = 11;
this.txtPort.Text = "1000";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(568, 381);
this.Controls.Add(this.txtPort);
this.Controls.Add(this.btnSend2);
this.Controls.Add(this.txtSend2);
this.Controls.Add(this.label3);
this.Controls.Add(this.txtServer);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "客户端 ";
this.ResumeLayout(false);
}
#endregion
UdpClient udpclient;
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
//客户端发送
private void btnSend2_Click(object sender, System.EventArgs e)
{
//转换成字节数组
string str=txtSend2.Text;
byte[] sds=Encoding.ASCII.GetBytes(str);
udpclient=new UdpClient();
udpclient.Send(sds,sds.Length,txtServer.Text,int.Parse(txtPort.Text));
}
}
}
////////////////
例(5)服务器端,WinForm程序
////////////////
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Net;
namespace TcpClientTest
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtPort2;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox txtReceive2;
private System.Windows.Forms.Button btnReceive2;
private System.Windows.Forms.Button btnBind;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnBind = new System.Windows.Forms.Button();
this.txtPort2 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.txtReceive2 = new System.Windows.Forms.TextBox();
this.btnReceive2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnBind
//
this.btnBind.Location = new System.Drawing.Point(232, 8);
this.btnBind.Name = "btnBind";
this.btnBind.Size = new System.Drawing.Size(72, 24);
this.btnBind.TabIndex = 5;
this.btnBind.Text = "绑定";
this.btnBind.Click += new System.EventHandler(this.btnBind_Click);
//
// txtPort2
//
this.txtPort2.Location = new System.Drawing.Point(104, 8);
this.txtPort2.Name = "txtPort2";
this.txtPort2.Size = new System.Drawing.Size(112, 21);
this.txtPort2.TabIndex = 4;
this.txtPort2.Text = "1000";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 24);
this.label1.TabIndex = 3;
this.label1.Text = "端口号";
//
// label4
//
this.label4.Location = new System.Drawing.Point(8, 48);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(80, 24);
this.label4.TabIndex = 11;
this.label4.Text = "接收到数据";
//
// txtReceive2
//
this.txtReceive2.Location = new System.Drawing.Point(104, 48);
this.txtReceive2.Name = "txtReceive2";
this.txtReceive2.Size = new System.Drawing.Size(368, 21);
this.txtReceive2.TabIndex = 12;
this.txtReceive2.Text = "";
//
// btnReceive2
//
this.btnReceive2.Location = new System.Drawing.Point(480, 48);
this.btnReceive2.Name = "btnReceive2";
this.btnReceive2.Size = new System.Drawing.Size(72, 24);
this.btnReceive2.TabIndex = 13;
this.btnReceive2.Text = "接收";
this.btnReceive2.Click += new System.EventHandler(this.btnReceive2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(568, 381);
this.Controls.Add(this.btnReceive2);
this.Controls.Add(this.txtReceive2);
this.Controls.Add(this.label4);
this.Controls.Add(this.btnBind);
this.Controls.Add(this.txtPort2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "服务器端 ";
this.ResumeLayout(false);
}
#endregion
UdpClient udpserver;
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
//绑定
private void btnBind_Click(object sender, System.EventArgs e)
{
udpserver=new UdpClient(int.Parse(txtPort2.Text));
}
//服务器接收
private void btnReceive2_Click(object sender, System.EventArgs e)
{
IPEndPoint remote=new IPEndPoint(IPAddress.Any,0);
byte[] revs=udpserver.Receive(ref remote);
txtReceive2.Text=Encoding.ASCII.GetString(revs);
}
}
}