本文介绍了使用BeginConnect()的套接字连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

您好,



在socket编程中,beginconnect()的重要性是什么?实际上我遇到了一个错误,因为我做了一个实验:



- 我有一台设备通过网络连接到我的PC应用程序。

- 我然后取下LAN电缆。

- 过了一段时间后,我再次将LAN电缆重新连接回设备。我的PC应用程序抛出一个错误说:

使用beginconnect再次连接,也可以使用另一个端点。



- 所以我尝试了另一件事:我编写了以下代码:



套接字s =新套接字(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

IPEndPoint ip = new IPEndPoint(IPAddress.Parse(172.16.3.230),502);

s.Connect(ip);

do

{

Console.WriteLine(connect?);

string s1 = Console.ReadLine();

如果(s1 ==y)

{

s.Connect(ip);

}





} while(true);



}



- 在此我按y重新连接。但是这一次它给出了另一个错误:



你已经连接到那个端点了。这里的错误是不同的。这是否意味着beginconnect是重新连接的最佳选择?



我读到Socket.BeginConnect():它的语法是:



public IAsyncResult BeginConnect(

IPAddress []地址,------ 1

int port,------ 2

AsyncCallback requestCallback,------ 3

对象状态------ 4





有人可以向我解释第四个论点吗?



谢谢,

- Rahul

Hello,

Whats the significance of beginconnect() in socket programming? Actually i encountered an error because i did an experiment:

- I had a device connected on network to my PC application.
- I then removed the LAN cable.
- After sometime i again reconnected the LAN cable back to the device. My PC application threw an error saying :
"use beginconnect to connect once again and that too to a different endpoint".

- So i tried out another thing: i wrote the following code:

Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("172.16.3.230"),502);
s.Connect(ip);
do
{
Console.WriteLine("connect?");
string s1 = Console.ReadLine();
if (s1 == "y")
{
s.Connect(ip);
}


} while (true);

}

- In this i press "y" to reconnect again. However this time it gives another error saying:

"You are already connected to that endpoint". The error here is different. Does this imply that beginconnect is the best option to reconnect?

I read about Socket.BeginConnect(): its syntax is :

public IAsyncResult BeginConnect(
IPAddress[] addresses, ------1
int port, ------2
AsyncCallback requestCallback, ------3
Object state ------4
)

Can someone please explain me the 4th argument?

Thanks,
- Rahul

推荐答案


这篇关于使用BeginConnect()的套接字连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 23:07