首页 新闻 搜索 专区 学院

跪求指点,C#聊天p2p,不知道 哪出问题?感激不尽

-1
悬赏园豆:80 [已解决问题] 解决于 2011-07-25 17:34

在form1里new出一个form2实现简单的聊天或者开两个form1程序聊天,老是提示说目标计算机拒绝,无法连接,求帮助啊!

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.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;


namespace WindowsFormsApplication9
{
public partial class Form1 : Form
{
private TcpClient tclient;
// public Form2 Form2;
private Thread f1,f2;
public Form1()
{
InitializeComponent();
Form2 fm2
= new Form2();
fm2.Show();
/*f3 = new Thread(new ThreadStart());
f4 = new Thread(new ThreadStart());
*/
}
private void Form1_Load(object sender, EventArgs e)
{
button2.Click
+= new EventHandler(button2_Click);
f1
= new Thread(new ThreadStart(server));
f1.Start();

}
private void server()
{
TcpListener tlisten
= new TcpListener(new IPEndPoint(IPAddress.Any,80));
tlisten.Start();
MessageBox.Show(
"start");
while (/*f1.IsAlive*/true)
{

tclient
= tlisten.AcceptTcpClient();

f2
= new Thread(new ThreadStart(receiving));
f2.Start();


tlisten.Stop();
}
}
private void receiving()
{ MessageBox.Show(
"accept");
NetworkStream networks
= tclient.GetStream();
try {
Byte[] receviebytes
= new byte[1001];
int i = networks.Read(receviebytes/*设置缓冲去*/ , 0, receviebytes.Length/*从缓冲大小去多少数*/ );
string a = Encoding.ASCII.GetString(receviebytes/*设置缓冲去*/, 0, i/*从缓冲大小去多少数*/ );
MessageBox.Show(
"yes");
this.textBox2.Text = this.textBox2.Text + a +" \r\n";//换行
}
catch (Exception e) { MessageBox.Show(e.ToString ());}
tclient.Close();
// listBox1.Tag = it.Address.ToString();/*加括号*/
}
private void button2_Click(object sender, EventArgs e)
{
try
{
// string host = Dns.GetHostName();
TcpClient tcent = new TcpClient(Dns .GetHostName (),80);
/* string host = Dns.GetHostName();
IPHostEntry s = Dns.GetHostByName(host);
IPEndPoint it = new IPEndPoint(s.AddressList[0], 80);
tcent.Connect(it);
*/
NetworkStream networks
= tcent.GetStream();
string t = textBox1.Text;
Byte[] sendbytes
= Encoding.ASCII.GetBytes(t);
networks.Write(sendbytes,
0, sendbytes.Length);
textBox1.Clear();
tcent.Close();
}
//MessageBox.Show(" what");
//tclient.Close();
catch (Exception g)
{ MessageBox.Show(g.ToString()); }

}

问题补充:

我有关掉防火墙,有这么难吗,帮我认真看看啊,搞了好久

cheatingdeath的主页 cheatingdeath | 初学一级 | 园豆:110
提问于:2011-07-22 16:41
< >
分享
最佳答案
0

两个问题

1.tlisten.Stop();停了谁监听啊

2.this.textBox2.Text = this.textBox2.Text + a +" \r\n";//换行
多线程是不能直接访问控件

有问题Q我:156074228

 

View Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private TcpClient tclient;
// public Form2 Form2;
private Thread f1, f2;
public Form1()
{
InitializeComponent();
/*f3 = new Thread(new ThreadStart());
f4 = new Thread(new ThreadStart());
*/
}

private void Form1_Load(object sender, EventArgs e)
{
button2.Click
+= new EventHandler(button2_Click);
f1
= new Thread(new ThreadStart(server));
f1.Start();
}

private void server()
{
TcpListener tlisten
= new TcpListener(new IPEndPoint(IPAddress.Any, 1000));

tlisten.Start();
MessageBox.Show(
"start");
while (/*f1.IsAlive*/true)
{
tclient
= tlisten.AcceptTcpClient();
f2
= new Thread(new ThreadStart(receiving));
f2.Start();
}
}

private void receiving()
{
MessageBox.Show(
"accept");
NetworkStream networks
= tclient.GetStream();
try
{
Byte[] receviebytes
= new byte[1001];
int i = networks.Read(receviebytes/*设置缓冲去*/ , 0, receviebytes.Length/*从缓冲大小去多少数*/ );
string a = Encoding.ASCII.GetString(receviebytes/*设置缓冲去*/, 0, i/*从缓冲大小去多少数*/ );
MessageBox.Show(
"yes");

UpdateMessage(a);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
tclient.Close();
// listBox1.Tag = it.Address.ToString();/*加括号*/
}

public delegate void UpdateMessageHandle(string message);
private void UpdateMessage(string message)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new UpdateMessageHandle(UpdateMessage), new object[] { message });
return;
}

this.textBox2.Text = this.textBox2.Text + message + " \r\n";//换行
}

private void button2_Click(object sender, EventArgs e)
{
try
{
// string host = Dns.GetHostName();
TcpClient tcent = new TcpClient(Dns.GetHostName(), 1000);
/* string host = Dns.GetHostName();
IPHostEntry s = Dns.GetHostByName(host);
IPEndPoint it = new IPEndPoint(s.AddressList[0], 80);
tcent.Connect(it);
*/
NetworkStream networks
= tcent.GetStream();
string t = textBox1.Text;
Byte[] sendbytes
= Encoding.ASCII.GetBytes(t);
networks.Write(sendbytes,
0, sendbytes.Length);
textBox1.Clear();
tcent.Close();
}
//MessageBox.Show(" what");
//tclient.Close();
catch (Exception g)
{
MessageBox.Show(g.ToString());
}
}
}
}
收获园豆:80
彭汉生 | 小虾三级 |园豆:1233 | 2011-07-23 15:19
谢谢啊,还是不行啊,我加你了,还是目标计算机拒绝!
cheatingdeath | 园豆:110 (初学一级) | 2011-07-23 20:41
QQ发了离线文件给你
彭汉生 | 园豆:1233 (小虾三级) | 2011-07-25 13:35
其他回答(3)
0

你够狠,用80端口。

dataxiu.com | 园豆:294 (菜鸟二级) | 2011-07-22 18:17
改成其他也没有用啊,有这么难吗,哎!
支持(0) 反对(0) cheatingdeath | 园豆:110 (初学一级) | 2011-07-22 19:37
0

你直接阻塞UI线程,不死才怪!

大石头 | 园豆:110 (初学一级) | 2011-07-25 08:28
0

园子里已经有非常成熟的通信框架ESFramework,可以用来做类似QQ的IM软件,支持P2P,传文件,而且还可以支持语音视频聊天,我们公司就是用的该框架来开发视频会议系统的。可以参考一下。

C#开源即时通讯GGTalk | 园豆:162 (初学一级) | 2013-04-26 10:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册