C# p2p应用程序示例

2008.06.14 / 标签: / 分类: WinForm
Sofa

P2P,即英文peer-to-peer的缩写,译为对等互联或点对点技术。p2p是一种用于在不同PC用户之间,不经过中继设备直接交换数据或服务的技术,它允许Internet用户直接使用对方的文件。网络中的任意用户都可以直接连接到其他用户的计算机,并进行文件的交换,而不需要连接到服务器上在进行浏览与下载。因为消除了中间环节,p2p技术是的网络上的沟通变得更容易、更直接。此程序演示了如何从一点向另一点发送文件。 源码1:

 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. using System.Net;
  10. using System.Net.Sockets;
  11.  
  12. namespace FileReceive
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         private void button1_Click(object sender, EventArgs e)
  22.         {
  23.             IPAddress ip = IPAddress.Parse("127.0.0.1");
  24.             TcpListener tcplistener = new TcpListener(ip, 4525);
  25.             tcplistener.Start();
  26.  
  27.             TcpClient tcpclient = tcplistener.AcceptTcpClient();
  28.             NetworkStream ns = tcpclient.GetStream();
  29.             StreamReader sr = new StreamReader(ns,Encoding.Default);
  30.  
  31.             string result = sr.ReadToEnd();
  32.             this.textBox1.Text = result;
  33.             sr.Close();
  34.             ns.Close();
  35.             tcpclient.Close();
  36.             tcplistener.Stop();
  37.         }
  38.     }
  39. }

源码2:

 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. using System.Net;
  10. using System.Net.Sockets;
  11.  
  12. namespace Peer2Peer
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.        &nbs
    p;    InitializeComponent();
  19.         }
  20.  
  21.         private void btnSend_Click(object sender, EventArgs e)
  22.         {
  23.             
  24.             this.openFileDialog1.ShowDialog();
  25.             string filename = this.openFileDialog1.FileName;
  26.             FileStream fs = File.Open(filename, FileMode.Open);
  27.  
  28.             TcpClient tcpclient = new TcpClient(txtIp.Text, int.Parse(txtPort.Text));
  29.             NetworkStream ns = tcpclient.GetStream();
  30.             
  31.             int data = fs.ReadByte();
  32.             while (data != -1)
  33.             {
  34.                 ns.WriteByte((byte)data);
  35.                 data = fs.ReadByte();
  36.             }
  37.             fs.Close();
  38.             ns.Close();
  39.             tcpclient.Close();
  40.         }
  41.     }
  42. }

 

C#网页浏览器、网页源码浏览器

2008.06.14 / 标签: / 分类: WinForm

winform的高级应用,我咋就不觉得有多高级呢?这个C#网页浏览器、网页源码浏览器就是所谓高级应用。代码很短,很简单。所以就不写注释了。效果图: 源码:

 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.IO;
  11.  
  12. namespace WebBrowser
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         private void btnSeeCode_Click(object sender, EventArgs e)
  22.         {
  23.             axWebBrowser1.Visible = false;
  24.             WebClient wbc = new WebClient();
  25.             Stream stm = wbc.OpenRead(txtUrl.Text);
  26.             StreamReader sr = new StreamReader(stm,Encoding.Default);
  27.             string line;
  28.             string lines = "";
  29.             while ((line = sr.ReadLine()) != null)
  30.             {
  31.                 lines += line;
  32.             }
  33.             txtCode.Text = lines;
  34.         }
  35.  
  36.         private void btnView_Click(object sender, EventArgs e)
  37.         {
  38.             axWebBrowser1.Visible = true;
  39.             axWebBrowser1.Navigate(txtUrl.Text);
  40.         }
  41.     }
  42. }

 

C#C/S结构网络编程

2008.06.14 / 标签: / 分类: WinForm
Sofa

这个程序所要实现的功能是:客户端发送一段字符串给服务端,服务端将其字母换成大写形式并返回给客户端。当然这个程序还是为了学习而写的,练练手而已。 客户端源码:

 
  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.IO;
  8. using System.Net.Sockets;
  9. using System.Net;
  10.  
  11. namespace ClientTest
  12. {
  13.     /// <summary>
  14.     /// Form1 的摘要说明。
  15.     /// </summary>
  16.     public class Form1 : System.Windows.Forms.Form
  17.     {
  18.         private System.Windows.Forms.Button button1;
  19.         private System.Windows.Forms.TextBox textBox1;
  20.         private System.Windows.Forms.TextBox textBox2;
  21.         /// <summary>
  22.         /// 必需的设计器变量。
  23.         /// </summary>
  24.         private System.ComponentModel.Container components = null;
  25.  
  26.         public Form1()
  27.         {
  28.             //
  29.             // Windows 窗体设计器支持所必需的
  30.             //
  31.             InitializeComponent();
  32.  
  33.             //
  34.             // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  35.             //
  36.         }
  37.  
  38.         /// <summary>
  39.         /// 清理所有正在使用的资源。
  40.         /// </summary>
  41.         protected override void Dispose( bool disposing )
  42.         {
  43.             if( disposing )
  44.             {
  45.                 if (components != null
  46.                 {
  47.                     components.Dispose();
  48.                 }
  49.             }
  50.             base.Dispose( disposing );
  51.         }
  52.  
  53.         #region Windows 窗体设计器生成的代码
  54.         /// <summary>
  55.         /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  56.         /// 此方法的内容。
  57.         /// </summary>
  58.         private void InitializeComponent()
  59.         {
  60.             this.button1 = new System.Windows.Forms.Butt
    on();
  61.             this.textBox1 = new System.Windows.Forms.TextBox();
  62.             this.textBox2 = new System.Windows.Forms.TextBox();
  63.             this.SuspendLayout();
  64.             // 
  65.             // button1
  66.             // 
  67.             this.button1.Location = new System.Drawing.Point(56, 80);
  68.             this.button1.Name = "button1";
  69.             this.button1.TabIndex = 0;
  70.             this.button1.Text = "发送";
  71.             this.button1.Click += new System.EventHandler(this.button1_Click);
  72.             // 
  73.             // textBox1
  74.             // 
  75.             this.textBox1.Location = new System.Drawing.Point(48, 8);
  76.             this.textBox1.Name = "textBox1";
  77.             this.textBox1.TabIndex = 1;
  78.             this.textBox1.Text = "";
  79.             // 
  80.             // textBox2
  81.             // 
  82.             this.textBox2.Location = new System.Drawing.Point(48, 40);
  83.             this.textBox2.Name = "textBox2";
  84.             this.textBox2.TabIndex = 2;
  85.             this.textBox2.Text = "";
  86.             // 
  87.             // Form1
  88.             // 
  89.             this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  90.             this.ClientSize = new System.Drawing.Size(200, 118);
  91.             this.Controls.Add(this.textBox2);
  92.             this.Controls.Add(this.textBox1);
  93.             this.Controls.Add(this.button1);
  94.             this.Name = "Form1";
  95.             this.Text = "Form1";
  96.             this.ResumeLayout(false);
  97.  
  98.         }
  99.         #endregion
  100.  
  101.         /// <summary>
  102.         /// 应用程序的主入口点。
  103.         /// </summary>
  104.         [STAThread]
  105.          lass="keyword">static void Main() 
  106.         {
  107.             Application.Run(new Form1());
  108.         }
  109.  
  110.         private void button1_Click(object sender, System.EventArgs e)
  111.         {
  112.             //创建tcpclient对象
  113.             TcpClient TClient = new TcpClient();
  114.             //创建与服务器的连接
  115.             TClient.Connect(IPAddress.Parse("127.0.0.1"),8000);
  116.             //获取网络流
  117.             NetworkStream ns = TClient.GetStream();
  118.             //创建流写入对象
  119.             StreamWriter sw = new StreamWriter(ns);
  120.             //写入数据
  121.             sw.WriteLine(this.textBox1.Text);
  122.             //清空缓冲
  123.             sw.Flush();
  124.             //创建流读取对象
  125.             StreamReader sr = new StreamReader(ns);
  126.             //读出数据
  127.             this.textBox2.Text=sr.ReadLine();
  128.             
  129.             //关闭
  130.             sr.Close();
  131.             sw.Close();
  132.             ns.Close();
  133.             TClient.Close();
  134.         }
  135.     }
  136. }
  137.  
  138. [/CODE]
  139. Server端源码:
  140. [CODE=csharp]
  141. using System;
  142. using System.IO;
  143. using System.Threading;
  144. using System.Net;
  145. using System.Net.Sockets;
  146.  
  147. namespace ServerTest
  148. {
  149.     /// <summary>
  150.     /// Class1 的摘要说明。
  151.     /// </summary>
  152.     class Class1
  153.     {
  154.         /// <summary>
  155.         /// 应用程序的主入口点。
  156.         /// </summary>
  157.         [STAThread]
  158.         static void Main(string[] args)
  159.         {
  160.             //
  161.             // TODO: 在此处添加代码以启动应用程序
  162.             //
  163.             Console.WriteLine("Running…..");
  164.             Thread thread = new Thread(new ThreadStart(Convert));
  165.             thread.Start();
  166.         }
  167.         /// <summary>
  168.         /// 静态的方法对字符转换
  169.         /// </summary>
  170.         static an> void Convert()
  171.         {
  172.             //创建监听对象并启动
  173.             TcpListener TListener = new TcpListener(8000);
  174.             TListener.Start();
  175.             
  176.             while(true)
  177.             {
  178.                 //获取socket
  179.                 Socket socket = TListener.AcceptSocket();
  180.                 //获取网络流
  181.                 NetworkStream ns = new NetworkStream(socket);
  182.                 //创建流读取对象
  183.                 StreamReader sr = new StreamReader(ns);
  184.                 //创建流写入对象
  185.                 StreamWriter sw = new StreamWriter(ns);
  186.                 //读取数据转换大小写并写入
  187.                 sw.WriteLine(sr.ReadLine().ToUpper());
  188.                 //关闭
  189.                 sw.Flush();
  190.  
  191.                 sw.Close();
  192.                 sr.Close();
  193.                 ns.Close();
  194.                 socket.Close();
  195.                 Console.WriteLine("OK");
  196.             }
  197.         }
  198.     }
  199. }

 

C#局域网聊天

2008.06.14 / 标签: / 分类: WinForm
Sofa

o(∩_∩)o…当然和QQ是没得比的了。主要是为了学习C#网络编程及winform高级编程。聊天程序截图如下: C#局域网聊天器源码:

 
  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.IO;
  9. using System.Net.Sockets;
  10. using System.Threading;
  11.  
  12. namespace Chat
  13. {
  14.     /// <summary>
  15.     /// Form1 的摘要说明。
  16.     /// </summary>
  17.     public class frmMain : System.Windows.Forms.Form
  18.     {
  19.         private System.Windows.Forms.Button btnConnect;
  20.         private System.Windows.Forms.TextBox txtPort1;
  21.         private System.Windows.Forms.TextBox txtPort2;
  22.         private System.Windows.Forms.ListBox lstInfo;
  23.         private System.Windows.Forms.Button btnSend;
  24.         private System.Windows.Forms.TextBox txtMsg;
  25.         /// <summary>
  26.         /// 必需的设计器变量。
  27.         /// </summary>
  28.         private System.ComponentModel.Container components = null;
  29.  
  30.         public frmMain()
  31.         {
  32.             //
  33.             // Windows 窗体设计器支持所必需的
  34.             //
  35.             InitializeComponent();
  36.  
  37.             //
  38.             // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  39.             //
  40.         }
  41.  
  42.         /// <summary>
  43.         /// 清理所有正在使用的资源。
  44.         /// </summary>
  45.         protected override void Dispose( bool disposing )
  46.         {
  47.             if( disposing )
  48.             {
  49.                 if (components != null
  50.                 {
  51.                     components.Dispose();
  52.                 }
  53.             }
  54.             base.Dispose( disposing );
  55.         }
  56.  
  57.         #region Windows 窗体设计器生成的代码
  58.         /// <summary>
  59.         /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  60.         /// 此方法的内容。
  61.         
    /// </summary>
  62.         private void InitializeComponent()
  63.         {
  64.             this.btnConnect = new System.Windows.Forms.Button();
  65.             this.txtPort1 = new System.Windows.Forms.TextBox();
  66.             this.txtPort2 = new System.Windows.Forms.TextBox();
  67.             this.lstInfo = new System.Windows.Forms.ListBox();
  68.             this.btnSend = new System.Windows.Forms.Button();
  69.             this.txtMsg = new System.Windows.Forms.TextBox();
  70.             this.SuspendLayout();
  71.             // 
  72.             // btnConnect
  73.             // 
  74.             this.btnConnect.Location = new System.Drawing.Point(200, 224);
  75.             this.btnConnect.Name = "btnConnect";
  76.             this.btnConnect.TabIndex = 0;
  77.             this.btnConnect.Text = "连接";
  78.             this.btnConnect.Click += new System.EventHandler(this.button1_Click);
  79.             // 
  80.             // txtPort1
  81.             // 
  82.             this.txtPort1.Location = new System.Drawing.Point(16, 224);
  83.             this.txtPort1.Name = "txtPort1";
  84.             this.txtPort1.Size = new System.Drawing.Size(64, 21);
  85.             this.txtPort1.TabIndex = 1;
  86.             this.txtPort1.Text = "";
  87.             // 
  88.             // txtPort2
  89.             // 
  90.             this.txtPort2.Location = new System.Drawing.Point(112, 224);
  91.             this.txtPort2.Name = "txtPort2";
  92.             this.txtPort2.Size = new System.Drawing.Size(64, 21);
  93.             this.txtPort2.TabIndex = 2;
  94.             this.txtPort2.Text = "";
  95.             // 
  96.             // lstInfo
  97.             // 
  98.             this.lstInfo.ItemHeight = 12;
  99.             this.lstInfo.Location = new System.Drawing.Point(16, 8);
  100.             this.lstInfo.Name = "lstInfo";
  101.             this.lstInfo.Size = new System.Drawing.Size(464, 136);
  102.             this.lstInfo.TabIndex = 3;
  103.             // 
  104.             // btnSend
  105.             // 
  106.             this.btnSend.Location = new System.Drawing.Point(328, 224);
  107.             this.btnSend.Name = "btnSend";
  108.             this.btnSend.TabIndex = 4;
  109.             this.btnSend.Text = "发送";
  110.             this.btnSend.Click += new System.EventHandler(this.button2_Click);
  111.             // 
  112.             // txtMsg
  113.             // 
  114.             this.txtMsg.Location = new System.Drawing.Point(16, 152);
  115.             this.txtMsg.Multiline = true;
  116.             this.txtMsg.Name = "txtMsg";
  117.             this.txtMsg.Size = new System.Drawing.Size(464, 56);
  118.             this.txtMsg.TabIndex = 5;
  119.             this.txtMsg.Text = "";
  120.             // 
  121.             // frmMain
  122.             // 
  123.             this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  124.             this.ClientSize = new System.Drawing.Size(488, 266);
  125.             this.Controls.Add(this.txtMsg);
  126.             this.Controls.Add(this.btnSend);
  127.             this.Controls.Add(this.lstInfo);
  128.             this.Controls.Add(this.txtPort2);
  129.             this.Controls.Add(this.txtPort1);
  130.             this.Controls.Add(this.btnConnect);
  131.             this.Name = "frmMain";
  132.             this.Text = "简单聊天";
  133.             this.ResumeLayout(false);
  134.  
  135.         }
  136.         #endregion
  137.  
  138.         /// <summary>
  139.         /// 应用程序的主入口点。
  140.         /// </summary>
  141.         [STAThread]
  142.         static void Main() 
  143.         {
  144.             Application.Run(new frmMain());
  145.         }
  146.  
  147.          class="keyword">private void button2_Click(object sender, System.EventArgs e)
  148.         {
  149.             //创建tcpclient对象
  150.             TcpClient TClient = new TcpClient();
  151.             //连接
  152.             TClient.Connect(IPAddress.Parse("127.0.0.1"),int.Parse(this.txtPort1.Text));
  153.             //获取网络流
  154.             NetworkStream ns = TClient.GetStream();
  155.             //创建流写入对象
  156.             StreamWriter sw =new StreamWriter(ns);
  157.             //写入信息
  158.             sw.WriteLine(this.txtMsg.Text);
  159.             //清空缓冲
  160.             sw.Flush();
  161.             //添加入聊天窗口
  162.             sw.Close();
  163.             ns.Close();
  164.             this.lstInfo.Items.Add("你说:"+DateTime.Now.ToString());
  165.             this.lstInfo.Items.Add(this.txtMsg.Text);
  166.             this.txtMsg.Text="";
  167.         }
  168.         /// <summary>
  169.         /// 数据接收方法
  170.         /// </summary>
  171.         void Recieve()
  172.         {
  173.             //创建监听对象并启动
  174.             TcpListener TListener = new TcpListener(int.Parse(this.txtPort2.Text));
  175.             TListener.Start();
  176.  
  177.             while(true)
  178.             {
  179.                 //创建socket
  180.                 Socket socket = TListener.AcceptSocket();
  181.                 //创建网络流对象
  182.                 NetworkStream ns = new NetworkStream(socket);
  183.                 //创建流读取对象
  184.                 StreamReader sr =new StreamReader(ns);
  185.                 //获取信息
  186.                 string line = sr.ReadLine();
  187.                 //写入聊天窗口
  188.                 this.lstInfo.Items.Add("对方说:"+DateTime.Now.ToString());
  189.                 this.lstInfo.Items.Add(line);
  190.                 sr.Close();
  191.                 ns.Close();
  192.                 socket.Close();
  193.             }
  194.         }
  195.  
  196.         private void button1_Click(ob
    ject
     sender, System.EventArgs e)
  197.         {
  198.             //点击此按钮时开始监听
  199.             Thread thread = new Thread(new ThreadStart(Recieve));
  200.             thread.Start();
  201.         }
  202.     }
  203. }

 

C#网页源码读取器

2008.06.11 / 标签: / 分类: WinForm

C#网页源码读取器,以前在网上见别人写过这个东西,用来查看某个网站中网页的源代码(若是动态网站,当看到的不是真正的源码)看看里面有没有被挂马,本来还觉得那东西挺神秘的,今天自己写了一下,原来如此easy o(∩_∩)o… 程序源码:

 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Net;
  9. using System.IO;
  10.  
  11. namespace PageCode
  12. {
  13.     public partial class frmMain : Form
  14.     {
  15.         public frmMain()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private void btnGet_Click(object sender, EventArgs e)
  21.         {
  22.             //创建WebClient对象
  23.             WebClient client = new WebClient();
  24.             //创建Stream流对象
  25.             Stream strm = client.OpenRead(txtUrl.Text);
  26.             //创建StreamReader对象
  27.             StreamReader sr = new StreamReader(strm);
  28.             //创建StringBuilder对象用于存储网页代码
  29.             StringBuilder sb = new StringBuilder();
  30.             string line;
  31.             //获取网页代码并存入sb中
  32.             while ((line = sr.ReadLine()) != null)
  33.             {
  34.                 sb.Append(line);
  35.             }
  36.             sr.Close();
  37.             strm.Close();
  38.             //显示读取到的代码
  39.             this.txtCode.Text = sb.ToString();
  40.         }
  41.     }
  42. }

 

Winform访问Internet

2008.06.11 / 标签: / 分类: WinForm
Sofa

.NET框架支持在应用程序中实现Internet服务,应用程序可以通过构建可插接式协议来充分利用新的Internet协议。Internet应用程序分为两种类型:客户端应用程序和服务器应用程序。客户端/服务器应用程序最常见的实例是万维网,可以使用浏览器在世界任何地方访问存储在web服务器上的数据。
阅读全文>>

无觅相关文章插件,快速提升流量