Home > Blockchain >  C#, socket communication (external connection)
C#, socket communication (external connection)

Time:01-26

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 :]

CodePudding user response:

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

Normal connection over 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.

  •  Tags:  
  • Related