Power by DMForum.NET!
RSS | ATOM | WAP
DMForum.NET 官方支持讨论区 > 技术区 > 应用程序开发

浏览主题:???????????????????????????????????????????????????????????????????

主题:???????????????????????????????????????????????????????????????????
Senlan
普通会员
3级
等级:论坛游侠
发帖:106
金钱:310
经验:1675
性别:帅哥
状态:离线
注册:2006-8-5 9:24:00
开始学习C#了,抄了一例子,老有问题,请老大们帮我看看  
[ 2007-11-21 13:51:23 ]

c# 程序代码

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.Threading;
namespace p2pChat
{
             
public partial class Form1 : Form
             {
                             
private bool listenerRun = true;
                             
private TcpListener tcpl;
                             
private Thread th;

                             
public Form1()
                             {
                                             InitializeComponent();
                             }

                             
private void Listen()
                             {
                                             
try
                                             {
                                                             tcpl 
= new TcpListener(5656);
                                                             tcpl.Start();
                                                             toolStripStatusLabel2.Text 
= "正在监听";
                                                             
while (listenerRun)
                                                             {
                                                                             Socket s 
= tcpl.AcceptSocket();
                                                                             Byte[] stream 
= new Byte[80];
                                                                             
int i = s.Receive(stream);
                                                                             
string message = System.Text.Encoding.UTF8.GetString(stream);
                                                                             richTextBox1.AppendText(message);
                                                             }
                                             }
                                             
catch (System.Security.SecurityException)
                                             {
                                                             MessageBox.Show(
"防火墙安全错误!""错误",
                                                                             MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                             }
                                             
catch (Exception)
                                             {
                                                             toolStripStatusLabel2.Text 
= "有错误发生,已停止监听!";
                                             }
                             }
                             
private void Send()
                             {
                                             
try
                                             {
                                                             
string msg = "<" + textBox3.Text + ">" + textBox2.Text;
                                                             TcpClient tcpc 
= new TcpClient(textBox1.Text, 5656);
                                                             NetworkStream tcpStream 
= tcpc.GetStream();
                                                             StreamWriter reqStreamW 
= new StreamWriter(tcpStream);
                                                             reqStreamW.Write(msg);
                                                             reqStreamW.Flush();
                                                             tcpStream.Close();
                                                             tcpc.Close();
                                                             richTextBox1.AppendText(msg);
                                                             textBox2.Clear();
                                             }
                                             
catch (Exception)
                                             {
                                                             toolStripStatusLabel2.Text 
= "目标计算机拒绝连接请求!";
                                             }
                             }
                             
private void Stop()
                             {
                                             tcpl.Stop();
                                             th.Abort();
                             }

                             [STAThread]
                             
                             
private void button1_Click(object sender, System.EventArgs e)
                             {
                                             button1.Enabled 
= false;
                                             button2.Enabled 
= true;
                                             Thread th 
= new Thread(new ThreadStart(Listen));
                                             th.Start();
                             }
                             
private void button2_Click(object sender, System.EventArgs e)
                             {
                                             button1.Enabled 
= true;
                                             button2.Enabled 
= false;
                                             listenerRun 
= false;
                                             Stop();
                             }
                             
private void button3_Click(object sender, System.EventArgs e)
                             {
                                             
//MessageBox.Show("防火墙安全错误", "错误");
                                             Send();
                             }
                             
private void button4_Click(object sender, EventArgs e)
                             {
                             
                                             Application.Exit();
                             }
                             
                         

             }

}

现在的问题是,点"开始监听",状态栏能显示已经开始监听,但是点"发送消息"和"停止监听"却没有一点反应,程序就停在那里

看了很多次书上的例子,没有发现在错误,但是就是不能正常运行...

那位高手帮看看,谢谢了

上传的附件 rar
p2pChat.rar 35.635 KB
编辑标志
本帖最后由[Senlan]在 2007-11-21 14:03:31 编辑
Senlan
普通会员
3级
等级:论坛游侠
发帖:106
金钱:310
经验:1675
性别:帅哥
状态:离线
注册:2006-8-5 9:24:00
RE:???????????????????????????????????????????????????????????????????  
[ 2007-11-22 14:18:46 ]

一天来看好几回,好像还是没有人理

哎..

叶子
管理员
7级
荣誉:老会员
等级:骑士
发帖:2452
金钱:3530
经验:19595
积分:40
性别:帅哥
状态:离线
注册:2007-8-7 23:34:00
RE:???????????????????????????????????????????????????????????????????  
[ 2007-11-22 18:31:46 ]

我在动数据库这块。。。

多啦A梦
(靓仔)
普通会员
5级
等级:战士
发帖:179
金钱:660
经验:2552
性别:帅哥
状态:离线
注册:2006-7-29 23:38:00
RE:???????????????????????????????????????????????????????????????????  
[ 2007-12-7 9:45:02 ]

这种程序写得不爽啊。。

c#的精华我觉得是把复杂的程序结构条理化。。


回到顶部
Powered by DMForum.NET  1.4 Beta . URIMAP enabled. DEBUG enabled.
Copyright © 2003-2006 Devms.Com. All rights reserved.