Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

winforms - Public Chat Program: Windows Forms, C#


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your problem isn't about C# or WinForms, it's about networking and how TCP/IP networks operate. You won't be able to (easily) connect directly to your friend's computer because his computer is on another private network.

If it's a home computer, that network likely has a router which connects it to the internet. That router presents a single public IP address to the outside world, which is shared by all the computers within it, for the purpose of internet communication . It probably uses Network Address Translation to translate between each individual device's local IP (see below) and the public IP when forwarding data. It also probably implements a firewall which prevents incoming connections.

For internal communication each machine, and the router, will have local IP addresses which are unique within that network, but are almost certainly not unique across the entire internet - but since these addresses are not visible outside the private network, that doesn't matter.

So, even if you try to send a message to the public IP address, which is reachable from anywhere, by default the router won't send the information on to a specific machine within that network. Firstly, the firewall prevents it (to stop malicious traffic entering the network), and secondly it doesn't know which specific device the message is intended for anyway.

Most routers offer a feature called Port Forwarding where you can configure it to accept incoming messages on a specific TCP port and forward them on to a specific device on the network. This would help you in your situation, but note that it can create a security vulnerability in your network and should be used with caution, only when you know what you're doing.

(If you're wondering, big company networks operate in a broadly similar way, with public and private IP addresses, but the actual implementation and features are more complex...the broad outcome is the same in terms of individual machines being inaccessible from outside the network. You don't, at this stage, need to worry about the other details.)

Note that the public IP of a private, domestic network can often change - Internet Service Providers don't usually provide a static IP for that type of network. So every time you want to connect, you will need to check with your friend what their current public IP address is. Or they can use a dynamic DNS service, so you can look it up based on a hostname.


P.S. Another alternative for you here is to re-write your chat application as a web application which can be hosted on a single, central public server and be accessed by anyone from anywhere, anytime. (You can of course use authentication to then restrict usage to those you wish to allow onto it.) These kinds of situations is one big reason why web applications have become so popular in the last 20 years.

A middle-ground approach would be to have a central server running somewhere (again needs to be publicly accessible - either on a cloud server or on a server in your network where you've set port-forwarding on your own router to expose it). In that scenario, all instances of the WinForms application would connect to that server, which would act as a hub for relaying messages. This is a pretty classic client-server architecture. You could even then have other clients (web app, mobile apps etc.) which can connect to the same server to take part in chat.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...