Async vs Sync
-
Well I am working on my first Clients / Server application. First I started off working with syncronous sockets, and then switched to asyncronous sockets. The way it looks like it is going to work for now (until I get a better understanding) is the Client connects to server, Client sends data to server, Server gets data, Server looks for data pertaining to that specific client, Server returns data to client, connections close and end. I see a problem though? What if the client connects to the server, sends data to server, server gets it, then server loses connection to the internet? This would mean that my client is stuck on the BeginReceive part. I can't find a timeout. Now I haven't tried to test this yet.. but I was wondering how to handle this situation? Would syncronous sockets be better than asyncronous?
-
Well I am working on my first Clients / Server application. First I started off working with syncronous sockets, and then switched to asyncronous sockets. The way it looks like it is going to work for now (until I get a better understanding) is the Client connects to server, Client sends data to server, Server gets data, Server looks for data pertaining to that specific client, Server returns data to client, connections close and end. I see a problem though? What if the client connects to the server, sends data to server, server gets it, then server loses connection to the internet? This would mean that my client is stuck on the BeginReceive part. I can't find a timeout. Now I haven't tried to test this yet.. but I was wondering how to handle this situation? Would syncronous sockets be better than asyncronous?
Hi Jacob, this is how I see it: 1. there are no synchronous or asynchronous sockets, all sockets are the same; however you can operate them in sync or async way; you can choose your way separately for clients and servers. 2. you can always mimic an async operation by launching a separate thread that works synchronously. The disadvantage is cost (one more thread, with its state and stack), the advantage is comfort, as you have less of a problem remembering your state. 3. The .NET Socket class supports ReceiveTimeout in sync mode only; when using the async methods, if you want some kind of timeout, you have to implement it yourself. And even then, it will not pre-empt an outstanding async Receive, all it will do is tell your app sooner the data isn't coming (in time). 4. Assuming your client is using only one or a few sockets at any point in time, I don't see much objections to using the thread and sync mode there. On the server side, the potential number of clients may force you to work in async mode. Hope this helps.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
Hi Jacob, this is how I see it: 1. there are no synchronous or asynchronous sockets, all sockets are the same; however you can operate them in sync or async way; you can choose your way separately for clients and servers. 2. you can always mimic an async operation by launching a separate thread that works synchronously. The disadvantage is cost (one more thread, with its state and stack), the advantage is comfort, as you have less of a problem remembering your state. 3. The .NET Socket class supports ReceiveTimeout in sync mode only; when using the async methods, if you want some kind of timeout, you have to implement it yourself. And even then, it will not pre-empt an outstanding async Receive, all it will do is tell your app sooner the data isn't coming (in time). 4. Assuming your client is using only one or a few sockets at any point in time, I don't see much objections to using the thread and sync mode there. On the server side, the potential number of clients may force you to work in async mode. Hope this helps.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
Ok thanks! My server could possibly be accepting 50-100 connections. If you have agents out there checking in every 2-5 minutes. So I should implement the asyncronous method on the server end, and use syncronous on the client? That way I can specify a timeout and won't get in a situation like I mentioned above. Do you see any real objections to doing something like that?
-
Ok thanks! My server could possibly be accepting 50-100 connections. If you have agents out there checking in every 2-5 minutes. So I should implement the asyncronous method on the server end, and use syncronous on the client? That way I can specify a timeout and won't get in a situation like I mentioned above. Do you see any real objections to doing something like that?
Yes, that is what I would do in a first iteration, as it keeps the clients simple, and optimizes the server. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
Yes, that is what I would do in a first iteration, as it keeps the clients simple, and optimizes the server. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
Awesome! Thanks for the replies. I will use async on server side and sync on client side.
-
Awesome! Thanks for the replies. I will use async on server side and sync on client side.
You're welcome. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.