C# - Catching Socket Desconection

  • Thread starter Thread starter Dairo Cortez
  • Start date Start date
D

Dairo Cortez

Hi,

SOCKET_ONERROR is an event on visual Basic 6.0 that provides information
about when the socket is disconnected, without taking in consideration if
the socket is in use. I need to do the same in C#. I apreciate any
information. Regards


Dairo A Cortes
 
SOCKET_ONERROR is an event on visual Basic 6.0 that provides
information about when the socket is disconnected

Have you tried handling exceptions? Is this what you're looking for? Maybe
if you explain what it is you're trying to do we can help you code it in C#.

-JG
 
Hi, This is my code

class CSocket
{
private TcpClient GO_Canal;
private CRespuestaTIP GU_Rta;
private static NetworkStream myStream;
private static byte[] myBuffer;
public string GC_Cadena;
public CSocket()
{}

//Connect my socket
public int Conectar(string TC_Host,int TN_Puerto)
{
try
{
GO_Canal = new TcpClient(TC_Host,TN_Puerto);
myStream = GO_Canal.GetStream();
myBuffer = new byte[GO_Canal.ReceiveBufferSize];
} catch
{
Console.WriteLine("Error abriendo el puerto");
return -1;
}

return 0;
}

public void Desconectar()
{
GO_Canal.Close();

}

public int Enviar(string TC_Msg)
{
try
{
myStream.Write(Encoding.ASCII.GetBytes(TC_Msg.ToCharArray()), 0,
TC_Msg.Length);
}
catch
{
Console.WriteLine("Error escribiendo");
return -1;
}
return TC_Msg.Length;
}

public string Recibir()
{
string LC_Cadena;
try
{
bool HayDatos=false;
int i=0;
do
{
HayDatos = myStream.DataAvailable;
i++;
}while ((!HayDatos) && (i<3500000));
if (HayDatos)
{
int lData = myStream.Read(myBuffer, 0, GO_Canal.ReceiveBufferSize);

LC_Cadena = Encoding.ASCII.GetString(myBuffer);
LC_Cadena = LC_Cadena.Substring(0,lData);
}
else{
Console.WriteLine("Error en comando");
return "";
}
}
catch
{
Console.WriteLine("Error Leyendo");
return "";
}
return LC_Cadena;
}

If the socket is in use, I can control the event using try and catch,But
in any moment, when the socket isn't in use, the socket is disconected,
i need to catch that event

thanks
 
Back
Top