2008.06.14 / 标签:
C#网络编程 / 分类:
WinForm
P2P,即英文peer-to-peer的缩写,译为对等互联或点对点技术。p2p是一种用于在不同PC用户之间,不经过中继设备直接交换数据或服务的技术,它允许Internet用户直接使用对方的文件。网络中的任意用户都可以直接连接到其他用户的计算机,并进行文件的交换,而不需要连接到服务器上在进行浏览与下载。因为消除了中间环节,p2p技术是的网络上的沟通变得更容易、更直接。此程序演示了如何从一点向另一点发送文件。
源码1:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
-
- namespace FileReceive
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- IPAddress ip = IPAddress.Parse("127.0.0.1");
- TcpListener tcplistener = new TcpListener(ip, 4525);
- tcplistener.Start();
-
- TcpClient tcpclient = tcplistener.AcceptTcpClient();
- NetworkStream ns = tcpclient.GetStream();
- StreamReader sr = new StreamReader(ns,Encoding.Default);
-
- string result = sr.ReadToEnd();
- this.textBox1.Text = result;
- sr.Close();
- ns.Close();
- tcpclient.Close();
- tcplistener.Stop();
- }
- }
- }
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;using System.Net;using System.Net.Sockets;namespace FileReceive{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { IPAddress ip = IPAddress.Parse("127.0.0.1"); TcpListener tcplistener = new TcpListener(ip, 4525); tcplistener.Start(); TcpClient tcpclient = tcplistener.AcceptTcpClient(); NetworkStream ns = tcpclient.GetStream(); StreamReader sr = new StreamReader(ns,Encoding.Default); string result = sr.ReadToEnd(); this.textBox1.Text = result; sr.Close(); ns.Close(); tcpclient.Close(); tcplistener.Stop(); } }}
源码2:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
-
- namespace Peer2Peer
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- &nbs
p; InitializeComponent();
- }
-
- private void btnSend_Click(object sender, EventArgs e)
- {
-
- this.openFileDialog1.ShowDialog();
- string filename = this.openFileDialog1.FileName;
- FileStream fs = File.Open(filename, FileMode.Open);
-
- TcpClient tcpclient = new TcpClient(txtIp.Text, int.Parse(txtPort.Text));
- NetworkStream ns = tcpclient.GetStream();
-
- int data = fs.ReadByte();
- while (data != -1)
- {
- ns.WriteByte((byte)data);
- data = fs.ReadByte();
- }
- fs.Close();
- ns.Close();
- tcpclient.Close();
- }
- }
- }
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;using System.Net;using System.Net.Sockets;namespace Peer2Peer{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnSend_Click(object sender, EventArgs e) { this.openFileDialog1.ShowDialog(); string filename = this.openFileDialog1.FileName; FileStream fs = File.Open(filename, FileMode.Open); TcpClient tcpclient = new TcpClient(txtIp.Text, int.Parse(txtPort.Text)); NetworkStream ns = tcpclient.GetStream(); int data = fs.ReadByte(); while (data != -1) { ns.WriteByte((byte)data); data = fs.ReadByte(); } fs.Close(); ns.Close(); tcpclient.Close(); } }}
2008.06.14 / 标签:
C#网络编程 / 分类:
WinForm
winform的高级应用,我咋就不觉得有多高级呢?这个C#网页浏览器、网页源码浏览器就是所谓高级应用。代码很短,很简单。所以就不写注释了。效果图: 
源码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Net;
- using System.Net.Sockets;
- using System.IO;
-
- namespace WebBrowser
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void btnSeeCode_Click(object sender, EventArgs e)
- {
- axWebBrowser1.Visible = false;
- WebClient wbc = new WebClient();
- Stream stm = wbc.OpenRead(txtUrl.Text);
- StreamReader sr = new StreamReader(stm,Encoding.Default);
- string line;
- string lines = "";
- while ((line = sr.ReadLine()) != null)
- {
- lines += line;
- }
- txtCode.Text = lines;
- }
-
- private void btnView_Click(object sender, EventArgs e)
- {
- axWebBrowser1.Visible = true;
- axWebBrowser1.Navigate(txtUrl.Text);
- }
- }
- }
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Net;using System.Net.Sockets;using System.IO;namespace WebBrowser{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnSeeCode_Click(object sender, EventArgs e) { axWebBrowser1.Visible = false; WebClient wbc = new WebClient(); Stream stm = wbc.OpenRead(txtUrl.Text); StreamReader sr = new StreamReader(stm,Encoding.Default); string line; string lines = ""; while ((line = sr.ReadLine()) != null) { lines += line; } txtCode.Text = lines; } private void btnView_Click(object sender, EventArgs e) { axWebBrowser1.Visible = true; axWebBrowser1.Navigate(txtUrl.Text); } }}
2008.06.14 / 标签:
C#网络编程 / 分类:
WinForm
这个程序所要实现的功能是:客户端发送一段字符串给服务端,服务端将其字母换成大写形式并返回给客户端。当然这个程序还是为了学习而写的,练练手而已。
客户端源码:
using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.IO;using System.Net.Sockets;using System.Net;namespace ClientTest{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // 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.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(56, 80); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "发送"; this.button1.Click += new System.EventHandler(this.button1_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(48, 8); this.textBox1.Name = "textBox1"; this.textBox1.TabIndex = 1; this.textBox1.Text = ""; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(48, 40); this.textBox2.Name = "textBox2"; this.textBox2.TabIndex = 2; this.textBox2.Text = ""; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(200, 118); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { //创建tcpclient对象 TcpClient TClient = new TcpClient(); //创建与服务器的连接 TClient.Connect(IPAddress.Parse("127.0.0.1"),8000); //获取网络流 NetworkStream ns = TClient.GetStream(); //创建流写入对象 StreamWriter sw = new StreamWriter(ns); //写入数据 sw.WriteLine(this.textBox1.Text); //清空缓冲 sw.Flush(); //创建流读取对象 StreamReader sr = new StreamReader(ns); //读出数据 this.textBox2.Text=sr.ReadLine(); //关闭 sr.Close(); sw.Close(); ns.Close(); TClient.Close(); } }}[/CODE]Server端源码:[CODE=csharp]using System;using System.IO;using System.Threading;using System.Net;using System.Net.Sockets;namespace ServerTest{ /// <summary> /// Class1 的摘要说明。 /// </summary> class Class1 { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main(string[] args) { // // TODO: 在此处添加代码以启动应
用程序 // Console.WriteLine("Running....."); Thread thread = new Thread(new ThreadStart(Convert)); thread.Start(); } /// <summary> /// 静态的方法对字符转换 /// </summary> static void Convert() { //创建监听对象并启动 TcpListener TListener = new TcpListener(8000); TListener.Start(); while(true) { //获取socket Socket socket = TListener.AcceptSocket(); //获取网络流 NetworkStream ns = new NetworkStream(socket); //创建流读取对象 StreamReader sr = new StreamReader(ns); //创建流写入对象 StreamWriter sw = new StreamWriter(ns); //读取数据转换大小写并写入 sw.WriteLine(sr.ReadLine().ToUpper()); //关闭 sw.Flush(); sw.Close(); sr.Close(); ns.Close(); socket.Close(); Console.WriteLine("OK"); } } }}
2008.06.14 / 标签:
C#网络编程 / 分类:
WinForm
o(∩_∩)o…当然和QQ是没得比的了。主要是为了学习C#网络编程及winform高级编程。聊天程序截图如下:
C#局域网聊天器源码:
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.Net;
- using System.IO;
- using System.Net.Sockets;
- using System.Threading;
-
- namespace Chat
- {
-
-
-
- public class frmMain : System.Windows.Forms.Form
- {
- private System.Windows.Forms.Button btnConnect;
- private System.Windows.Forms.TextBox txtPort1;
- private System.Windows.Forms.TextBox txtPort2;
- private System.Windows.Forms.ListBox lstInfo;
- private System.Windows.Forms.Button btnSend;
- private System.Windows.Forms.TextBox txtMsg;
-
-
-
- private System.ComponentModel.Container components = null;
-
- public frmMain()
- {
-
-
-
- InitializeComponent();
-
-
-
-
- }
-
-
-
-
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if (components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
-
- #region Windows 窗体设计器生成的代码
-
-
-
-
- private void InitializeComponent()
- {
- this.btnConnect = new System.Windows.Forms.Button();
- this.txtPort1 = new System.Windows.Forms.TextBox();
- this.txtPort2 = new System.Windows.Forms.TextBox();
- this.lstInfo = new System.Windows.Forms.ListBox();
- this.btnSend = new System.Windows.Forms.Button();
- this.txtMsg = new System.Windows.Forms.TextBox();
- this.SuspendLayout();
-
-
-
- this.btnConnect.Location = new System.Drawing.Point(200, 224);
- this.btnConnect.Name = "btnConnect";
- this.btnConnect.TabIndex = 0;
- this.btnConnect.Text = "连接";
- this.btnConnect.Click += new System.EventHandler(this.button1_Click);
-
-
-
- this.txtPort1.Location = new System.Drawing.Point(16, 224);
- this.txtPort1.Name = "txtPort1";
- this.txtPort1.Size = new System.Drawing.Size(64, 21);
- this.txtPort1.TabIndex = 1;
- this.txtPort1.Text = "";
-
-
-
- this.txtPort2.Location = new System.Drawing.Point(112, 224);
- this.txtPort2.Name = "txtPort2";
- this.txtPort2.Size = new System.Drawing.Size(64, 21);
- this.txtPort2.TabIndex = 2;
- this.txtPort2.Text = "";
-
-
-
- this.lstInfo.ItemHeight = 12;
- this.lstInfo.Location = new System.Drawing.Point(16, 8);
- this.lstInfo.Name = "lstInfo";
- this.lstInfo.Size = new System.Drawing.Size(464, 136);
- this.lstInfo.TabIndex = 3;
-
-
-
- this.btnSend.Location = new System.Drawing.Point(328, 224);
- this.btnSend.Name = "btnSend";
- this.btnSend.TabIndex = 4;
- this.btnSend.Text = "发送";
- this.btnSend.Click += new System.EventHandler(this.button2_Click);
-
-
-
- this.txtMsg.Location = new System.Drawing.Point(16, 152);
- this.txtMsg.Multiline = true;
- this.txtMsg.Name = "txtMsg";
- this.txtMsg.Size = new System.Drawing.Size(464, 56);
- this.txtMsg.TabIndex = 5;
- this.txtMsg.Text = "";
-
-
-
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(488, 266);
- this.Controls.Add(this.txtMsg);
- this.Controls.Add(this.btnSend);
- this.Controls.Add(this.lstInfo);
- this.Controls.Add(this.txtPort2);
- this.Controls.Add(this.txtPort1);
- this.Controls.Add(this.btnConnect);
- this.Name = "frmMain";
- this.Text = "简单聊天";
- this.ResumeLayout(false);
-
- }
- #endregion
-
-
-
-
- [STAThread]
- static void Main()
- {
- Application.Run(new frmMain());
- }
-
-
class="keyword">private void button2_Click(object sender, System.EventArgs e)
- {
-
- TcpClient TClient = new TcpClient();
-
- TClient.Connect(IPAddress.Parse("127.0.0.1"),int.Parse(this.txtPort1.Text));
-
- NetworkStream ns = TClient.GetStream();
-
- StreamWriter sw =new StreamWriter(ns);
-
- sw.WriteLine(this.txtMsg.Text);
-
- sw.Flush();
-
- sw.Close();
- ns.Close();
- this.lstInfo.Items.Add("你说:"+DateTime.Now.ToString());
- this.lstInfo.Items.Add(this.txtMsg.Text);
- this.txtMsg.Text="";
- }
-
-
-
- void Recieve()
- {
-
- TcpListener TListener = new TcpListener(int.Parse(this.txtPort2.Text));
- TListener.Start();
-
- while(true)
- {
-
- Socket socket = TListener.AcceptSocket();
-
- NetworkStream ns = new NetworkStream(socket);
-
- StreamReader sr =new StreamReader(ns);
-
- string line = sr.ReadLine();
-
- this.lstInfo.Items.Add("对方说:"+DateTime.Now.ToString());
- this.lstInfo.Items.Add(line);
- sr.Close();
- ns.Close();
- socket.Close();
- }
- }
-
- private void button1_Click(ob
ject sender, System.EventArgs e)
- {
-
- Thread thread = new Thread(new ThreadStart(Recieve));
- thread.Start();
- }
- }
- }
using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Net;using System.IO;using System.Net.Sockets;using System.Threading;namespace Chat{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class frmMain : System.Windows.Forms.Form { private System.Windows.Forms.Button btnConnect; private System.Windows.Forms.TextBox txtPort1; private System.Windows.Forms.TextBox txtPort2; private System.Windows.Forms.ListBox lstInfo; private System.Windows.Forms.Button btnSend; private System.Windows.Forms.TextBox txtMsg; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public frmMain() { // // 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.btnConnect = new System.Windows.Forms.Button(); this.txtPort1 = new System.Windows.Forms.TextBox(); this.txtPort2 = new System.Windows.Forms.TextBox(); this.lstInfo = new System.Windows.Forms.ListBox(); this.btnSend = new System.Windows.Forms.Button(); this.txtMsg = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // btnConnect // this.btnConnect.Location = new System.Drawing.Point(200, 224); this.btnConnect.Name = "btnConnect"; this.btnConnect.TabIndex = 0; this.btnConnect.Text = "连接"; this.btnConnect.Click += new System.EventHandler(this.button1_Click); // // txtPort1 // this.txtPort1.Location = new System.Drawing.Point(16, 224); this.txtPort1.Name = "txtPort1"; this.txtPort1.Size = new System.Drawing.Size(64, 21); this.txtPort1.TabIndex = 1; this.txtPort1.Text = ""; // // txtPort2 // this.txtPort2.Location = new System.Drawing.Point(112, 224); this.txtPort2.Name = "txtPort2"; this.txtPort2.Size = new System.Drawing.Size(64, 21); this.txtPort2.TabIndex = 2; this.txtPort2.Text = ""; // // lstInfo // this.lstInfo.ItemHeight = 12; this.lstInfo.Location = new System.Drawing.Point(16, 8); this.lstInfo.Name = "lstInfo"; this.lstInfo.Size = new System.Drawing.Size(464, 136); this.lstInfo.TabIndex = 3; // // btnSend // this.btnSend.Location = new System.Drawing.Point(328, 224); this.btnSend.Name = "btnSend"; this.btnSend.TabIndex = 4; this.btnSend.Text = "发送"; this.btnSend.Click += new System.EventHandler(this.button2_Click); // // txtMsg // this.txtMsg.Location = new System.Drawing.Point(16, 152); this.txtMsg.Multiline = true; this.txtMsg.Name = "txtMsg"; this.txtMsg.Size = new System.Drawing.Size(464, 56); this.txtMsg.TabIndex = 5; this.txtMsg.Text = ""; // // frmMain // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(488, 266); this.Controls.Add(this.txtMsg); this.Controls.Add(this.btnSend); this.Controls.Add(this.lstInfo); this.Controls.Add(this.txtPort2); this.Controls.Add(this.txtPort1); this.Controls.Add(this.btnConnect); this.Name = "frmMain"; this.Text = "简单聊天"; this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new frmMain()); } private void button2_Click(object sender, System.EventArgs e) { //创建tcpclient对象 TcpClient TClient = new TcpClient(); //连接 TClient.Connect(IPAddress.Parse("127.0.0.1"),int.Parse(this.txtPort1.Text)); //获取网络流 NetworkStream ns = TClient.GetStream(); //创建流写入对象 StreamWriter sw =new StreamWriter(ns); //写入信息 sw.WriteLine(this.txtMsg.Text); //清空缓冲 sw.Flush(); //添加入聊天窗口 sw.Close(); ns.Close(); this.lstInfo.Items.Add("你说:"+DateTime.Now.ToString()); this.lstInfo.Items.Add(this.txtMsg.Text); this.txtMsg.Text=""; } /// <summary> /// 数据接收方法 /// </summary> void Recieve() { //创建监听对象并启动 TcpListener TListener = new TcpListener(int.Parse(this.txtPort2.Text)); TListener.Start(); while(true) { //创建socket Socket socket = TListener.AcceptSocket(); //创建网络流对象 NetworkStream ns = new NetworkStream(socket); //创建流读取对象 StreamReader sr =new StreamReader(ns); //获取信息 string line = sr.ReadLine(); //写入聊天窗口 this.lstInfo.Items.Add("对方说:"+DateTime.Now.ToString()); this.lstInfo.Items.Add(line); sr.Close(); ns.Close(); socket.Close(); } } private void button1_Click(object sender, System.EventArgs e) { //点击此按钮时开始监听 Thread thread = new Thread(new ThreadStart(Recieve)); thread.Start(); } }}
2008.06.11 / 标签:
C#网络编程 / 分类:
WinForm
C#网页源码读取器,以前在网上见别人写过这个东西,用来查看某个网站中网页的源代码(若是动态网站,当看到的不是真正的源码)看看里面有没有被挂马,本来还觉得那东西挺神秘的,今天自己写了一下,原来如此easy o(∩_∩)o… 程序源码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Net;
- using System.IO;
-
- namespace PageCode
- {
- public partial class frmMain : Form
- {
- public frmMain()
- {
- InitializeComponent();
- }
-
- private void btnGet_Click(object sender, EventArgs e)
- {
-
- WebClient client = new WebClient();
-
- Stream strm = client.OpenRead(txtUrl.Text);
-
- StreamReader sr = new StreamReader(strm);
-
- StringBuilder sb = new StringBuilder();
- string line;
-
- while ((line = sr.ReadLine()) != null)
- {
- sb.Append(line);
- }
- sr.Close();
- strm.Close();
-
- this.txtCode.Text = sb.ToString();
- }
- }
- }
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Net;using System.IO;namespace PageCode{ public partial class frmMain : Form { public frmMain() { InitializeComponent(); } private void btnGet_Click(object sender, EventArgs e) { //创建WebClient对象 WebClient client = new WebClient(); //创建Stream流对象 Stream strm = client.OpenRead(txtUrl.Text); //创建StreamReader对象 StreamReader sr = new StreamReader(strm); //创建StringBuilder对象用于存储网页代码 StringBuilder sb = new StringBuilder(); string line; //获取网页代码并存入sb中 while ((line = sr.ReadLine()) != null) { sb.Append(line); } sr.Close(); strm.Close(); //显示读取到的代码 this.txtCode.Text = sb.ToString(); } }}
2008.06.11 / 标签:
C#网络编程 / 分类:
WinForm
.NET框架支持在应用程序中实现Internet服务,应用程序可以通过构建可插接式协议来充分利用新的Internet协议。Internet应用程序分为两种类型:客户端应用程序和服务器应用程序。客户端/服务器应用程序最常见的实例是万维网,可以使用浏览器在世界任何地方访问存储在web服务器上的数据。
阅读全文>>