TCP连接抛出错误主动拒绝连接 [英] TCP Connection throws error actively refused connection
问题描述
public void ICreateServer(string SIpAddress,int SPortNum)
{
尝试
{
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)b
返回;
this.newThread = new Thread(new ThreadStart(this.DoWo rk));
this.newThread.IsBackground = true;
this.newThread.Start();
this.IThreadStarted = true;
}
catch(异常) ex)
{
ex.ToString();
this.IErrormsg = ex.Message;
MessageBox.Show(ex.ToString());
TraceService( ex.ToString());
}
}
private void DoWork()
{
while(this.ThreadTrigger)<登记/>
{
试试
{
this.IsThreadRunning = true ; $
if(this.listener.Pending())
{
&NBSP; 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)。读取(numArray,0,this.client.ReceiveBufferSize);
字符串Query = Encoding.ASCII .GetString(numArray,0,count);
if(Query!= string.Empty&& Query!= null )
{
if(Query.Contains(" AuditLog")))
{
跨度> / / MessageBox.Show(Query);
这个。 IReadDataFromDB(" AuditTrail.db",Query);
byte [] bytes = Encoding.ASCII.GetBytes(this.DataTable_To_CSV (this.dt));
$
((Stream)stream).Write(bytes,0,bytes.Length);
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;
}
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()
{
尝试
{
this.listener.Stop();
}
catch(Exception ex)
{
string ermsg = ex.ToString();
跨度> MessageBox.Show (ex.ToString());
TraceService(ex.ToString());
< span style ="white-space:pre"> }
}
< span style ="white-space:pre"> public void IRestartServer()
{
尝试
{
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();
试试
< span style ="white-space:pre"> {
if(dt.Rows.Count> 0)
{
string [] columnNames = dt.Columns.Cast< DataColumn>()。
选择(column => column.ColumnName )。$
ToArray();
sb.AppendLine(string.Join(",quot;,columnNames));
$
foreach(dt.Rows中的DataRow行)
{
string [] fields = row.ItemArray.Select(field => field.ToString()。Replace(",",""))。br />
ToArray();
sb.AppendLine(string.Join(",quot;,fields));
}
}
}¥b $ b catch(exception ex)
{
ex.ToString();
this.IErrormsg = ex.Message;
TraceService(ex.ToString() ); $
}
return sb.ToString();
}
< span style ="white-space:pre"> public void IReadDataFromDB(string DataBaseName,string Query)
{
尝试
{
这个。 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());
}
}
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());
}
}
推荐答案
好吧,你试图尝试连接到没有运行和监听,或防火墙阻止访问。
Well, whet you are trying to connect to is not running and listening, or a firewall is blocking access.
这篇关于TCP连接抛出错误主动拒绝连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!