简体   繁体   English

C#,socket通讯(外接)

[英]C#, socket communication (external connection)

I have a question about C# communication.我有一个关于 C# 通信的问题。

Currently using.Net and studying Socket.目前在使用.Net,正在学习Socket。 You can create a socket server using the internal IP and connect as an internal client.您可以使用内部 IP 创建套接字服务器并作为内部客户端连接。 Is it possible to open a server in another network like a chat program and connect to Socket Ip:Port from another network?是否可以像聊天程序一样在另一个网络中打开服务器并从另一个网络连接到 Socket Ip:Port?

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)?我需要配置什么来使用上面的 IP 和端口号打开服务器并从另一个网络(客户端)连接到服务器吗?

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 .在路由器中打开一个端口(也就是启用端口转发)并将60800转发到192.168.0.20:8000
  2. Determine your external ip.确定您的外部 ip。 Google for "what is my ip" or go to my site .谷歌搜索“我的 ip 是什么”或 go 到我的网站
  3. Connect the via your-external-ip:60800 .通过your-external-ip:60800连接。

Hint: 60800 can be any other random port-value.提示: 60800可以是任何其他随机端口值。 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.我建议您开始学习 ip-communication 是如何工作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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