我做了个简单的聊天程序,在局域网中运行正常,现在我想把它修改成能在广域网上运行,查了很多资料,还是没有搞定。代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Net.Sockets;
using System.IO;
namespace chat
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private Thread MyListenThead;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.RichTextBox richTextBox3;
private System.Windows.Forms.Button button1;
private TcpListener MyTcpListener; private void startListen()
{
MyTcpListener=new TcpListener(888);
MyTcpListener.Start();
while(true)
{
TcpClient myTcpClient=MyTcpListener.AcceptTcpClient();
NetworkStream myStream=myTcpClient.GetStream();
byte[] myByte=new byte[1024];
int myBytesRead=myStream.Read(myByte,0,myByte.Length);
//string myMsg=System.Text.Encoding.Default.GetString(myByte,0,myBytesRead);
//byte[] byteM=System.Text.Encoding.GetEncoding("gb2312").GetBytes(Msg1);
string myMsg=System.Text.Encoding.UTF8.GetString(myByte,0,myBytesRead);
this.richTextBox2.Text += myMsg;
}
} 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()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.richTextBox3 = new System.Windows.Forms.RichTextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
// 
// textBox1
// 
this.textBox1.Location = new System.Drawing.Point(104, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(200, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "192.168.23.250";
// 
// textBox2
// 
this.textBox2.Location = new System.Drawing.Point(336, 8);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 1;
this.textBox2.Text = "wgf";
// 
// richTextBox1
// 
this.richTextBox1.Location = new System.Drawing.Point(16, 48);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(576, 72);
this.richTextBox1.TabIndex = 2;
this.richTextBox1.Text = "inf";
// 
// richTextBox2
// 
this.richTextBox2.Location = new System.Drawing.Point(16, 128);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(248, 144);
this.richTextBox2.TabIndex = 3;
this.richTextBox2.Text = "his";
// 
// richTextBox3
// 
this.richTextBox3.Location = new System.Drawing.Point(280, 128);
this.richTextBox3.Name = "richTextBox3";
this.richTextBox3.Size = new System.Drawing.Size(312, 144);
this.richTextBox3.TabIndex = 4;
this.richTextBox3.Text = "get";
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(456, 8);
this.button1.Name = "button1";
this.button1.TabIndex = 5;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(608, 301);
this.Controls.Add(this.button1);
this.Controls.Add(this.richTextBox3);
this.Controls.Add(this.richTextBox2);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.Closed += new System.EventHandler(this.Form1_Closed);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{
this.MyListenThead=new Thread(new ThreadStart(startListen));
this.MyListenThead.Start();
} private void button1_Click(object sender, System.EventArgs e)
{
if(this.textBox1.Text.Length<1 || this.textBox2.Text.Length<1 || this.richTextBox1.Text.Length<1)
{
return;
}
try
{
string myMsg= this.textBox2.Text + ":" + this.richTextBox1.Text + "\n";
TcpClient myClient=new TcpClient(this.textBox1.Text,888);
NetworkStream myTcpStream =myClient.GetStream();
StreamWriter myStream =new StreamWriter(myTcpStream);
myStream.Write(myMsg);
myStream.Flush();
myStream.Close();
myClient.Close();
this.richTextBox3.AppendText(this.richTextBox1.Text+"\n");
this.richTextBox1.Clear(); }
catch(Exception Err)
{
MessageBox.Show(Err.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
} private void Form1_Closed(object sender, System.EventArgs e)
{
try
{
if(this.MyTcpListener != null)
{
this.MyTcpListener.Stop();
}
if(this.MyListenThead != null)
{
if(this.MyListenThead.ThreadState==ThreadState.Running)
{
this.MyListenThead.Abort();
}

}
}
catch(Exception err)
{
MessageBox.Show(err.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information); }
}
}
}

解决方案 »

  1.   

    在广域网上跑没啥区别啊.只不过IP变成公网IP而已....至于是内网的机,IP自然由路由器NAT来搞定.不用操心..
      

  2.   

    这不是程序本身的问题了。一般情况下,整个局域网都是利用同一个IP地址来上网的,如果你想连接或访问一个局域网中的其中一台机器,一般情况下会被交换机或路由器拒绝,而如果你在局域网中想访问一个公网IP地址,则可以,局域网的交换机会把数据包的源地址和端口改成外网的再发送。所以,如果你的程序处于局域网内,但却一定要访问另一个局域网中的其中一台机器的话,就得用P2P技术了。这东东都可以写成几本书了!
      

  3.   

    使用TCP或UDP渗透打洞请求就能搞定,私网就能搞定
      

  4.   

    采用P2P技术,解决Nat问题。
      

  5.   

    UDP穿越NAT的原理首先先介绍一些基本概念:
        NAT(Network Address Translators),网络地址转换:网络地址转换是在IP地址日益缺乏的情况下产生的,它的主要目的就是为了能够地址重用。NAT分为两大类,基本的NAT和NAPT(Network Address/Port Translator)。
        最开始NAT是运行在路由器上的一个功能模块。
        
        最先提出的是基本的NAT,它的产生基于如下事实:一个私有网络(域)中的节点中只有很少的节点需要与外网连接(呵呵,这是在上世纪90年代中期提出的)。那么这个子网中其实只有少数的节点需要全球唯一的IP地址,其他的节点的IP地址应该是可以重用的。
        因此,基本的NAT实现的功能很简单,在子网内使用一个保留的IP子网段,这些IP对外是不可见的。子网内只有少数一些IP地址可以对应到真正全球唯一的IP地址。如果这些节点需要访问外部网络,那么基本NAT就负责将这个节点的子网内IP转化为一个全球唯一的IP然后发送出去。(基本的NAT会改变IP包中的原IP地址,但是不会改变IP包中的端口)
        关于基本的NAT可以参看RFC 1631
        
        另外一种NAT叫做NAPT,从名称上我们也可以看得出,NAPT不但会改变经过这个NAT设备的IP数据报的IP地址,还会改变IP数据报的TCP/UDP端口。基本NAT的设备可能我们见的不多(呵呵,我没有见到过),NAPT才是我们真正讨论的主角。看下图:
                                    Server S1                         
                             18.181.0.31:1235                          
                                          |
              ^  Session 1 (A-S1)  ^      |  
              |  18.181.0.31:1235  |      |   
              v 155.99.25.11:62000 v      |    
                                          |
                                         NAT
                                     155.99.25.11
                                          |
              ^  Session 1 (A-S1)  ^      |  
              |  18.181.0.31:1235  |      |  
              v   10.0.0.1:1234    v      |  
                                          |
                                       Client A
                                    10.0.0.1:1234
        有一个私有网络10.*.*.*,Client A是其中的一台计算机,这个网络的网关(一个NAT设备)的外网IP是155.99.25.11(应该还有一个内网的IP地址,比如10.0.0.10)。如果Client A中的某个进程(这个进程创建了一个UDP Socket,这个Socket绑定1234端口)想访问外网主机18.181.0.31的1235端口,那么当数据包通过NAT时会发生什么事情呢?
        首先NAT会改变这个数据包的原IP地址,改为155.99.25.11。接着NAT会为这个传输创建一个Session(Session是一个抽象的概念,如果是TCP,也许Session是由一个SYN包开始,以一个FIN包结束。而UDP呢,以这个IP的这个端口的第一个UDP开始,结束呢,呵呵,也许是几分钟,也许是几小时,这要看具体的实现了)并且给这个Session分配一个端口,比如62000,然后改变这个数据包的源端口为62000。所以本来是(10.0.0.1:1234->18.181.0.31:1235)的数据包到了互联网上变为了(155.99.25.11:62000->18.181.0.31:1235)。
        一旦NAT创建了一个Session后,NAT会记住62000端口对应的是10.0.0.1的1234端口,以后从18.181.0.31发送到62000端口的数据会被NAT自动的转发到10.0.0.1上。(注意:这里是说18.181.0.31发送到62000端口的数据会被转发,其他的IP发送到这个端口的数据将被NAT抛弃)这样Client A就与Server S1建立以了一个连接。    呵呵,上面的基础知识可能很多人都知道了,那么下面是关键的部分了。
        看看下面的情况:
        Server S1                                     Server S2
     18.181.0.31:1235                              138.76.29.7:1235
            |                                             |
            |                                             |
            +----------------------+----------------------+
                                   |
       ^  Session 1 (A-S1)  ^      |      ^  Session 2 (A-S2)  ^
       |  18.181.0.31:1235  |      |      |  138.76.29.7:1235  |
       v 155.99.25.11:62000 v      |      v 155.99.25.11:62000 v
                                   |
                                Cone NAT
                              155.99.25.11
                                   |
       ^  Session 1 (A-S1)  ^      |      ^  Session 2 (A-S2)  ^
       |  18.181.0.31:1235  |      |      |  138.76.29.7:1235  |
       v   10.0.0.1:1234    v      |      v   10.0.0.1:1234    v
                                   |
                                Client A
                             10.0.0.1:1234
        接上面的例子,如果Client A的原来那个Socket(绑定了1234端口的那个UDP Socket)又接着向另外一个Server S2发送了一个UDP包,那么这个UDP包在通过NAT时会怎么样呢?
        这时可能会有两种情况发生,一种是NAT再次创建一个Session,并且再次为这个Session分配一个端口号(比如:62001)。另外一种是NAT再次创建一个Session,但是不会新分配一个端口号,而是用原来分配的端口号62000。前一种NAT叫做Symmetric NAT,后一种叫做Cone NAT。我们期望我们的NAT是第二种,呵呵,如果你的NAT刚好是第一种,那么很可能会有很多P2P软件失灵。(可以庆幸的是,现在绝大多数的NAT属于后者,即Cone NAT)
       
        好了,我们看到,通过NAT,子网内的计算机向外连结是很容易的(NAT相当于透明的,子网内的和外网的计算机不用知道NAT的情况)。
        但是如果外部的计算机想访问子网内的计算机就比较困难了(而这正是P2P所需要的)。
        那么我们如果想从外部发送一个数据报给内网的计算机有什么办法呢?首先,我们必须在内网的NAT上打上一个“洞”(也就是前面我们说的在NAT上建立一个Session),这个洞不能由外部来打,只能由内网内的主机来打。而且这个洞是有方向的,比如从内部某台主机(比如:192.168.0.10)向外部的某个IP(比如:219.237.60.1)发送一个UDP包,那么就在这个内网的NAT设备上打了一个方向为219.237.60.1的“洞”,(这就是称为UDP Hole Punching的技术)以后219.237.60.1就可以通过这个洞与内网的192.168.0.10联系了。(但是其他的IP不能利用这个洞)。
        
        呵呵,现在该轮到我们的正题P2P了。有了上面的理论,实现两个内网的主机通讯就差最后一步了:那就是鸡生蛋还是蛋生鸡的问题了,两边都无法主动发出连接请求,谁也不知道谁的公网地址,那我们如何来打这个洞呢?我们需要一个中间人来联系这两个内网主机。
        现在我们来看看一个P2P软件的流程,以下图为例:                       Server S (219.237.60.1)
                              |
                              |
       +----------------------+----------------------+
       |                                             |
     NAT A (外网IP:202.187.45.3)                 NAT B (外网IP:187.34.1.56)
       |   (内网IP:192.168.0.1)                      | (内网IP:192.168.0.1)
       |                                             |
    Client A  (192.168.0.20:4000)             Client B (192.168.0.10:40000)    首先,Client A登录服务器,NAT A为这次的Session分配了一个端口60000,那么Server S收到的Client A的地址是202.187.45.3:60000,这就是Client A的外网地址了。同样,Client B登录Server S,NAT B给此次Session分配的端口是40000,那么Server S收到的B的地址是187.34.1.56:40000。
        此时,Client A与Client B都可以与Server S通信了。如果Client A此时想直接发送信息给Client B,那么他可以从Server S那儿获得B的公网地址187.34.1.56:40000,是不是Client A向这个地址发送信息Client B就能收到了呢?答案是不行,因为如果这样发送信息,NAT B会将这个信息丢弃(因为这样的信息是不请自来的,为了安全,大多数NAT都会执行丢弃动作)。现在我们需要的是在NAT B上打一个方向为202.187.45.3(即Client A的外网地址)的洞,那么Client A发送到187.34.1.56:40000的信息,Client B就能收到了。这个打洞命令由谁来发呢,呵呵,当然是Server S。
        总结一下这个过程:如果Client A想向Client B发送信息,那么Client A发送命令给Server S,请求Server S命令Client B向Client A方向打洞。呵呵,是不是很绕口,不过没关系,想一想就很清楚了,何况还有源代码呢(侯老师说过:在源代码面前没有秘密 8)),然后Client A就可以通过Client B的外网地址与Client B通信了。
        
        注意:以上过程只适合于Cone NAT的情况,如果是Symmetric NAT,那么当Client B向Client A打洞的端口已经重新分配了,Client B将无法知道这个端口(如果Symmetric NAT的端口是顺序分配的,那么我们或许可以猜测这个端口号,可是由于可能导致
      

  6.   

    根据楼上NAT原理,局域网内可以使用的程序,可以不用修改直接在广域网上使用
      

  7.   

    公司上班不能用QQ和MSN,实在是无聊,就自己做了一个局域网的聊天工具。
    有兴趣的朋友可以试试看。不需要建立服务器!!!名称:EasyMessenger
    版本:2006 第2版主要功能介绍:
     点对点私聊
     多人群聊
     自定义表情
     发送网络图片
     即时传送文件
     历史记录保存有需要的朋友可以来信,可以自己定义需要的群组。下载页面:
    http://www.easymessenger.cn