简体   繁体   中英

C#, socket communication (external connection)

I have a question about C# communication.

Currently using.Net and studying Socket. You can create a socket server using the internal IP and connect as an internal client. Is it possible to open a server in another network like a chat program and connect to Socket Ip:Port from another network?

Socket sListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.20"), 8000);
    
sListener.Bind(ipEndPoint);
sListener.Listen(20);
Socket sClient = sListener.Accept();

Is there anything I need to configure to open the server using the above IP and port number and connect to the server from another network (Client)?

Thank you:]

Yes of course. I draw a little image for a normal connection over the internet.

通过互联网正常连接

What you have to do?

  1. Open a port in your router (aka enable port-forward) and forward 60800 to 192.168.0.20:8000 .
  2. Determine your external ip. Google for "what is my ip" or go to my site .
  3. Connect the via your-external-ip:60800 .

Hint: 60800 can be any other random port-value. I suggest high random numbers, so that hacker/scanner can't discover your service so fast.

That is very basic. I recommend, that you start learn how the ip-communication is working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM