473,586 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ 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(11 0, "some.server.co m")
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 ManualResetEven ts is the way except I can't make it work without blocking, Unless I do the following

SocketClient client = new SocketClient(11 0, "some.server.co m")
Thread connectThread = new Thread(new ThreadStart(cli ent.Connect))
connectThread.S tart()

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 2479
=?Utf-8?B?TWF0dGhldyB LaW5n?= <ma**@intervisi onsoftware.com> wrote in
news:10******** *************** ***********@mic rosoft.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(11 0, "some.server.co m");
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/
"Programmin g 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(cli ent.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?TWF0dGhldyB LaW5n?= <an*******@disc ussions.microso ft.com> wrote
in news:00******** *************** ***********@mic rosoft.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(cli ent.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/
"Programmin g 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
1315
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 having a problem at the moment when I shut the service down. When I stop the service I attempt to shutdown and close the socket. But when the service...
1
6209
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 BufferedStream.BeginRead() not behave similar to the NetworkStream.BeginRead() ?? The following short program illustrates my problem.: using System;
7
2372
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 what they were doing could take a look at my code and tell me where and why I'm going wrong. Any suggestions of groups or forums?
2
6864
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 there is a problem with the connection. I need to know which client has sent the incoming data as each client has its own buffer on my "server"...
0
4664
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 number of different types of data coming in. I have a databuffer for each type of data coming in.
4
3594
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 client and asynchronous socket server example code provided in the .NET framework developers guide is a great start but I have not dealt with...
5
8227
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 common process of connecting and of the server and client... here is the code i am trying to implement. this code snippet is for message transfer only...
0
1301
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 namespace SocketServer { public class StateObject //class created to clear the duplicate records { // Client socket.
1
7143
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 site, which is actually for sending strings. I tried to adapt this code so that the client sends an image instead of a string. However, there is...
0
8339
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7965
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6617
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3838
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
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 we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1184
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.