Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Building an Asyc Socket DLL using C#, I followed MS .NET FRAMEWORK

library sample (see "Asynchronous Client Socket Example") and created

a Windows' Form EXE.

 

This .exe has a form which has a button and a text field to invoke the

async communication and display return data.

 

<F5>, runs ok and data returned is being displayed. Without exiting

the app, click on the button again, this supposed do the exact

connect,

send, receive, close and displaying data. But, the following comes up

in a dialog box,

 

----------------------------------------------------------------------------

An unhandled exception of type 'System.Net.Sockets.SocketException'

occurred in netsock.dll

 

Additional information: A request to send or receive data was

disallowed because the socket is not connected and (when sending on a

datagram socket using a sendto call) no address was supplied

----------------------------------------------------------------------------

 

Ok, I set a breakpoint at the line the DLL is called, stepping through

all of the lines, it ran perfectly fine, no error, returns data as

expected.

 

It's repeated many times with machine rebooting. I opened up a DOS

window, use

 

netstat -p TCP

 

to check socket connection statistics, whenever I got the above error,

netstat showed the State as ESTABLISHED.

 

It's always running fine for the first time but not afterward.

However, breakpoint debug mode runs ok.

 

In another machine it runs just fine, and in another one it throws exception:

"A blocking operation was interrupted by a call to WSACancelBlockingCall" in the line

sock.EndConnect(ar);

 

The code:

 

 

private void connectstart()

{

try

{

sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

this.sock.Blocking = true;

AsyncCallback onconnect = new AsyncCallback(afterconnection);

Console.WriteLine("Beginning connection.");

this.sock.BeginConnect(this.remoteendpoint, onconnect,null);

}

catch(SocketException e)

{

Console.WriteLine("Cannot connect: " + e.Message);

}

}

 

public void afterconnection(IAsyncResult ar)

{

try

{

this.sock.EndConnect(ar);

sendMessage();

}

catch(Exception e)

{

Console.WriteLine("Exception in afterconnect"+e.Message);

}

}

public void sendMessage()

{

NetworkStream ns = new NetworkStream(this.sock);

if(ns != null && this.sock != null)

{

Thread.Sleep(500);

string teststring = "TEST";

byte[] testbytes = System.Text.Encoding.ASCII.GetBytes(teststring);

if (this.sock.Poll(5000, SelectMode.SelectWrite))

{

ns.Write(testbytes, 0, testbytes.Length);

Console.WriteLine("Sending test message.");

}

}

else

{

Console.WriteLine("Could not connect to remote user.");

}

}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...