在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()); }
}
我有关掉防火墙,有这么难吗,帮我认真看看啊,搞了好久
两个问题
1.tlisten.Stop();停了谁监听啊
2.this.textBox2.Text = this.textBox2.Text + a +" \r\n";//换行
多线程是不能直接访问控件
有问题Q我:156074228
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端口。
你直接阻塞UI线程,不死才怪!