把这两个贴子的分数一同加上:
http://www.csdn.net/expert/TopicView.asp?id=243694
http://www.csdn.net/expert/TopicView.asp?id=242305

解决方案 »

  1.   

    to Kevin_qing斑竹,我绝对没那个意思!早两天发个的那个贴子的确是欠考虑,在这里像斑竹,和vc版道歉了,你看我连续发了三个贴子.为了就是解决这个问题,请帮助了.
    maxsuy你说容易,请你讲讲
      

  2.   

    看上去好像muder兄的见解是对的。
      

  3.   

    这个涉及到NAT
    另外两个局域网没有server中转的话一定是不可能直接发送的,但是可以早开始的时候通过Server转包,然后得到端口就可以直接通讯了。
      

  4.   

    to Kevin_qing斑竹,NAT是什么?(中文意思)你说的得到端口,这个端口是不是应用程序端口?是不是对方通过这个端口就可以定位到局域网里的机子呢?但是我觉得处于一个局域网里的不同机子的端口难道是一定不同的吗?
      

  5.   

    如果用了NAT,这些都是透明的,就好像局域网中的机子直接连到外面一样。WinRoute有NAT功能:
    Network Address Translation (NAT) is a process that modifies packets sent from/to the local area network to/from the Internet or other IP based networks.On the way out
    Packets passing through the address translator engine on the way from the LAN are changed or translated to look as if they came from the computer running NAT (this computer is directly connected to the Internet). What actually happens is the "source" IP address is changed in the header replaced by (public) IP address of the "NAT" computer.The NAT engine also creates a record table of information for each packet that passed through to the Internet.On the way back
    Packets passing through the NAT on the way to the LAN are searched against to the records kept by the NAT engine. There the "destination" IP address is changed (based on the records in the database) back to the specific internal private class IP address in order to reach the computer on the LAN .Remember that the packet came with the public IP address of the NAT computer as a "destination" originally. The NAT engine had to change this information in order to deliver the packet to the correct recipient within the local network.
      

  6.   

    那这个网关上面映射的端口怎么样得到呢?怎么样设置呢?
    HowUCan,我先看看你给的资料了,先谢谢了;)
      

  7.   

    c1->gatway1->server ip1:port1
    c2->gatway2->server ip2:port2server ->c1(ip2:port2)
    server ->c2(ip1:port1)
    c1<->gatway1<->gatway2<->c2
      

  8.   

    那如果要实现两台处于不同局域网中的机子的udp通信,编程怎么实现?要增加什么特殊的代码(是不是要实现nat的端口映射)?
      

  9.   

    现在有个帖子你难道没看见吗?就在你的问题下面。
    ?QQ,C/S UDP原码的请进。
      

  10.   

     to masterjames那个是面向局域网的
      

  11.   

    我想如果不同网段之间要通信,一定要有个中转者。若这个中转者是网关或者NAT Server时,在客户端的编程中不必区分是否同在一个局域网内,反正往某个IP地址发就是了。若这个中转者是代理服务器,我不知道怎么做。
      

  12.   

    还没有明白啊?程序上面不需要做改动。
    但是需要照我说的哪个流程来做。也就是先由两个client像某一个server发消息,得到上面映射的端口
    然后这个server通知他们对方的端口,然后就可以通信了
      

  13.   

    明白了,明白了!就是客户端先向服务器端(已知ip和端口)发udp消息,服务器端可以通过,recvfrom函数得到客户端的ip和端口.这个ip和端口应该是客户端网关分配给要通信的机子的端口(也就是形成了端口映射),然后服务器就把这个端口和ip发给该用户的好友.那意思是该用户的所有的好友应该是都用这个ip和端口向该用户发消息.是不是这样,斑竹?
      

  14.   

    对~
    另外要注意的是gatway上面映射的端口是有超时时间的,所以需要定时发送些数据,以保持端口映射不会被close。
      

  15.   

    由于这个贴子得分有限,所以都给Kevin_qing斑竹和HowUCan了,欢迎关注的兄弟们去我另外的贴子领分