Hi Morph King,
it actually depends on the protocol which you will use in your application.
For
Tcp Client Protocol you may write a class as below:
class Connection
{
private TcpClient Connection { get; set; }
private NetworkStream ns { get; set; }
public bool isConnection{ get; set; }
private byte[] BytesToSend, bytesReceived;
public Baglanti(string hostname, string port)
{
baglanti = new TcpClient(hostname, int.Parse(port));
if (baglanti.Connected)
{
ByteReceived= new byte[baglanti.ReceiveBufferSize];
ns = Connection.GetStream();
isConnection = true;
}
}
public bool Baglan(string hostname, string port)
{
if (isConnection)
return isConnection;
try
{
Connection = new TcpClient(hostname, int.Parse(port));
if (Connection.Connected)
{
ByteReceived = new byte[Connection.ReceiveBufferSize];
ns = Connection.GetStream();
isConnection = true;
return true;
}
}
catch (Exception)
{
isConnection = false;
}
return isConnection;
}
public void CutOffConnection()
{
ns.Close();
Connection.Close();
isConnection = false;
}
public bool SendData(string s)
{
if (!Connection.Connected)
{
isConnection = false;
return false;
}
try
{
BytesToSend = System.Text.Encoding.ASCII.GetBytes(s);
ns.Write(BytesToSend, 0, BytesToSend.Length);
ns.Flush();
return true;
}
catch (Exception)
{
baglantivar = false;
return false;
}
}
public bool GetData(ref string ServerResponse)
{
if (!isConnection.Connected)
{
isConnection = false;
return false;
}
try
{
ns.Read(ByteReceived, 0, Conneciton.ReceiveBufferSize);
ServerResponse = System.Text.Encoding.ASCII.GetString(ByteReceived );
return true;
}
catch (Exception)
{
isConnection = false;
return false;
}
}
}
One more question:
Are you going to communicate with an electronic device (such as pic, dsPic) or a computer Host?