嗨,我使用的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地址。誰能告訴我爲什麼發生這種情況。即使我也刪除防火牆設置。
你在同一時間按下兩個按鈕,或者我不明白的東西... – Cynede
是目標機器上的端口免費供您使用? – MrFox
@Heather一鍵點擊連接插座,然後傳送和第二個按鈕recive數據 – Jankya