| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1120 人关注过本帖
标题:[求助]关于P2P中的networkstream,我的用法貌似有错,望高手不吝赐教!
取消只看楼主 加入收藏
va1entinc
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-7-7
收藏
 问题点数:0 回复次数:2 
[求助]关于P2P中的networkstream,我的用法貌似有错,望高手不吝赐教!




在下是个学生,正在学习C#,在偶然的机会发现了这个不错的论坛。。。在下在这里虚心向各位前辈们请教了。。。
这个是老师布置的一项作业,是一个P2P的聊天工具,连接能正常连接,但发消息不知道能不能正常发,因为调试看不出来。。。但收消息肯定不能正常收。。。在发消息的一方退出的时候,收消息的一方才会收到消息,并进入死循环中。。。
以下是在下做的源代码,请前辈们帮忙看看。。
----------------------------------------------------------------------------------------------------------




using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace P2PTest
{
using System.Threading;
using System.Net.Sockets;
using System.IO;
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label lblConnet;
private System.Windows.Forms.TextBox txtIp;
private System.Windows.Forms.Label lblIp;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.Label lblName;
private System.Windows.Forms.Label lblExit;
private System.Windows.Forms.Label lblDisconnect;
private System.Windows.Forms.Label lblConnect;
private System.Windows.Forms.Label lblSend;
private System.Windows.Forms.RichTextBox rxtChat;
private System.Windows.Forms.RichTextBox rxtCharInfo;
private System.Windows.Forms.Label lblMain;

private TcpListener m_TcpListener;
private TcpClient m_TcpClient;
private NetworkStream m_NetworkStreamServer;
private NetworkStream m_NetworkStreamClient;
private StreamWriter m_StreamWriter;
private StreamReader m_StreamReader;
public Socket m_SocketForClient;
private string m_Name;
/// <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()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.panel1 = new System.Windows.Forms.Panel();
this.lblConnet = new System.Windows.Forms.Label();
this.txtIp = new System.Windows.Forms.TextBox();
this.lblIp = new System.Windows.Forms.Label();
this.txtName = new System.Windows.Forms.TextBox();
this.lblName = new System.Windows.Forms.Label();
this.lblExit = new System.Windows.Forms.Label();
this.lblDisconnect = new System.Windows.Forms.Label();
this.lblConnect = new System.Windows.Forms.Label();
this.lblSend = new System.Windows.Forms.Label();
this.rxtChat = new System.Windows.Forms.RichTextBox();
this.rxtCharInfo = new System.Windows.Forms.RichTextBox();
this.lblMain = new System.Windows.Forms.Label();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.BackColor
= System.Drawing.Color.FromArgb(((System.Byte)(255)),
((System.Byte)(255)), ((System.Byte)(192)));
this.panel1.Controls.Add(this.lblConnet);
this.panel1.Controls.Add(this.txtIp);
this.panel1.Controls.Add(this.lblIp);
this.panel1.Controls.Add(this.txtName);
this.panel1.Controls.Add(this.lblName);
this.panel1.Location = new System.Drawing.Point(244, 136);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(296, 104);
this.panel1.TabIndex = 15;
//
// lblConnet
//
this.lblConnet.Image = ((System.Drawing.Image)(resources.GetObject("lblConnet.Image")));
this.lblConnet.Location = new System.Drawing.Point(216, 16);
this.lblConnet.Name = "lblConnet";
this.lblConnet.Size = new System.Drawing.Size(72, 64);
this.lblConnet.TabIndex = 4;
this.lblConnet.Click += new System.EventHandler(this.lblConnet_Click);
//
// txtIp
//
this.txtIp.Location = new System.Drawing.Point(72, 62);
this.txtIp.Name = "txtIp";
this.txtIp.Size = new System.Drawing.Size(128, 21);
this.txtIp.TabIndex = 3;
this.txtIp.Text = "";
//
// lblIp
//
this.lblIp.Image = ((System.Drawing.Image)(resources.GetObject("lblIp.Image")));
this.lblIp.Location = new System.Drawing.Point(4, 56);
this.lblIp.Name = "lblIp";
this.lblIp.Size = new System.Drawing.Size(208, 32);
this.lblIp.TabIndex = 2;
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(72, 13);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(128, 21);
this.txtName.TabIndex = 1;
this.txtName.Text = "";
//
// lblName
//
this.lblName.Image = ((System.Drawing.Image)(resources.GetObject("lblName.Image")));
this.lblName.Location = new System.Drawing.Point(8, 8);
this.lblName.Name = "lblName";
this.lblName.Size = new System.Drawing.Size(200, 32);
this.lblName.TabIndex = 0;
//
// lblExit
//
this.lblExit.Image = ((System.Drawing.Image)(resources.GetObject("lblExit.Image")));
this.lblExit.Location = new System.Drawing.Point(4, 353);
this.lblExit.Name = "lblExit";
this.lblExit.Size = new System.Drawing.Size(112, 23);
this.lblExit.TabIndex = 14;
//
// lblDisconnect
//
this.lblDisconnect.Image = ((System.Drawing.Image)(resources.GetObject("lblDisconnect.Image")));
this.lblDisconnect.Location = new System.Drawing.Point(12, 316);
this.lblDisconnect.Name = "lblDisconnect";
this.lblDisconnect.TabIndex = 13;
//
// lblConnect
//
this.lblConnect.Image = ((System.Drawing.Image)(resources.GetObject("lblConnect.Image")));
this.lblConnect.Location = new System.Drawing.Point(12, 280);
this.lblConnect.Name = "lblConnect";
this.lblConnect.TabIndex = 12;
this.lblConnect.Click += new System.EventHandler(this.lblConnect_Click);
//
// lblSend
//
this.lblSend.Image = ((System.Drawing.Image)(resources.GetObject("lblSend.Image")));
this.lblSend.Location = new System.Drawing.Point(556, 315);
this.lblSend.Name = "lblSend";
this.lblSend.Size = new System.Drawing.Size(72, 64);
this.lblSend.TabIndex = 11;
this.lblSend.Click += new System.EventHandler(this.lblSend_Click);
//
// rxtChat
//
this.rxtChat.BackColor
= System.Drawing.Color.FromArgb(((System.Byte)(255)),
((System.Byte)(255)), ((System.Byte)(192)));
this.rxtChat.Location = new System.Drawing.Point(164, 312);
this.rxtChat.Name = "rxtChat";
this.rxtChat.Size = new System.Drawing.Size(392, 72);
this.rxtChat.TabIndex = 10;
this.rxtChat.Text = "";
//
// rxtCharInfo
//
this.rxtCharInfo.BackColor
= System.Drawing.Color.FromArgb(((System.Byte)(255)),
((System.Byte)(255)), ((System.Byte)(192)));
this.rxtCharInfo.Location = new System.Drawing.Point(164, 72);
this.rxtCharInfo.Name = "rxtCharInfo";
this.rxtCharInfo.Size = new System.Drawing.Size(472, 232);
this.rxtCharInfo.TabIndex = 9;
this.rxtCharInfo.Text = "";
//
// lblMain
//
this.lblMain.Dock = System.Windows.Forms.DockStyle.Fill;
this.lblMain.Image = ((System.Drawing.Image)(resources.GetObject("lblMain.Image")));
this.lblMain.Location = new System.Drawing.Point(0, 0);
this.lblMain.Name = "lblMain";
this.lblMain.Size = new System.Drawing.Size(672, 408);
this.lblMain.TabIndex = 8;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(672, 408);
this.Controls.Add(this.panel1);
this.Controls.Add(this.lblExit);
this.Controls.Add(this.lblDisconnect);
this.Controls.Add(this.lblConnect);
this.Controls.Add(this.lblSend);
this.Controls.Add(this.rxtChat);
this.Controls.Add(this.rxtCharInfo);
this.Controls.Add(this.lblMain);
this.Name = "Form1";
this.Text = "Form1";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void StartServer()
{
m_TcpListener = new TcpListener(8080);
m_TcpListener.Start();
MessageBox.Show("P2P聊天工具服务器已经启动!");
m_SocketForClient = m_TcpListener.AcceptSocket();
m_NetworkStreamServer = new NetworkStream(m_SocketForClient);
m_StreamReader = new StreamReader(m_NetworkStreamServer);
try
{
while(m_SocketForClient.Connected)
{
MessageBox.Show("有人上线!");
if(m_NetworkStreamServer!=null)
{
string msg = m_StreamReader.ReadLine();
this.rxtCharInfo.Text = msg + "\r\n";
m_NetworkStreamServer.Flush();
}
}
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
}

private void ConnectToServer()
{
try
{
m_TcpClient = new TcpClient(this.txtIp.Text, 8080);
}
catch
{
MessageBox.Show("建立连接出错!");
return;
}
this.m_Name = this.txtName.Text;
m_NetworkStreamClient = m_TcpClient.GetStream();
m_StreamWriter = new StreamWriter(m_NetworkStreamClient);
}

private void MsgSend()
{
if(this.rxtChat.Text == "")
{
MessageBox.Show("不能发送空消息!");
this.rxtChat.Focus();
return;
}
try
{
string msg = m_Name + this.rxtChat.Text;
this.rxtCharInfo.Text = msg + "\r\n";
m_NetworkStreamClient.Flush();
m_StreamWriter.Write(msg);
m_StreamWriter.Flush();
this.rxtChat.Clear();
this.rxtChat.Focus();
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
}

private void lblConnect_Click(object sender, System.EventArgs e)
{
Thread objThread = new Thread(new ThreadStart(StartServer));
objThread.Start();
}

private void lblConnet_Click(object sender, System.EventArgs e)
{
ConnectToServer();
}

private void lblSend_Click(object sender, System.EventArgs e)
{
MsgSend();
}

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
try
{
this.m_NetworkStreamClient.Close();
this.m_NetworkStreamServer.Close();
this.m_StreamReader.Close();
this.m_StreamWriter.Close();
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
}
}
}

[此贴子已经被作者于2007-7-8 10:48:24编辑过]

搜索更多相关主题的帖子: 源代码 用法 聊天工具 不吝 
2007-07-07 18:47
va1entinc
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-7-7
收藏
得分:0 
自己帮自己顶下下。。。
大家帮帮忙啊。。。

2007-07-07 21:59
va1entinc
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-7-7
收藏
得分:0 
改成红色的了。。。。

2007-07-08 10:50
快速回复:[求助]关于P2P中的networkstream,我的用法貌似有错,望高手不吝赐教! ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023282 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved