浏览主题:???????????????????????????????????????????????????????????????????
主题:???????????????????????????????????????????????????????????????????
|
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 编辑标志 本帖最后由[Senlan]在 2007-11-21 14:03:31 编辑 |
|
RE:???????????????????????????????????????????????????????????????????
[ 2007-11-22 14:18:46 ] 一天来看好几回,好像还是没有人理 哎.. |
|
RE:???????????????????????????????????????????????????????????????????
[ 2007-11-22 18:31:46 ] 我在动数据库这块。。。 |

