PC Review


Reply
Thread Tools Rate Thread

asynchronous socket communication

 
 
panko
Guest
Posts: n/a
 
      25th Jan 2007
Hello,
I can't manage with asynchronous socket communication.
I wrote a class CSocket.cs.
This class is taking care of sending strings to LED display. This
display is actually communicating via serial port and serial/ethernet
converter (MOXA NE-4100T) with TCP server. So communication is in that
way:

MyApplication(TCP
client)-----------------(TCPServer)MOXA(serial)---(serial)LED Display

In the constructor there are three parameters:
public CSocket(IPAddress p_ipAddress, int p_port, ArrayList
p_arrayToSend)

The array is array of strings that are valid for special LED display
protocol. The server is giving reply OK or BAD if the string it
received is display

protocol valid (syntax, crc etc).
The main public method of that class is
public void ConnectAndSend()
{
if (this.m_status != EStatus.Connected)
{
res = this.SequenceConnect();
}

this.m_iTriedSend = 0;
if (this.m_status = EStatus.Connected)
{
while(counter<p_arrayToSend.Length || this.m_iTriedSend <
REPEATSEND)
{
Send(CurrStrFromArray);
this.m_iTriedSend++;
if (this.m_status==EStatus.Received) counter++;
}
}
}
This is only abstract of that method to give general idea.
The SequenceConnect tries to connect using Socket.BeginConnect methd.
When connection is established OnClientConnect method is called to call
Socket.EndConnect method.
This process I wanted to have in the ConnThread . When the thread is
finished (connection established or not) the main thread can go further
to send data (or not)

The same idea idea is with sending the strings to TCP server. When the
data is sent ReceiveThread is launched to wait for the reply and when
there is response from the server, the main thread will keep on sending
following strings.

Here is most important part of the code. I hope names are clear enough

private void SequenceConnect()
{
this.m_iTriedConnect = 0;
while (this.m_iTriedConnect < REPEATCONNECT)
{
ThreadStart privThreadDeleg = new ThreadStart(TryConnect);
Thread ConnThread = new Thread(privThreadDeleg);
ConnThread.Start();
// wait until thread finish or time elapses
ConnThread.Join(TIME_ELAPSED);
if (this.m_status == EStatus.Connected) break;
}
}

public void TryConnect()
{
this.m_iTriedConnect++;
try
{
IPEndPoint remoteEndPoint = new
System.Net.IPEndPoint(this.m_ipAddress, this.m_iPort);
this.m_socketClient.BeginConnect(remoteEndPoint,new AsyncCallback (
OnClientConnect ),null);
this.m_status = EStatus.Connecting;
}
catch (SocketException e)
{
else this.m_status = EStatus.ConnError;
}
return;
}

private void OnClientConnect(IAsyncResult asyn)
{
try
{
this.m_socketClient.EndConnect(asyn);
if(this.m_socketClient.Connected) this.m_status = EStatus.Connected;
else this.m_status = EStatus.ConnError;
}
catch (SocketException e)
{
else this.m_status = EStatus.ConnError;
}
return;
}

private void Send(string p_stringToSend)
{
this.m_status=EStatus.Sending;
try
{
Object objData = p_stringToSend;
byte[] byData = Encoding.ASCII.GetBytes(objData.ToString ());
this.m_socketClient.Send (byData);
this.m_status = EStatus.Sent;

ThreadStart privThreadDeleg = new ThreadStart(WaitForData);
Thread ReceiveThread = new Thread(privThreadDeleg);
ReceiveThread.Start();
// wait until thread finish or time elapses
ReceiveThread.Join(TIME_ELAPSED);
}
catch (SocketException e)
{
this.m_status = EStatus.SendError;
}
return;
}

private void WaitForData()
{
this.m_status = EStatus.Receiving;
try
{
IAsyncResult m_asynResult =
m_socketClient.BeginReceive(m_bDataBuffer,0,8,SocketFlags.None,pfnCallBack,null);
}
catch (SocketException e)
{
this.m_status = EStatus.ReceiveError;
}
return;
}

private void OnDataReceived(IAsyncResult asyn)
{
try
{
int iRx = 0;
iRx = m_socketClient.EndReceive(asyn);
char[] chars = new char[iRx + 1];
Decoder d = Encoding.UTF8.GetDecoder();
this.m_iCharLen = d.GetChars(m_bDataBuffer, 0, iRx, chars, 0);
this.m_Received = new String(chars);
this.m_status = EStatus.Received;
}
catch (SocketException e)
{
this.m_status = EStatus.ReceiveError;
}
return;
}


The problem I found here is that application doesn't stop until
ConnThread is finished. Main thread passes
"ConnThread.Join(TIME_ELAPSED);" and goes to next statement which is
"if (this.m_status == EStatus.Connected) break;" while the value is
Connecting - so next loop is done. How can I prevent it?
Sometimes main thread is waiting for "ConnThread.Join(TIME_ELAPSED);"
statement.
I don't understand why. Does AsyncCallback start a new thread?
I think I have some problem with threading here.
Or maybe you know some better way/idea to solve that kind of
communication.

Regards,
panko

 
Reply With Quote
 
 
 
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      25th Jan 2007
Hi,

Well that's a fair amount of code let me tell you. Did you try a sync
connection first?



"panko" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
| Hello,
| I can't manage with asynchronous socket communication.
| I wrote a class CSocket.cs.
| This class is taking care of sending strings to LED display. This
| display is actually communicating via serial port and serial/ethernet
| converter (MOXA NE-4100T) with TCP server. So communication is in that
| way:
|
| MyApplication(TCP
| client)-----------------(TCPServer)MOXA(serial)---(serial)LED Display
|
| In the constructor there are three parameters:
| public CSocket(IPAddress p_ipAddress, int p_port, ArrayList
| p_arrayToSend)
|
| The array is array of strings that are valid for special LED display
| protocol. The server is giving reply OK or BAD if the string it
| received is display
|
| protocol valid (syntax, crc etc).
| The main public method of that class is
| public void ConnectAndSend()
| {
| if (this.m_status != EStatus.Connected)
| {
| res = this.SequenceConnect();
| }
|
| this.m_iTriedSend = 0;
| if (this.m_status = EStatus.Connected)
| {
| while(counter<p_arrayToSend.Length || this.m_iTriedSend <
| REPEATSEND)
| {
| Send(CurrStrFromArray);
| this.m_iTriedSend++;
| if (this.m_status==EStatus.Received) counter++;
| }
| }
| }
| This is only abstract of that method to give general idea.
| The SequenceConnect tries to connect using Socket.BeginConnect methd.
| When connection is established OnClientConnect method is called to call
| Socket.EndConnect method.
| This process I wanted to have in the ConnThread . When the thread is
| finished (connection established or not) the main thread can go further
| to send data (or not)
|
| The same idea idea is with sending the strings to TCP server. When the
| data is sent ReceiveThread is launched to wait for the reply and when
| there is response from the server, the main thread will keep on sending
| following strings.
|
| Here is most important part of the code. I hope names are clear enough
|
| private void SequenceConnect()
| {
| this.m_iTriedConnect = 0;
| while (this.m_iTriedConnect < REPEATCONNECT)
| {
| ThreadStart privThreadDeleg = new ThreadStart(TryConnect);
| Thread ConnThread = new Thread(privThreadDeleg);
| ConnThread.Start();
| // wait until thread finish or time elapses
| ConnThread.Join(TIME_ELAPSED);
| if (this.m_status == EStatus.Connected) break;
| }
| }
|
| public void TryConnect()
| {
| this.m_iTriedConnect++;
| try
| {
| IPEndPoint remoteEndPoint = new
| System.Net.IPEndPoint(this.m_ipAddress, this.m_iPort);
| this.m_socketClient.BeginConnect(remoteEndPoint,new AsyncCallback (
| OnClientConnect ),null);
| this.m_status = EStatus.Connecting;
| }
| catch (SocketException e)
| {
| else this.m_status = EStatus.ConnError;
| }
| return;
| }
|
| private void OnClientConnect(IAsyncResult asyn)
| {
| try
| {
| this.m_socketClient.EndConnect(asyn);
| if(this.m_socketClient.Connected) this.m_status = EStatus.Connected;
| else this.m_status = EStatus.ConnError;
| }
| catch (SocketException e)
| {
| else this.m_status = EStatus.ConnError;
| }
| return;
| }
|
| private void Send(string p_stringToSend)
| {
| this.m_status=EStatus.Sending;
| try
| {
| Object objData = p_stringToSend;
| byte[] byData = Encoding.ASCII.GetBytes(objData.ToString ());
| this.m_socketClient.Send (byData);
| this.m_status = EStatus.Sent;
|
| ThreadStart privThreadDeleg = new ThreadStart(WaitForData);
| Thread ReceiveThread = new Thread(privThreadDeleg);
| ReceiveThread.Start();
| // wait until thread finish or time elapses
| ReceiveThread.Join(TIME_ELAPSED);
| }
| catch (SocketException e)
| {
| this.m_status = EStatus.SendError;
| }
| return;
| }
|
| private void WaitForData()
| {
| this.m_status = EStatus.Receiving;
| try
| {
| IAsyncResult m_asynResult =
|
m_socketClient.BeginReceive(m_bDataBuffer,0,8,SocketFlags.None,pfnCallBack,null);
| }
| catch (SocketException e)
| {
| this.m_status = EStatus.ReceiveError;
| }
| return;
| }
|
| private void OnDataReceived(IAsyncResult asyn)
| {
| try
| {
| int iRx = 0;
| iRx = m_socketClient.EndReceive(asyn);
| char[] chars = new char[iRx + 1];
| Decoder d = Encoding.UTF8.GetDecoder();
| this.m_iCharLen = d.GetChars(m_bDataBuffer, 0, iRx, chars, 0);
| this.m_Received = new String(chars);
| this.m_status = EStatus.Received;
| }
| catch (SocketException e)
| {
| this.m_status = EStatus.ReceiveError;
| }
| return;
| }
|
|
| The problem I found here is that application doesn't stop until
| ConnThread is finished. Main thread passes
| "ConnThread.Join(TIME_ELAPSED);" and goes to next statement which is
| "if (this.m_status == EStatus.Connected) break;" while the value is
| Connecting - so next loop is done. How can I prevent it?
| Sometimes main thread is waiting for "ConnThread.Join(TIME_ELAPSED);"
| statement.
| I don't understand why. Does AsyncCallback start a new thread?
| I think I have some problem with threading here.
| Or maybe you know some better way/idea to solve that kind of
| communication.
|
| Regards,
| panko
|


 
Reply With Quote
 
panko
Guest
Posts: n/a
 
      25th Jan 2007
Hi,
I've only read about it and some sample code and tutorials.
I don't like the idea of checking periodically if i received any answer
from the server.
I have event driven environment and I would like to take advantage of
it.


On Jan 25, 8:07 pm, "Ignacio Machin \( .NET/ C# MVP \)" <machin TA
laceupsolutions.com> wrote:
> Hi,
>
> Well that's a fair amount of code let me tell you. Did you try a sync
> connection first?


 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      25th Jan 2007
Hi,

"panko" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
| Hi,
| I've only read about it and some sample code and tutorials.

It's simple once you know the concept, try it first with a simple
send/receive.

| I don't like the idea of checking periodically if i received any answer
| from the server.

Now that you mention that, are you sure that the connection is always open?
is it possible that it gets close after some time with out activiti?

| I have event driven environment and I would like to take advantage of
| it.

Of course, but even with a sync. you can do that, you can use one thread
just for the comm. and this thread send events to the UI thread when
something is changed.

In any case the sync scenario would be to test that the comm is correct, and
that the protocol is being interpreted correctly.


--
Ignacio Machin
machin AT laceupsolutions com


 
Reply With Quote
 
panko
Guest
Posts: n/a
 
      25th Jan 2007

I understand how it works, but I would like to be notified when data
arrives.
Sync communication doesn't allow me to do that. That's why I don't like
sync.

On Jan 25, 9:10 pm, "Ignacio Machin \( .NET/ C# MVP \)" <machin TA
laceupsolutions.com> wrote:
> Hi,
>
> "panko" <PKoper...@gmail.com> wrote in messagenews:(E-Mail Removed)...
> | Hi,
> | I've only read about it and some sample code and tutorials.
>
> It's simple once you know the concept, try it first with a simple
> send/receive.
>


 
Reply With Quote
 
Barry Kelly
Guest
Posts: n/a
 
      25th Jan 2007
panko wrote:

> I understand how it works, but I would like to be notified when data
> arrives.
> Sync communication doesn't allow me to do that. That's why I don't like
> sync.


A synchronous blocking TCP/IP connection only returns to the calling
program when data arrives, or the connection is closed. It is not
polling. If you want to have a responsive UI etc. with a synchronous,
blocking TCP/IP connection, then you need to use a separate thread.

However, a blocking approach in a separate thread is simpler than
asynchronous code to start with. The asynchronous approach uses threads
in the background anyway, it will call you back on a threadpool / IO
thread, so you still need to deal with synchronization issues.

-- Barry

--
http://barrkel.blogspot.com/
 
Reply With Quote
 
msgroup
Guest
Posts: n/a
 
      25th Jan 2007
Hi, panko:

Do you really care for notification. If you do, take a time to look at
our SocketPro at www.udaparts.com.

See comments from our customers at
http://www.udaparts.com/groups/viewtopic.php?t=39 and real samples written
from our SocketPro at http://www.wramp.net/casestudies1.html.

SocketPro has a built-in chat (or notification) service for real-time
notification from either client or server side. See the site at
http://www.udaparts.com/document/art...hatservice.htm

If you use worker thread approach, you will NEVER get real-time
notification.

Regards,

"panko" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello,
> I can't manage with asynchronous socket communication.
> I wrote a class CSocket.cs.
> This class is taking care of sending strings to LED display. This
> display is actually communicating via serial port and serial/ethernet
> converter (MOXA NE-4100T) with TCP server. So communication is in that
> way:
>
> MyApplication(TCP
> client)-----------------(TCPServer)MOXA(serial)---(serial)LED Display
>
> In the constructor there are three parameters:
> public CSocket(IPAddress p_ipAddress, int p_port, ArrayList
> p_arrayToSend)
>
> The array is array of strings that are valid for special LED display
> protocol. The server is giving reply OK or BAD if the string it
> received is display
>
> protocol valid (syntax, crc etc).
> The main public method of that class is
> public void ConnectAndSend()
> {
> if (this.m_status != EStatus.Connected)
> {
> res = this.SequenceConnect();
> }
>
> this.m_iTriedSend = 0;
> if (this.m_status = EStatus.Connected)
> {
> while(counter<p_arrayToSend.Length || this.m_iTriedSend <
> REPEATSEND)
> {
> Send(CurrStrFromArray);
> this.m_iTriedSend++;
> if (this.m_status==EStatus.Received) counter++;
> }
> }
> }
> This is only abstract of that method to give general idea.
> The SequenceConnect tries to connect using Socket.BeginConnect methd.
> When connection is established OnClientConnect method is called to call
> Socket.EndConnect method.
> This process I wanted to have in the ConnThread . When the thread is
> finished (connection established or not) the main thread can go further
> to send data (or not)
>
> The same idea idea is with sending the strings to TCP server. When the
> data is sent ReceiveThread is launched to wait for the reply and when
> there is response from the server, the main thread will keep on sending
> following strings.
>
> Here is most important part of the code. I hope names are clear enough
>
> private void SequenceConnect()
> {
> this.m_iTriedConnect = 0;
> while (this.m_iTriedConnect < REPEATCONNECT)
> {
> ThreadStart privThreadDeleg = new ThreadStart(TryConnect);
> Thread ConnThread = new Thread(privThreadDeleg);
> ConnThread.Start();
> // wait until thread finish or time elapses
> ConnThread.Join(TIME_ELAPSED);
> if (this.m_status == EStatus.Connected) break;
> }
> }
>
> public void TryConnect()
> {
> this.m_iTriedConnect++;
> try
> {
> IPEndPoint remoteEndPoint = new
> System.Net.IPEndPoint(this.m_ipAddress, this.m_iPort);
> this.m_socketClient.BeginConnect(remoteEndPoint,new AsyncCallback (
> OnClientConnect ),null);
> this.m_status = EStatus.Connecting;
> }
> catch (SocketException e)
> {
> else this.m_status = EStatus.ConnError;
> }
> return;
> }
>
> private void OnClientConnect(IAsyncResult asyn)
> {
> try
> {
> this.m_socketClient.EndConnect(asyn);
> if(this.m_socketClient.Connected) this.m_status = EStatus.Connected;
> else this.m_status = EStatus.ConnError;
> }
> catch (SocketException e)
> {
> else this.m_status = EStatus.ConnError;
> }
> return;
> }
>
> private void Send(string p_stringToSend)
> {
> this.m_status=EStatus.Sending;
> try
> {
> Object objData = p_stringToSend;
> byte[] byData = Encoding.ASCII.GetBytes(objData.ToString ());
> this.m_socketClient.Send (byData);
> this.m_status = EStatus.Sent;
>
> ThreadStart privThreadDeleg = new ThreadStart(WaitForData);
> Thread ReceiveThread = new Thread(privThreadDeleg);
> ReceiveThread.Start();
> // wait until thread finish or time elapses
> ReceiveThread.Join(TIME_ELAPSED);
> }
> catch (SocketException e)
> {
> this.m_status = EStatus.SendError;
> }
> return;
> }
>
> private void WaitForData()
> {
> this.m_status = EStatus.Receiving;
> try
> {
> IAsyncResult m_asynResult =
> m_socketClient.BeginReceive(m_bDataBuffer,0,8,SocketFlags.None,pfnCallBack,null);
> }
> catch (SocketException e)
> {
> this.m_status = EStatus.ReceiveError;
> }
> return;
> }
>
> private void OnDataReceived(IAsyncResult asyn)
> {
> try
> {
> int iRx = 0;
> iRx = m_socketClient.EndReceive(asyn);
> char[] chars = new char[iRx + 1];
> Decoder d = Encoding.UTF8.GetDecoder();
> this.m_iCharLen = d.GetChars(m_bDataBuffer, 0, iRx, chars, 0);
> this.m_Received = new String(chars);
> this.m_status = EStatus.Received;
> }
> catch (SocketException e)
> {
> this.m_status = EStatus.ReceiveError;
> }
> return;
> }
>
>
> The problem I found here is that application doesn't stop until
> ConnThread is finished. Main thread passes
> "ConnThread.Join(TIME_ELAPSED);" and goes to next statement which is
> "if (this.m_status == EStatus.Connected) break;" while the value is
> Connecting - so next loop is done. How can I prevent it?
> Sometimes main thread is waiting for "ConnThread.Join(TIME_ELAPSED);"
> statement.
> I don't understand why. Does AsyncCallback start a new thread?
> I think I have some problem with threading here.
> Or maybe you know some better way/idea to solve that kind of
> communication.
>
> Regards,
> panko
>



 
Reply With Quote
 
panko
Guest
Posts: n/a
 
      26th Jan 2007
First of all - thanks for your useful reply.
According to what you recommend I will start with synchronous blocking
communication and threading.
I already use threading here in async so I hope threading in sync would
be more controllable.
Could you recommend me any sample?

By the way - I think I understand where my mistke was. My ConnThread
finishes with TryConnect method. OnClientConnect called by delegate is
new thread.
Main thread is only paying attention to ConnThread. It doesn't care if
that thread started another one or not.


On 26 Sty, 00:23, Barry Kelly <barry.j.ke...@gmail.com> wrote:
> panko wrote:
> > I understand how it works, but I would like to be notified when data
> > arrives.
> > Sync communication doesn't allow me to do that. That's why I don't like
> > sync.A synchronous blocking TCP/IP connection only returns to the calling

> program when data arrives, or the connection is closed. It is not
> polling. If you want to have a responsive UI etc. with a synchronous,
> blocking TCP/IP connection, then you need to use a separate thread.
>
> However, a blocking approach in a separate thread is simpler than
> asynchronous code to start with. The asynchronous approach uses threads
> in the background anyway, it will call you back on a threadpool / IO
> thread, so you still need to deal with synchronization issues.
>
> -- Barry
>
> --http://barrkel.blogspot.com/


 
Reply With Quote
 
Barry Kelly
Guest
Posts: n/a
 
      26th Jan 2007
panko wrote:

> First of all - thanks for your useful reply.
> According to what you recommend I will start with synchronous blocking
> communication and threading.
> I already use threading here in async so I hope threading in sync would
> be more controllable.
> Could you recommend me any sample?


There is a simple sample in the Quickstart samples in the .NET SDK. This
might not be installed by default in 2.0, I believe it was in 1.1 and
before. It hasn't changed since 1.1 anyway, IIRC.

It's at <sdk-location>/QuickStart/howto/samples/net/tcpudp/cs

In there there are two relevant files, datetimeserver.cs and
datetimeclient.cs, which shows ultra-basic behaviour.

There are lots of other TCP client / server samples out there in C#. I
expect most will use the synchronous approach since it is easier to
reason about, especially when starting. With the asynchronous model, you
often have to create some kind of explicit state machine to model the
progress of a conversation.

-- Barry

--
http://barrkel.blogspot.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Asynchronous Socket ckkwan@my-deja.com Microsoft Dot NET Framework 1 6th Feb 2008 06:12 AM
Forms and asynchronous Serial Communication Vadym Stetsyak Microsoft C# .NET 5 21st Nov 2006 02:09 AM
practical applications for synchronous and asynchronous communication jrefactors@hotmail.com Windows Networking 0 5th May 2005 12:54 AM
Asynchronous vs Synchronous network communication Noel Microsoft Dot NET 1 7th Dec 2004 02:34 PM
Asynchronous Socket Questions Bruce Vander Werf Microsoft Dot NET Framework 0 26th Mar 2004 02:30 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:39 PM.