| Start |
|---|
| Asynchronous Socket (Client / Server) example. Server Side example Create Listener for server Start of with creating an server that will handle clients that connect, and requests that will be send. So create an Listener Class that will handle this. First we need to initialize the Listener socket where we can listen on for any connections. We are going to use an Tcp Socket that is why we use SocketType.Stream. Also we specify to witch port the server should listen to Then we start listening for any incoming connections. The tree methods we use here are: So when a client connects we can accept them by this method: Three methods wee use here are: Now we have an Listening Socket but how do we receive data send by the client that is what the next code is showing. Create Server Receiver for each client First of create a receive class with a constructor that takes in a Socket as parameter: In the next method we first start off with giving the buffer a size of 4 bytes (Int32) or package contains to parts {lenght, actual data}. So the first 4 bytes we reserve for the lenght of the data the rest for the actual data. Next we use BeginReceive() (opens new window) method. This method is used to start receiving from connected clients and when it will receive data it will run the So we've setup a server that can receive and listen for incoming connections. When a clients connect it will be added to a list of clients and every client has his own receive class. To make the server listen: Some Classes I use in this example Client Side example Connecting to server First of all we want to create a class what connects to the server te name we give it is: Connector: Next Method for this class is TryToConnect() This method goth a few interestin things: Sending a message to the server So now we have an almost finish or Socket application. The only thing that we don't have jet is a Class for sending a message to the server. Finaly crate two buttons one for connect and the other for sending a message: The client class I used in this example Notice The Receive Class from the server is the same as the receive class from the client. Conclusion You now have a server and a client. You can work this basic example out. For example make it that the server also can receive files or other tings. Or send a message to the client. In the server you got a list of client so when you receive something you will know from with client it came from. Final result: RemarksSocket 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 = Server is listening to port 1234. Forward incoming connections on 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 (opens new window). So now you create a request on this endpoint : If you want to connect to a local IP you won't have to portforwart just change the loopback address to 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. |
Read Article
https://develop.horje.com/learn/1434/reference