zoukankan      html  css  js  c++  java
  • 基于p2p聊天室的原理介绍.个人学习笔记

           在博客园里也算有二个多月了,在这里我学到了很多东西,也想把自己学到的东西分享给大家,说到网络编程其实自己也是没学到什么高深的东西,只是个人兴趣粗略的看了一下,一路都是自己摸索过来的。说得不好请大家不要在意,毕竟还没有大学毕业,老师也没有讲到这门课程。这完全是个人理解。

           【说到他的原理我觉得他有点像文件的读写操作】

    【发送内容】当我们要保存文字的内容时,我们首先是声明一个filestream,然后通过他的write()方法把内容写到文本里面去。

    基于p2p,内容的发送也一样,有点像文本的写入,而不同的是,这里用到NetworkStream网络工作流这一东西,然后再通过StreamWriter的write方法把内容写到指定的ip主机上。而使用

    NetworkStream时,要先实例化TcpClient对象【TcpClient client = new TcpClient(Dns.GetHostName(), 888)】他有点像filestream构造函数里面的路径这一参数,第一个参数为主机名,第二个参数为端口号

     TcpClient client = new TcpClient(Dns.GetHostName(), 888);//实例化Tcpclient
    NetworkStream netstream = client.GetStream();//获取网络工作流
    StreamWriter wstream = new StreamWriter(netstream, Encoding.Default);//实例化写入流
    wstream.Write(“内容”);

    【接收内容】接收内容时,要在一个线程上,对某一端口进行侦听。侦听接受到来自发送方的请求,然后读取数据,最后再把数据给接受方

                     TcpListener tcpListener = new TcpListener(888);//指定对某一端口创建侦听
    tcpListener.Start();//开始侦听
    TcpClient tclient = tcpListener.AcceptTcpClient(); //接受连接请求
    NetworkStream nstream = tclient.GetStream(); //获取数据流
    byte[] mbyte = new byte[1024]; //建立缓存
    int i = nstream.Read(mbyte, 0, mbyte.Length); //将数据流写入缓存
    string message = Encoding.Default.GetString(mbyte,0,i);
    MessageBox.Show(message);

    好了,你们对比一下内容的发送和接收是不是与文本文件的读写像相似。
    再符上一个较完整的硬代码(对与某一主机通信,自己改一下ip就可以了,这里用的是用一主机进行通信)

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
    namespace WindowsFormsApplication1
    {
    public partial class Form1 : Form
    {

    string message = "";//内容
    public Form1()
    {
    InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    Thread td = new Thread(new ThreadStart(this.liste));
    td.Start();
    }
    private void button1_Click(object sender, EventArgs e)//发送内容
    {
    try
    {
    TcpClient client = new TcpClient(Dns.GetHostName(), 888);//实例化Tcpclient
    NetworkStream netstream = client.GetStream();//获取网络工作流
    StreamWriter wstream = new StreamWriter(netstream, Encoding.Default);//实例化写入流
    wstream.Write(textBox1.Text);//将字符写入流
    //wstream.Flush();
    //wstream.Close();
    //client.Close();
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }
    private void liste()//侦听内容
    {
    try
    {
    TcpListener tcpListener = new TcpListener(888);//指定对某一端口创建侦听
    tcpListener.Start();//开始侦听
    TcpClient tclient = tcpListener.AcceptTcpClient(); //接受连接请求,中单位

               NetworkStream nstream = tclient.GetStream(); //获取数据流
    byte[] mbyte = new byte[1024]; //建立缓存
    int i = nstream.Read(mbyte, 0, mbyte.Length); //将数据流写入缓存
    message = Encoding.Default.GetString(mbyte,0,i);
    }
    catch (Exception exp)
    {
    MessageBox.Show(exp.Message);
    }
    }
    private void button2_Click(object sender, EventArgs e)//显内容
    {
    MessageBox.Show(message);
    }
    }
    }

        

  • 相关阅读:
    开源APM应用性能管理工具调研
    Inside ARC — to see the code inserted by the compiler
    报表应用系统中怎样正确使用图表功能
    创建cifs系统案例之“实现将Windows磁盘共享至Linux”
    Eclipse快捷键 10个最有用的快捷键
    如何生成KeyStore
    android中调用系统的发送短信、发送邮件、打电话功能
    android自带theme
    Android 报错:Conversion to Dalvik format failed: Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace.
    Android oncreate onupgrade什么时候被调用
  • 原文地址:https://www.cnblogs.com/lifeOfIT/p/2309286.html
Copyright © 2011-2022 走看看