![](/img/trans.png)
[英]SignalR Error “No connection could be made because the target machine actively refused it”
[英]No connection could be made because the target machine actively refused it why?
嗨我正在使用TCPCLient和TCPlitner傳輸數據,但我收到錯誤不連接下面是我的代碼
private void button1_Click(object sender, EventArgs e)
{
TcpClient tcpc = new TcpClient("192.168.21.46", 10);
NetworkStream nts = tcpc.GetStream();
if (nts.CanWrite)
{
Byte[] sends = System.Text.Encoding.ASCII.GetBytes(textBox1.Text.ToCharArray());
nts.Write(sends, 0, sends.Length);
nts.Flush();
}
}
private void button2_Click(object sender, EventArgs e)
{
TcpListener myListener = new TcpListener(10);
myListener.Start();
while (true)
{
//Accept a new connection
Socket mySocket = myListener.AcceptSocket();
if (mySocket.Connected)
{
//make a byte array and receive data from the client
Byte[] receive = new Byte[64];
int i = mySocket.Receive(receive, receive.Length, 0);
char[] unwanted = { ' ', ' ', ' ' };
string rece = System.Text.Encoding.ASCII.GetString(receive);
label1.Text = rece.TrimEnd(unwanted);
}
}
}
這兩個按鈕我添加了相同的形式和Ip apddress,這是我的系統IP地址。 任何人都可以告訴我為什么會發生這種情況。 即使我也刪除了防火牆設置。
我假設你有兩個不同的程序,一個正在監聽而另一個正在發送。 如果不這樣做,請將它們分開。 我認為你有一個網絡問題,但不能確切地說它被阻止了。
你正在使用端口10,它通常對於任意應用程序使用來說太低了,即使它當前沒有被占用。 如果您正在構建自定義協議,請嘗試偵聽大約1000以上的端口(更多經驗法則)。 您可以在http://technet.microsoft.com/en-us/library/cc959828.aspx或google查看“tcp well known ports”以獲取更多詳細信息,並確保您沒有發生沖突。
嘗試運行TCP偵聽器,然后打開命令提示符並鍵入以下內容
telnet localhost 10
或使用您更改10的端口號。 我嘗試了你的代碼,並能夠毫無問題地連接到端口10。 如果您沒有“telnet”程序,則可以在Windows中打開該功能。 (搜索“功能”並選擇“打開或關閉Windows功能”然后打開“Telnet客戶端”)
如果telnet程序連接,您將知道該問題與連接無關。 如果telnet程序無法連接,那么你就會知道某些東西仍在阻塞該端口。 嘗試使用其他端口並重新測試連接。 一旦telnet連接,您就可以專注於程序的TCP客戶端部分。
首先,你的UI將在按鈕2上單擊,因為它停留在while(true)循環上,因此使用BeginAcceptSocket(IAsyncResult r,Object state)進行異步。
其次,你必須使用環回地址,否則防火牆應該阻止端口10,假設它沒有打開。 此外,TcpListener(int端口)是obsolote,它最好使用TcpListener(IPAddress localddr,int port)並使用兩個環回地址。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.