Stats

75 Contributors: 1
2017-04-18
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook

Asynchronous Socket

Download c# eBook

Introduction

By using asynchronous sockets a server can listening for incoming connections and do some other logic in the mean time in contrast to synchronous socket when they are listening they block the main thread and the application is becoming unresponsive an will freeze until a client connects.

Remarks

Socket and network

How to access a Server outside my own network? This is a common question and when it is asked is mostly flagged as of 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));

So now you create a request on this endpoint : 10.10.10.10:1234 if you did property port forward your router your server and client will connect without any problem.

If you want to connect to a local IP you won't have to portforwart just change the loopback address to 192.168.1.178 or something like that.

Sending data:

Data is send in byte array. You need to pack you data into an byte array and unpack it on the other side.

If you are familiar with socket you also can try to encrypt your byte array before sending. This will prevent anyone from stealing your package.

Related Examples