We're building a brand new version of the site, and we'd love to hear your ideas
Members
Technology Zones
IBM Learning Center
Articles
Hosted By
Info
|
[3918] Socket Programming in C# - Part 1
Last post 04-05-2008 11:30 PM by maxdev. 17 replies.
-
01-01-1999 12:00 AM
|
|
Advertisement
|
|
-
-
-
-
-
rajesh_vanchi


- Joined on 11-13-2003

- Points 5
|
Sockets Synchronous Vs Async
Regarding blocking socket calls. Are u assuming .net does the polling. As no additional code has to be written for polling. So no need to return to main thread and a delegate also is not required.
Now consider in asynchronous mode, multiple clients are connected. Then the delegates for beginreceive will fire at will and when multiple fire at same time, it gets stacked as there only one thread. Then the last delegate to be fired will be processed and then down the stack. In fact if we need to sequence something, a lot of coding is involved. The flow also is not clear and criss crosses.
Could you comment on the above. I have a req which needs sequential access of the messages and depends on a flow mapping of messages when there are multiple clients. I been thinking async(with delegates) will be an overhead and confusing/complicated to implement.
|
|
-
-
-
-
-
-
-
spowens


- Joined on 01-19-2006

- Points 15
|
Ok, here is the fix to make the samples work under C# 2005 without exceptions.
In the SocketServer project modify the SocketServer class as follows:
1. Add the following delegate declaration:
// This delegate enables asynchronous calls for setting
// the text property on a TextBox control.
delegate void AppendTextCallback(string text);
2. Add the following method:
// If the calling thread is different from the thread that
// created the TextBox control, this method creates a
// AppendTextCallback and calls itself asynchronously using the
// Invoke method.
//
// If the calling thread is the same as the thread that created
// the TextBox control, the Text property is set directly.
private void AppendRxText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.txtDataRx.InvokeRequired)
{
AppendTextCallback d = new AppendTextCallback(AppendRxText);
this.Invoke(d, new object[] { text });
}
else
{
txtDataRx.Text = txtDataRx.Text + text;
}
}
3. Replace the OnDataReceived method with the following, or make the minor one line code change
as commented in the method below if you prefer:
public void OnDataReceived(IAsyncResult asyn)
{
try
{
CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState ;
//end receive...
int iRx = 0 ;
iRx = theSockId.thisSocket.EndReceive (asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
// Old offending line
//txtDataRx.Text = txtDataRx.Text + szData;
AppendRxText(szData);
WaitForData(m_socWorker );
}
catch (ObjectDisposedException )
{
System.Diagnostics.Debugger.Log(0,"1","\nOnDataReceived: Socket has been closed\n");
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
}
|
|
-
-
-
-
|
Search
Code Samples
New Members
|