Having some problem with asynchronous tcp socket programming.

just_starting

New member
Joined
Jul 25, 2005
Messages
1
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.

Its 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.

Its 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.");
}
}
 

Similar threads

P
Replies
0
Views
176
Policy standard local admin account with Active Di
P
Back
Top