当我使用下一个方法启动我的应用程序时,我正在试图连接到套接字:
sck_client->Client_Connect(Convert::ToInt32(numericUpDownPort->Value),ip,sck_client->sck);
这被定义为:
public: void Client_Connect( int Port, IPAddress^ ipAddress, Socket^ %sender) {
try {
IPEndPoint^ remoteEP = gcnew IPEndPoint(ipAddress,Port);
// Create a TCP/IP socket.
sender = gcnew Socket(AddressFamily::InterNetwork, SocketType::Stream, ProtocolType::Tcp );
// Connect the socket to the remote endpoint. Catch any errors.
try {
sender->Connect(remoteEP);
//MessageBox::Show("Socket connected to " + Convert::ToString(sender->RemoteEndPoint), "OPC_Socket_CC", MessageBoxButtons::OK);
} catch (ArgumentNullException^ ane) {
MessageBox::Show("ArgumentNullException : " + ane->Message, "OPC_Socket_CC", MessageBoxButtons::OK);
} catch (SocketException^ se) {
MessageBox::Show("SocketException : " + se->Message, "OPC_Socket_CC", MessageBoxButtons::OK);
} catch (Exception^ e) {
MessageBox::Show("Unexpected exception : " + e->Message, "OPC_Socket_CC", MessageBoxButtons::OK);
}
} catch (Exception^ e) {
MessageBox::Show(e->Message, "Socket_CC", MessageBoxButtons::OK);
}
}
但是如果组合ip +端口不是我网上的有效方向,它会阻止程序大约15秒。
用户第一次启动应用程序时,IP和端口具有默认值,因此可能有不正确的端点地址。如何修改连接超时?
注意:不能使用异步连接。
提前谢谢。