473,320 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

TCP/IP refresher

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.
Nov 16 '05 #1
2 2006
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.Ma rshal.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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Frank Rizzo" <no**@none.com> wrote in message
news:Or**************@TK2MSFTNGP12.phx.gbl...
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.

Nov 16 '05 #2
Nicholas Paldino [.NET/C# MVP] wrote:
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.Ma rshal.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.

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: capes | last post by:
Hi, I am trying to get back to the workforce after staying home for 3 yrs w my son. I used to code in VC++, design in UML, and test with SILK before I quit. If i go through old books, It feels...
0
by: Dave S | last post by:
Hi, I'm trying to use cachedependency to refresh a variable when a config file in the web application changes, but it doesn't seem to work no matter what I do. The code is in the global.asax...
2
by: salad | last post by:
I read the following paragraph. "The report named Naples, Florida as the most overvalued of all housing markets in the United States. A single-family, median-priced home there sells for...
17
by: hemashiki | last post by:
hiii i have a form within which i have 3 radio button with same name but value will be retrieved from the mysql database.But each radio button will do different actions.can plz suggest me some way.
11
by: gjrw68 | last post by:
I used to code in C about 10 years ago but haven't done anything since than. What's the best refresher site out there for someone who codes daily and needs to get back into C? Cheers, TF
2
by: kattar | last post by:
hi everyone, i m new to dotnet and i m facing problem with frames, i have used 2 frames (frame 1 and frame2), in frame1 there table ,if i select items in table then depending upon item i...
1
by: getmathan | last post by:
I have the below script which ll display a box to accept url and time. I need to add some code to it so that it should open the accepted url in a popup and display it every n seconds given as input....
5
Delerna
by: Delerna | last post by:
Been playing around with DirectX 10 in C++ and I have been using arrays to store info for vertex buffers. To simplify my question I will drop DX10 from the equation. I am an experienced programmer...
4
by: Niall 84 | last post by:
Hi all, Haven't done C or C++ in a few years and have never done a huge amount of it. I'm trying to do some very basic programmes at the moment to get back into it. 1st of all I'm using...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.