在C#中建立P2P连接
我意识到这个问题与类似 一些 其他,但我认为我的情况有足够的不同来保证自己的问题(希望如此) 。
我计划在另一个人的计算机上部署一个程序,该程序将打开与我的计算机的连接,之后我假设计算机应该能够相互通信。一旦程序启动,它应该能够提取地址信息和端口(如果它们没有被阻止)来创建连接,对吗?
更重要的是我住的宿舍的互联网配置。每个房间都分配了一个唯一的端口和由 DHCP 服务器分配的静态 IP 地址。我如何将其纳入程序设计中?
我认为此设置不需要服务器作为中间访问点,因为我的地址详细信息将始终保持不变,并且主机可以简单地连接到我的计算机而无需进一步的信息。这是正确的吗?
最后,我正在阅读几页有关创建连接的内容,但对 TcpConnection、WCF、CORBA 等的所有可能性感到困惑。假设我只想将消息发送到,哪一个对我来说实际上是最简单的开始另一台机器?
更新:
地址是静态的,即它不会改变。我的 IP 地址的格式为 130.83.20.xxx,我可以等待 DHCP 服务器为我分配该地址,也可以使用静态 IP 配置自行手动输入该地址。
至于消息本身,简单的短信就足以开始。前面提到的端口是交换机端口,我相信在网络编程期间不会发挥作用。
I realize this question is similar to some others, but I figured my situation is sufficiently different to warrant its own question (hopefully).
What I'm planning on is deploying a program on another person's computer which will open a connection to my computer after which, I'm assuming, the computers should be able to communicate with each other. Once the program starts, it should be able to pull the address information and port (if they aren't blocked) to create a connection, right?
What's more is the internet configuration of the dorm I'm living in. Every room is assigned a unique port and a static IP address assigned by a DHCP server. How do I factor this into the design of my program?
I'm thinking that this setup does not require a server as an intermediate access point, as my address details will always remain the same and the host computer can simply connect to my computer without further information. Is this correct?
Finally, I am reading a few pages about creating a connection, but am confused with all the possibilities of TcpConnection, WCF, CORBA, etc. Which one would actually be the simplest one for me to start with assuming I only want to send messages to the other machine?
Update:
The address is static in the sense that it doesn't change. My IP address is of the form 130.83.20.xxx and I can either wait for the DHCP server to assign me this address, or I can manually enter it myself using a static IP configuration.
As for the messages itself, simple text messages will suffice for the start. The ports mentioned before are the switch ports and do not come into play during network programming I believe.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会选择 TcpClient 和 < a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.aspx#Y2166" rel="noreferrer">TcpListener。查看 MSDN 上的示例代码,将其复制并粘贴到两个 C# 控制台项目中并构建它们。
我将使用 127.0.0.1 (localhost) 在端口 5001(常用的测试端口)上进行测试。
然后您应该能够测试仅在您的计算机上运行的简单客户端/服务器。一旦你开始工作,你可以将客户端带到宿舍的另一台计算机上,并确保它仍然可以工作。一旦成功,您就可以带着客户去当地的一家咖啡店。让服务器在宿舍中的已知 IP 地址上运行。在这种情况下,让服务器绑定到您的实际外部 IP(而不是本地主机)。您只需为 TcpListener 构造函数指定一个端口即可做到这一点: `
一旦您自己或与朋友一起完成所有工作,然后将其发送到外部。最好先让这些东西在演示中运行,然后再将其发送给您的客户并让他与您一起排除故障。 :)
我的答案背后的推理:
简单的 TCP 客户端/服务器非常简单,并且允许在两台计算机之间使用简单的聊天程序。然后您可以增强它以发送您想要的任何数据流。您可以添加如下代码来访问 StreamWriter:
I would go with TcpClient and TcpListener. Check out the example code on MSDN, copy and paste it into two C# console projects and build them.
I would use 127.0.0.1 (localhost) for testing purposes on port 5001 (a commonly used test port).
Then you should be able to test a simple client/server that runs on your computer only. Once you get that working, you can take the client to another computer in your dorm and make sure it still works. Once that works, you can go to a local coffee shop and take the client with you. Leave the server running at a known IP address in your dorm. In this case, have the server bind to your actual external IP (not localhost). You can do this by just specifying a port to the TcpListener constructor: `
Once you get all that working by yourself or with a friend, then send it external. Better to get these things working in a demo before sending it to your customer and having him troubleshoot with you. :)
Reasoning behind my answer:
Simple TCP client/server is very straightforward and will allow a simple chat program between two computers. Then you can beef it up to send any data stream you want. You can add code like this to get access to a StreamWriter: