473,765 Members | 2,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a problem about Socket.connect( ) !

I try to connect to server using following codes:
(smart device application)

Socket s=new Socket();
try
{
s.connect(host) ;//host is an object of IPEndPoint class
}
catch (SocketExceptio n)
{
System.Windows. Forms.MessageBo x.Show("can't connect..");
}
Why does it takes 5 minutes to show the "can't connect"
message?(Users must wait for long time to know the
message.)Is there any solution to solve this problem??
PS:it takes only 5 seconds to show the message when
I use the same codes in windows application.
Nov 15 '05 #1
3 5585
cocla,

The reason probably has to do with the underlying API implementation of
the WinSock layer. If you code something similar using unmanaged code, do
you see the same delay? The classes in the System.Sockets namespace really
just wrap the WinSock API functions.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"cocla" <u0******@cc.nc u.edu.tw> wrote in message
news:06******** *************** *****@phx.gbl.. .
I try to connect to server using following codes:
(smart device application)

Socket s=new Socket();
try
{
s.connect(host) ;//host is an object of IPEndPoint class
}
catch (SocketExceptio n)
{
System.Windows. Forms.MessageBo x.Show("can't connect..");
}
Why does it takes 5 minutes to show the "can't connect"
message?(Users must wait for long time to know the
message.)Is there any solution to solve this problem??
PS:it takes only 5 seconds to show the message when
I use the same codes in windows application.

Nov 15 '05 #2
"cocla" <u0******@cc.nc u.edu.tw> wrote in message news:<06******* *************** ******@phx.gbl> ...
I try to connect to server using following codes:
(smart device application)
Why does it takes 5 minutes to show the "can't connect"
message?(Users must wait for long time to know the
message.)Is there any solution to solve this problem??
PS:it takes only 5 seconds to show the message when
I use the same codes in windows application.


cocla -

Instead of waiting for the synchronous Connect() method to
timeout, you can try using the asynchronous BeginConnect() method,
then set a timer for the amount of time you want to wait for the
connection. If the timer goes off before the connection is
established, you can close the socket and go on with life (you will
need to catch the Exception the EndConnect() method will throw).

Here's a small code snippet using this technique:

Socket sock = new Socket(AddressF amily.InterNetw ork,
SocketType.Stre am,
ProtocolType.Tc p);
IPEndPoint iep = new IPEndPoint(
IPAddress.Parse ("192.168.0.1", 9050);
IAsyncResult ar = sock.BeginConne ct(iep,
new AsyncCallback(C onnectedMethod) ,sock);
if (ar.AsyncWaitHa ndle.WaitOne(50 00, false))
{
Console.WriteLi ne("Connected") ;
// the socket is connected to the remote site, continue...

} else
{
Console.WriteLi ne("Not Connected");
sock.Close(); //this forces the EndConnect() to throw an
Exception
}

The WaitOne() method waits 5 seconds for the socket to connect. If
not, it closes the socket. In the ConnectedMethod () code, you need to
catch the Exception that might be thrown if the socket is closed:

void ConnectedMethod (IAsyncResult iar)
{
Socket client = (Socket)iar.Asy ncState;
try {
client.EndConne ct(iar);
// if this succeeds, continue on with the socket...
} catch(SocketExc eption) {
{
//if the socket was closed, do nothing...
}
}
Hope this gives you some ideas to use in your project.

Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
"Network Performance Open Source Toolkit" (Wiley)
http://www.wiley.com/WileyCDA/WileyT...471433012.html
Nov 15 '05 #3
What is the ConnectTimeout property set to for the socket?

"cocla" <u0******@cc.nc u.edu.tw> wrote in message
news:06******** *************** *****@phx.gbl.. .
I try to connect to server using following codes:
(smart device application)

Socket s=new Socket();
try
{
s.connect(host) ;//host is an object of IPEndPoint class
}
catch (SocketExceptio n)
{
System.Windows. Forms.MessageBo x.Show("can't connect..");
}
Why does it takes 5 minutes to show the "can't connect"
message?(Users must wait for long time to know the
message.)Is there any solution to solve this problem??
PS:it takes only 5 seconds to show the message when
I use the same codes in windows application.

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2273
by: faktujaa | last post by:
Hi, I am having some problem with callback used in socket implementation. private static void Connect(string strPrtrIPAddr, int intPrtrPort, ref Socket rsocClient) { try { // Create remote end point. System.Net.IPAddress IPAddress = System.Net.IPAddress.Parse(strPrtrIPAddr); System.Net.IPEndPoint IPEndPoint = new System.Net.IPEndPoint(IPAddress,
4
2168
by: 0to60 | last post by:
I have a question about socket programming in general. Exactly what happens behind the scenes when I one socket connects to a different socket in listen mode? Using the dotnet framework, I create a socket, bind it to a port, put it in listen mode, and then n sockets can connect to it. The code: Socket newSocket = listeningSocket.Accept(); returns a socket. I can communicate on newSocket, and listeningSocket goes
4
7090
by: zbcong | last post by:
Hello: I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting for a incoming connection,the relative code snipprt as following:: private IPAddress myIP=IPAddress.Parse("127.0.0.1"); private IPEndPoint myServer; private Socket socket; private Socket accSocket; private System.Windows.Forms.Button button2;...
8
6001
by: Mark Fink | last post by:
I try to port a server application to Jython. At the moment I use Jython21\Lib\socket.py Currently I do face problems with casting the string "localhost" to the desired value: D:\AUT_TEST\workspace\JyFIT>jython fit/JyFitServer2.py localhost 1234 23 localhost Traceback (innermost last): File "fit/JyFitServer2.py", line 146, in ?
14
25270
by: DaTurk | last post by:
I am makeing a Multicast server client setup and was wondering what the difference is between Socket.Connect, and Socket.Bind. It may be a stupid question, but I was just curious. Because I tried a test, and I can't bind two sockets to the same port/ip but I can connect with one socket, and bind with another to the same ip/port. Thanks
0
2421
by: doc | last post by:
Hi Hard to know where to post this and I don't want to double post it - please move it if you need to, I am trying to connect a Flash client socket (XML) to a php socket server (using Flash 8 and php 5). My php socket server responds correctly when I connect to it with a php client socket but I cannot connect with the flash xml client socket. Both codes are included below - they are both in the same folder on the web site and I am only...
3
3421
by: doc | last post by:
What will a flash xml client socket connect to? I have a working php TCP/IP server socket bound to a port >1023 and the flash client will not even connect to it. I can connect to it with non-xml client. Can anyone explain this to me - it has been driving me mad for weeks! Please help! Thanks
2
2208
by: sasimca007 | last post by:
Hello friends, When i used the following code and run i have an error: connect: Connection refused at sock.pl line 20. The code is: #!/bin/perl -w # Example of a TCP daytime client using perl calls directly
2
2403
by: Mirko Vogt | last post by:
Hey, it seems that the socket-module behaves differently on unix / windows when a timeout is set. Here an example: # test.py import socket sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM) print 'trying to connect...'
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5277
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.