Link to home
Start Free TrialLog in
Avatar of jay_eire
jay_eireFlag for United States of America

asked on

monitor my connections

Is this possible? if so, how can I do it,
Thanks
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("12.8.19.44");

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

            public void DisConnectSocket()
            {
                  MySocket.Close();
            }
      
            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.
                  try
                  {
                        bytes = MySocket.Receive(bytesReceived);
                        page = Encoding.Unicode.GetString(bytesReceived, 0, bytes);
            
                        return page;
                  }
                  catch(Exception)
                  {
                        int iErr = 0;
                        iErr = 1;
                  }
                  
                  return "OK";
            }
      }
}
ASKER CERTIFIED SOLUTION
Avatar of NipNFriar_Tuck
NipNFriar_Tuck

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jay_eire

ASKER

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 it.........how can I do this?
what i need to do is monitor my connection, if it closes i need to reopen the connection immediately
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial