Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

1 solution

 
Share this answer
 
Comments
Rahul VB 17-Jan-14 5:31am    
thanks sir,
But i have already gone through that article. I dint understand the 4th argument of the beginconnect(). I already have mentioned it above.

Thanks again,
- Rahul
Richard MacCutchan 17-Jan-14 5:39am    
state
Type: System.Object
A user-defined object that contains information about the connect operation. This object is passed to the requestCallback delegate when the operation is complete.


It is a reference to any object that you wish to pass back to your delegate code.
Rahul VB 17-Jan-14 6:01am    
Thanks a lot,
- Rahul

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900