gpt4 book ai didi

sockets - TCPClient 只会连接一次

转载 作者:可可西里 更新时间:2023-11-01 02:35:06 26 4
gpt4 key购买 nike

我正在构建一个通过 TCP 与机器通信的应用程序。我遇到的问题是我可以连接并且一切正常但是一旦我断开连接然后尝试重新连接我得到的只是错误

我在表单中的代码是

   private void btn_connect_Click(object sender, EventArgs e)
{
try
{

Log(LogMsgType.Normal, String.Format("Connected on {0}\n", DateTime.Now));

string ip = (txt_ipadd_1.Text + "." + txt_ipadd_2.Text + "." + txt_ipadd_3.Text + "." + txt_ipadd_4.Text);

Log(LogMsgType.Normal, String.Format("Connecting to IP address " + ip + " Port # " + txt_port.Text +"\n"));

string con = Codenet.Connect_TCP(ip, Convert.ToInt32(txt_port.Text)); //connect to this one

Log(LogMsgType.Normal, String.Format(con + "\n"));

toolStripStatusLabel1.Text = "Connected";

btn_connect.Visible = false;
btn_disconnect.Visible = true;
ip = "";


}

catch (Exception d)
{
Log(LogMsgType.Error, String.Format("Error..... " + d.StackTrace +"\n"));
}
}

从 Codenet dll 转到连接和断开连接的代码

     public static string Connect_TCP(string ip, int Port)
{
tcpclnt.Connect(ip, Port);
connection_type = "TCP";
return "Connected OK ";
}

public static void DisConnect_TCP()
{
tcpclnt.GetStream().Close();
tcpclnt.Close();
}

我得到的错误是:

Error..... at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port) at
comms_engine.Codenet.Connect_TCP(String ip, Int32 Port) in C:\a\code\g_com_test_1\comms_engine\Codenet.cs:line 37 at
comms_test.CommTester.btn_connect_Click(Object sender, EventArgs e) in C:\a\code\g_com_test_1\dll_test\ethernet.cs:line 98

即使 ip 不同,它也不会执行 tcpclnt.Connect(ip, Port)。似乎有些东西仍然打开,但我不知道是什么,因为机器已经断开连接。我知道,因为我有 UDP,它会发送它有多少个连接,我可以看到然后增加和减少 OK。

我尝试了“public string Connect_TCP(string ip, int Port)”并在从表单调用连接之前制作了一个新的 dll 实例,但这也不起作用。

任何想法我做错了什么。

感谢到目前为止的回答,但我仍然有问题。我阅读了 MSDN 并制作了一个测试应用程序,现在我可以看到出了什么问题,但目前对如何修复它一无所知。我将 Client 声明为 Static global,因此我可以从连接和断开连接按钮获取它。我认为这总是会创建新实例,而且我从不关闭它们。机器第一次告诉我它已经断开连接,但应用程序永远不会重新连接在 Debug模式下,它在 client.Connect(ipEndPoint) 处失败; “InvalidOperationException was unhandled”后跟“一旦套接字断开连接,您只能异步重新连接,并且只能重新连接到不同的端点。必须在操作完成之前不会退出的线程上调用 BeginConnect。”

我想我需要将这个 Client 放在 btn_connect_click 中并从断开连接中获取指向它的指针,但是怎么做呢?

完整代码为

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.IO.Ports;
using System.Diagnostics;
using System.Threading;

namespace TCP_Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

static Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

private void btn_connect_Click(object sender, EventArgs e)
{

string ip = (txt_ipadd_1.Text + "." + txt_ipadd_2.Text + "." + txt_ipadd_3.Text + "." + txt_ipadd_4.Text);

IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddr = IPAddress.Parse(ip);
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, Convert.ToInt32(txt_port.Text));

// Connect the socket to the remote end point.
client.Connect(ipEndPoint);

rchTxtBx_output.AppendText("connected to " + ipEndPoint + "\n\r");
}

private void btn_disconnect_Click(object sender, EventArgs e)
{
client.Shutdown(SocketShutdown.Both);

client.Disconnect(true);
if (client.Connected)
rchTxtBx_output.AppendText("We're still connnected \n\r");
else
rchTxtBx_output.AppendText("We're disconnected \n\r");

}




}


}

最佳答案

一旦断开 TCP 套接字,就无法重新连接。您必须释放套接字并创建一个新套接字。这不仅限于 .NET TCPClient 类,这是所有套接字 API(WinSock、BSD 等)的一般工作方式。

此规则的异常(exception)是 WinSock2 特定的 DisconnectEx() 函数,它有一个 TF_REUSE_SOCKET 标志,允许 ConnectEx() (和 AcceptEx()) 重新使用现有的 SOCKET 而不是需要创建新的套接字。但这不适用于您的情况。

在大多数情况下,一旦断开套接字,就必须从头开始创建一个新套接字。

关于sockets - TCPClient 只会连接一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25908270/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com