473,466 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Asynchronous Socket Client class problem

Hi all

I've written a asynchronous socket client class, but i've found that in order to consume it I have to use events, and cannot simply for example

SocketClient client = new SocketClient(110, "some.server.com")
client.Connect()
client.SendData("Hello World")

Instead I have to wait for the async method to raise a Connected event, and call client.SendData from there, for complex chains of operations this because a nightmare chain of events

I tried using a manual reset event as on the microsoft example to halt the return of my Connect method until the asynchronous callback had completed, but this blocks the UI

Microsofts example:
http://msdn.microsoft.com/library/de...ketexample.asp

blocks the UI as far as I can tell when consuming the class. Although it says it doesnt

So my question is, how can I use my asynchronous socket client like above, intead of having event chains? I believe using ManualResetEvents is the way except I can't make it work without blocking, Unless I do the following

SocketClient client = new SocketClient(110, "some.server.com")
Thread connectThread = new Thread(new ThreadStart(client.Connect))
connectThread.Start()

Is that the way that I must handle this? if so how do I dispose of the thread properly when it has completed? This poses the problem of not knowing when the thread has completed its job

Using synchronous sockets isn't really an option because its for a POP3 Client, and there will be alot of receiving large messages etc

Id appreciate any ideas on this,
Thanks
Matt.
Nov 15 '05 #1
3 2459
=?Utf-8?B?TWF0dGhldyBLaW5n?= <ma**@intervisionsoftware.com> wrote in
news:10**********************************@microsof t.com:
I've written a asynchronous socket client class, but i've found that in
order to consume it I have to use events, and cannot simply for example:

SocketClient client = new SocketClient(110, "some.server.com");
client.Connect();
client.SendData("Hello World");

Instead I have to wait for the async method to raise a Connected event,
and call client.SendData from there, for complex chains of operations
this because a nightmare chain of events.
Yes it is! What you describe is blocking vs non blocking. This is an old
article but describes what you are talking about:
http://www.swissdelphicenter.ch/en/showarticle.php?id=4

You should consider switching to blocking sockets, they are much easier to
use and less bug prone.

You can do this in the native .net sockets, but the methods are quite
limited. You should consider Indy, which is free and supports many more
methods and protocols.

http://www.indyproject.org/
Using synchronous sockets isn't really an option because its for a POP3
Client, and there will be alot of receiving large messages etc.


Just put the blocking sockets in a thread. Its much easier to manage than
async sockets.

Trying to do what you want "blocking" with non blocking sockets will only
create a big tar pit.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Nov 15 '05 #2
Thanks for the reply Chad Z

So if I change say my Connect method to be syncrhonous and I start the connect method on a thread start, how do I know when its complete? set an AutoResetEvent? and if Im calling

Thread thread = new Thread(new ThreadStart(client.Connect))
thread.Start()

Can I somehow pass params to the connect function

Is there any way to encapsulate the threads within the class, so the consumer doesn't have to worry about it

Thanks
Matt
Nov 15 '05 #3
=?Utf-8?B?TWF0dGhldyBLaW5n?= <an*******@discussions.microsoft.com> wrote
in news:00**********************************@microsof t.com:
So if I change say my Connect method to be syncrhonous and I start the
connect method on a thread start, how do I know when its complete? set
an AutoResetEvent? and if Im calling:

Thread thread = new Thread(new ThreadStart(client.Connect));
thread.Start();
You are still thinking like async - if you change to blocking you know it
connected when the connect call returns.

Even though that article I posted was old and for Indy the concepts still
apply. Did you take a gander at that?
Is there any way to encapsulate the threads within the class, so the
consumer doesn't have to worry about it?


The thread is a class. I dont have any .net threaded examples yet, but we are
working on porting them. Threading a blocking socket is a lot cleaner, as
well as easier than a non blocking socket.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Darren Thomas | last post by:
Dear all, I have written a TCP server as a windows service that accepts connections using the System.Net.Sockets.Socket class. I use the various asynchronous methods in the socket class. I'm...
1
by: Niels Johansen | last post by:
Hello, When using the asynchronous read method in the BufferedStream class, , it seems to me that it blocks like the normal synchronous read method. Why is it so? Why does the...
7
by: Colin | last post by:
I'm writing a little console socket server but I'm having some difficulty. Can I ask your advice - where is the best place to get some help on that topic? It would be nice if some people who knew...
2
by: Macca | last post by:
My app has an asynchronous socket server. It will have 20 clients connected to the server. Each client sends data every 500 millisecondsThe Connections once established will not be closed unless...
0
by: Macca | last post by:
Hi, I am writing an asychronous socket server to handle 20+ simulataneous connections. I have used the example in MSDN as a base. The code is shown at end of question. Each connection has a...
4
by: Engineerik | last post by:
I am trying to create a socket server which will listen for connections from multiple clients and call subroutines in a Fortran DLL and pass the results back to the client. The asynchronous socket...
5
by: raghubr | last post by:
Hi all, Can any one pls guide me through..I need to transfer the file from server to client and client to server using sockets in an Asynchronous mode so this file transfer doesn't hinder the...
0
by: cmrhema | last post by:
Hi, We have an asynchronous server socket program, which works fine BUT, when due to some circumstances, I receive null data or blank data, the server goes to infinite loop Here is the code ...
1
by: keksy | last post by:
Hi every1, I am writing a small client/server application and in it I want to send an image asynchronous from the client to the server through a TCP socket. I found an example code on the MSDN...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.