using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Web.UI.WebControls;
namespace MyApps
{
/// <summary>
/// Classe per la connessione via socket
/// </summary>
public class clSocket
{
private Socket s;
private string Fdns;
public string dns
{
get {return Fdns;}
}
private int Fport;
public int port
{
get {return Fport;}
}
private string Fuser;
public string user
{
get {return Fuser;}
}
private string Fpassword;
public string password
{
get {return Fpassword;}
}
public clSocket(){}
/// <summary>
/// Costruttore
/// </summary>
/// <param name="dns">Il server a cui collegarsi</param>
/// <param name="port">La porta</param>
/// <param name="user">Nome utente</param>
/// <param name="password">Password</param>
public clSocket(string dns, int port, string user, string password)
{
Fdns = dns;
Fport = port;
Fuser = user;
Fpassword = password;
Connect();
}
/// <summary>
/// Crea la connessione al socket
/// </summary>
/// <returns>True se va a buon fine</returns>
public bool Connect()
{
s = null;
IPEndPoint hostEndPoint;
IPAddress hostAddress = null;
IPHostEntry hostInfo = Dns.Resolve(this.dns);
IPAddress[] IPaddresses = hostInfo.AddressList;
hostAddress = IPaddresses[0];
hostEndPoint = new IPEndPoint(hostAddress, this.port);
try
{
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(hostEndPoint);
return s.Connected;
}
catch (Exception err)
{
s.Shutdown(SocketShutdown.Both);
s.Close();
throw err;
}
}
/// <summary>
/// Esegue il login
/// </summary>
/// <returns>True se va a buon fine</returns>
public bool login()
{
SendCommand("USER " + this.user);
SendCommand("PASS " + this.password);
if (ReceiveCommand("login").IndexOf("200 login OK, proceed\r\n\0")>-1)
{
clearBuffer();
return true;
}
else
return false;
}
/// <summary>
/// Esegue la disconnessione
/// </summary>
public void Disconnect()
{
s.Shutdown(SocketShutdown.Both);
s.Close();
s = null;
}
/// <summary>
/// Invia un comando via socket
/// </summary>
/// <param name="command">Il nome del comando inviato</param>
public void SendCommand(string command)
{
byte[] msg;
msg = Encoding.ASCII.GetBytes(command + "\r\n");
s.Send(msg, 0, msg.Length, SocketFlags.None);
}
/// <summary>
/// Legge la risposta (dopo l'invio di un comando
/// </summary>
/// <param name="sTerminate">Il carattere di fine che cia aspettiamo</param>
/// <returns>La risposta</returns>
public string ReceiveCommand(string sTerminate)
{
string str = "";
int iRx = 0;
byte[] buffer;
while (true)
{
if (s.Available > 0)
{
buffer = new byte[1024];
iRx = s.Receive(buffer);
str += Encoding.ASCII.GetString(buffer);
if (
str.IndexOf(sTerminate) > -1 || str.IndexOf("512") > -1 )
return str;
}
}
}
/// <summary>
/// Svuota il buffer di ricazione
/// </summary>
public void clearBuffer()
{
int iRx;
byte[] buffer;
while (s.Poll(100000, SelectMode.SelectRead))
{
buffer = new byte[1024];
iRx = s.Receive(buffer);
}
}
}
}
Un esempio di utilizzo
//istanzio la classe
clSocket cs = new clSocket("web.myDomain.com", port, user, password);
try
{
//eseguo il login
cs.login();
//invio un comando
cs.SendCommand("NOMECOMANDO");
//leggo la risposta
cs.ReceiveCommand("ENDCHAR")
}
catch
{
//gestione eccezioni
}
finally
{
cs.Disconnect();
}