Asynchronous Socket
suggest changeIntroduction
With asynchronous sockets, a server can listen for incoming connections and do other work in the meantime. A synchronous socket blocks the calling thread while it listens, so the application is unresponsive until a client connects.
Remarks
Socket and network
How do I reach a server from outside my own network? This is a common question, and it is often closed as off-topic.
Server Side
On the network of your server you need to port forward your router to your server.
For Example PC where server is running on:
local IP = 192.168.1.115
Server is listening to port 1234.
Forward incoming connections on Port 1234 router to 192.168.1.115
Client Side
The only thing you need to change is the IP. You don’t want to connect to your loopback address but to the public IP from the network your server is running on. This IP you can get here.
_connectingSocket.Connect(new IPEndPoint(IPAddress.Parse("10.10.10.10"), 1234));
The client then connects to 10.10.10.10:1234. If the router is port-forwarded correctly, the client and server can connect.
To connect to a machine on the same LAN you do not port-forward; use that machine’s local address, such as 192.168.1.178, instead of the loopback address.
Sending data:
Data is sent as a byte array. Pack the data into a byte array and unpack it on the other side.
If you are familiar with sockets, you can also encrypt that array before sending it. That prevents someone from reading the packet.