monitor my connections

Posted on 2005-05-12
Last Modified: 2010-04-16
Is this possible? if so, how can I do it,
Jay Eire

I need something to monitor my connections, if the connection dies or collapses I need it to automatically open back up again. Also is the any type of Connection Manager Class or Code out there I could use......... here's what I have..........

using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using Microsoft.VisualBasic;

namespace TCPConnection
      /// <summary>
      /// Summary description for MYTCPConnection.
      /// </summary>
      public class MYTCPConnection
            private Socket MySocket;
            public MYTCPConnection()
                  // TODO: Add constructor logic here

            public void Connectsocket()
                  IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
                  //IPAddress ipAddress = ipHostInfo.AddressList[0];
                  IPAddress ipAddress = IPAddress.Parse("");

                  IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 1002);
                  Socket tempsocket = new Socket(AddressFamily.InterNetwork,
                        SocketType.Stream, ProtocolType.Tcp );
                  if (tempsocket.Connected)
                        MySocket = null;
                        MySocket = tempsocket ;

            public void DisConnectSocket()
            public string SocketSendReceive(string ToSend)
                  //Set up variables and String to write to the server.
                  byte[] bytesReceived = new Byte[2500];
                  byte[] bytesSent = Encoding.Unicode.GetBytes(ToSend);

                  // Send request to the server.
                  int i = MySocket.Send(bytesSent);
                  // Receive the server  home page content.
                  int bytes ;

                  // Read the first 256 bytes.
                  string page = "";

                  //The following will block until the page is transmitted.
                        bytes = MySocket.Receive(bytesReceived);
                        page = Encoding.Unicode.GetString(bytesReceived, 0, bytes);
                        return page;
                        int iErr = 0;
                        iErr = 1;
                  return "OK";
Question by:jay_eire
    LVL 7

    Accepted Solution

    Why not do something like

    if ( !MySocket.Connected ) ConnectSocket;

    before trying to send the data?

    Also if you inherited from the socket class to make your own socket class you could put all this code in that class and you could make sure that the socket is cleaned up with the Dispose() method instead of setting to null.

    LVL 7

    Author Comment

    can i send a ping back and forth to keep my connection open? basically I don't want the connect to close I need to keep it open, that's what I want to monitor can I do this?
    LVL 7

    Author Comment

    what i need to do is monitor my connection, if it closes i need to reopen the connection immediately
    LVL 37

    Assisted Solution

    TCPKeepAlives will do this for you ....



    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now