TCP/IP refresher

F

Frank Rizzo

I got a server application where many PCs around the world will connect
to via TCP/IP. After they are connected I will need to send data to
these PCs every now and then. I need to hang on to the TCP/IP
connection because most of these PCs are behind NATs, firewalls, etc...
so I won't be able to connect to them from the server.

My question is how do hang on to these connections? How do I find out
when the connection has been dropped, disconnected, etc...
I've checked out the Socket, TcpClient, TcpListener classes but none of
them expose any events. With VB6 it was kind of simple: the Port
control exposed a bunch of events (connected, disconnected, etc...).

How do I do this in .NET? Also, any good tutorials on the web would be
welcomed.

Thanks.
 
N

Nicholas Paldino [.NET/C# MVP]

Frank,

You will have to hold onto the Socket object in order to keep it open
(and not call close). In order to determine if the connection you have is
valid, you will have to test it. Basically, check out the documentation for
the Connected property, and there will be a code snippet there. I've copied
it here for convenience:


client.Connect(anEndPoint);
if (!client.Connected)
{
Console.WriteLine("Winsock error: "
+
Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error())
);
}

// This is how you can determine whether a socket is still connected.
bool blockingState = client.Blocking;
try
{
byte [] tmp = new byte[1];

client.Blocking = false;
client.Send(tmp, 0, 0);
Console.WriteLine("Connected!");
}
catch (SocketException e)
{
// 10035 == WSAEWOULDBLOCK
if (e.NativeErrorCode.Equals(10035))
Console.WriteLine("Connected from an exception!");
else
{
Console.WriteLine("Disconnected: {0}!", e.NativeErrorCode);
}
}
finally
{
client.Blocking = blockingState;
}

Console.WriteLine("Connected: {0}", client.Connected);

Hope this helps.
 
F

Frank Rizzo

Nicholas said:
Frank,

You will have to hold onto the Socket object in order to keep it open
(and not call close). In order to determine if the connection you have is
valid, you will have to test it. Basically, check out the documentation for
the Connected property, and there will be a code snippet there. I've copied
it here for convenience:

Thanks, this was really helpful. How do I listen for a connection and
then pass the connection off to another class? Wouldn't I still need
that class to listen for other incoming connections?
client.Connect(anEndPoint);
if (!client.Connected)
{
Console.WriteLine("Winsock error: "
+
Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error())
);
}

// This is how you can determine whether a socket is still connected.
bool blockingState = client.Blocking;
try
{
byte [] tmp = new byte[1];

client.Blocking = false;
client.Send(tmp, 0, 0);
Console.WriteLine("Connected!");
}
catch (SocketException e)
{
// 10035 == WSAEWOULDBLOCK
if (e.NativeErrorCode.Equals(10035))
Console.WriteLine("Connected from an exception!");
else
{
Console.WriteLine("Disconnected: {0}!", e.NativeErrorCode);
}
}
finally
{
client.Blocking = blockingState;
}

Console.WriteLine("Connected: {0}", client.Connected);

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top