473,549 Members | 2,731 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 2472
=?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
1311
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
6203
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
2370
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
6863
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
4659
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
3592
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
8218
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
1300
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
7137
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
7518
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7715
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7956
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
7469
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
7808
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5368
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
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1935
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
1057
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.