none
TCP Connection throws error actively refused connection RRS feed

  • Question

  • public void ICreateServer(string SIpAddress, int SPortNum)
    {
    try
    {
    IPAddress localaddr = IPAddress.Parse(SIpAddress);
    if (this.listener == null)
    {
    this.listener = new TcpListener(localaddr, SPortNum);
    this.listener.Start();
    this.ICreateServerStatus = true;
    }
    if (this.newThread != null)
    return;
    this.newThread = new Thread(new ThreadStart(this.DoWork));
    this.newThread.IsBackground = true;
    this.newThread.Start();
    this.IThreadStarted = true;
    }
    catch (Exception ex)
    {
    ex.ToString();
    this.IErrormsg = ex.Message;
    MessageBox.Show(ex.ToString());
    TraceService(ex.ToString());
    }
    }

    private void DoWork()
    {
    while (this.ThreadTrigger)
    {
    try
    {
    this.IsThreadRunning = true;
    if (this.listener.Pending())
    {
        using (this.client = listener.AcceptTcpClient())
    using (NetworkStream stream = this.client.GetStream())
    {
    if (((Stream)stream).CanRead)
    {
    byte[] numArray = new byte[this.client.ReceiveBufferSize];
    int count = ((Stream)stream).Read(numArray, 0, this.client.ReceiveBufferSize);
    string Query = Encoding.ASCII.GetString(numArray, 0, count);
    if (Query != string.Empty && Query != null)
    {
    if (Query.Contains("AuditLog"))
    {
    // MessageBox.Show(Query);
    this.IReadDataFromDB("AuditTrail.db", Query);
    byte[] bytes = Encoding.ASCII.GetBytes(this.DataTable_To_CSV(this.dt));
    ((Stream)stream).Write(bytes, 0, bytes.Length);
                                                
    }
    else
    {
    this.IReadDataFromDB("DataBase.db", Query);
    byte[] bytes = Encoding.ASCII.GetBytes(this.DataTable_To_CSV(this.dt));
    ((Stream)stream).Write(bytes, 0, bytes.Length);
    }
    }
    }
    }
    }
    Thread.Sleep(10);
    }
    catch (Exception ex)
    {
    ex.ToString();
    this.IErrormsg = ex.Message;
    MessageBox.Show(ex.ToString());
    TraceService(ex.ToString());
    }
    }
    }

    public void ICloseServer()
    {
    try
    {
    this.listener.Stop();
    }
    catch(Exception ex)
    {
    string ermsg =ex.ToString();
    MessageBox.Show(ex.ToString());
    TraceService(ex.ToString());
    }
    }
    public void IRestartServer()
    {
    try
    {
    this.listener.Start();
    }
    catch (Exception ex)
    {
    ex.ToString();
    this.IErrormsg = ex.Message;
    MessageBox.Show(ex.ToString());
    TraceService(ex.ToString());
    }
    }

    public string DataTable_To_CSV(DataTable dt)
    {
    StringBuilder sb = new StringBuilder();
    try
    {
    if (dt.Rows.Count > 0)
    {
    string[] columnNames = dt.Columns.Cast<DataColumn>().
    Select(column => column.ColumnName).
    ToArray();
    sb.AppendLine(string.Join(",", columnNames));

    foreach (DataRow row in dt.Rows)
    {
    string[] fields = row.ItemArray.Select(field => field.ToString().Replace(",", "")).
    ToArray();
    sb.AppendLine(string.Join(",", fields));
    }
    }
    }
    catch (Exception ex)
    {
    ex.ToString();
    this.IErrormsg = ex.Message;
    TraceService(ex.ToString());
    }
    return sb.ToString();
    }

    public void IReadDataFromDB(string DataBaseName, string Query)
    {
    try
    {

    this.dt = new DataTable();
    this.conn.ConnectionString = "Data Source=" + this.ApplicationPath + "\\" + DataBaseName;
    this.conn.Open();
    new SQLiteDataAdapter(Query, this.conn).Fill(this.dt);
    this.conn.Close();
    }
    catch (Exception ex)
    {
    ex.ToString();
    this.conn.Close();
    this.IErrormsg = ex.Message;
    MessageBox.Show(ex.ToString());
    TraceService(ex.ToString());

    }
    }
    Tuesday, April 10, 2018 6:41 AM

All replies

  • Well, whet you are trying to connect to is not running and listening, or a firewall is blocking access.
    Tuesday, April 10, 2018 6:56 AM
  • Well, whet you are trying to connect to is not running and listening, or a firewall is blocking access.

    firewall is off

    Tuesday, April 10, 2018 6:59 AM
  • it will work for some time ...& after that goes in exception

    Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
    No connection could be made because the target machine actively refused it.

    Tuesday, April 10, 2018 7:00 AM
  • Maybe, you have some database issue and the server has blown-up.  How much data are you trying to pull?
    Tuesday, April 10, 2018 7:33 AM
  • at each poll ,forn each table 100 row 
    Tuesday, April 10, 2018 8:56 AM
  • Well, it seems to me that something is failing on the server side that is stopping the server from listening. What that is you'll have to find out.

    Maybe, it's best that you use a selfhosting WCF server over TCP/IP, a WCF client/server application, that may be more reliable than a legacy TCP/IP client/server application.   WCF does support datatable usage. 

    Tuesday, April 10, 2018 11:54 AM