Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

monitor my connections

Posted on 2005-05-12
4
Medium Priority
?
195 Views
Last Modified: 2010-04-16
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";
            }
      }
}
0
Comment
Question by:jay_eire
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
NipNFriar_Tuck earned 1000 total points
ID: 13988391
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.

HTH.
0
 
LVL 7

Author Comment

by:jay_eire
ID: 13993790
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?
0
 
LVL 7

Author Comment

by:jay_eire
ID: 13993977
what i need to do is monitor my connection, if it closes i need to reopen the connection immediately
0
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 1000 total points
ID: 14000915
TCPKeepAlives will do this for you ....

clientsocket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.KeepAlive,1000)


Greg
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

580 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