using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace SocketsTCPIP
{
    public class CClientTCPIP
    {
        TcpClient tc ;
        
        string _tcpIpError = string.Empty;

        public string TcpIpError
        {
            get { return _tcpIpError; }
            set { _tcpIpError = value; }
        }
        public CClientTCPIP()
        {
            tc = new TcpClient();
            

        }
        public bool SendTCPIP(string sdata,string  remoteip,int port)
        {
            tc = new TcpClient(remoteip, port);
            
            NetworkStream netStream = tc.GetStream();
            try
            {
                if (netStream.CanWrite)
                {
                    Byte[] sendBytes = Encoding.UTF8.GetBytes(sdata);
                    netStream.Write(sendBytes, 0, sendBytes.Length);

                }
                else
                {
                    tc.Close();

                    // Closing the tcpClient instance does not close the network stream.
                    netStream.Close();
                    return false;
                }
                //tc.Close();
                netStream.Close();

                return true;
            }
            catch (Exception ex)
            {
                tc.Close();
                netStream.Close();
                _tcpIpError=ex.Message;
                return false;
            }
            

        }
        public bool SendTCPIP(byte[] sdata, string remoteip, int port)
        {
            tc = new TcpClient(remoteip, port);
            NetworkStream netStream = tc.GetStream();
            try
            {
                if (netStream.CanWrite)
                {

                    netStream.Write(sdata, 0, sdata.GetLength(0));

                }
                else
                {
                    tc.Close();

                    // Closing the tcpClient instance does not close the network stream.
                    netStream.Close();
                    return false;
                }
                //tc.Close();
                netStream.Close();

                return true;
            }
            catch (Exception ex)
            {
                tc.Close();
                netStream.Close();
                _tcpIpError = ex.Message;
                return false;
            }


        }
        public byte[] ReceiveTCPIP(string remoteip, int port)
        {

            tc = new TcpClient(remoteip, port);
            NetworkStream netStream = tc.GetStream();
            byte[] bytes;
            try
            {

                if (netStream.CanRead)
                {
                    // Reads NetworkStream into a byte buffer.
                    bytes = new byte[tc.ReceiveBufferSize];

                    // Read can return anything from 0 to numBytesToRead. 
                    // This method blocks until at least one byte is read.
                    //netStream.ReadTimeout = 5000;
                    //if (netStream.DataAvailable==true)
                    do
                    {
                        netStream.Read(bytes, 0, (int)tc.ReceiveBufferSize);

                    }
                    while (netStream.DataAvailable);

                }
                else
                {
                    
                    tc.Close();

                    // Closing the tcpClient instance does not close the network stream.
                    netStream.Close();

                    return null;
                }
                tc.Close();
                netStream.Close();
                return bytes;
            }
            catch (Exception ex)
            {
                tc.Close();
                netStream.Close();
                _tcpIpError = ex.Message;
                return null;
            }


        }

    }
}
