C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 307,110 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,015 people online right now. Registration is fast and FREE... Join Now!




How check result of Connect() if server not found?

 

How check result of Connect() if server not found?

nima_pw

29 Jun, 2009 - 04:46 AM
Post #1

New D.I.C Head
*

Joined: 8 Jun, 2009
Posts: 9


My Contributions
Hi all,

I want when server not start yet, when client click on connect button,after 10 ms a message show to him (her) like "Server Not Found!"

But i don't know how find that server is run or no?
I test this for example (it isn't a real code, didn't do new and ...):

CODE
socket client;
client.connect();
if(client.Connect()!=-1)
{
MessegeBox.Show("Server Not Found");
Return();
}


But dosen't work, an error occur that says client.Connect() dosen't return a integer value.
Then how i must check result of socket programming functions like Connect(),Accept(), Listen() etc... in C# to secure that a function worked properly?

I hope explain oneself clearly.

Best regards,
Nima

User is offlineProfile CardPM
+Quote Post


eclipsed4utoo

RE: How Check Result Of Connect() If Server Not Found?

29 Jun, 2009 - 04:53 AM
Post #2

Not Your Ordinary Programmer
Group Icon

Joined: 21 Mar, 2008
Posts: 1,846



Thanked: 205 times
Dream Kudos: 500
Expert In: .NET

My Contributions
I would put it in a try/catch block. .Net will most likely throw an exception if the server is not found.
User is offlineProfile CardPM
+Quote Post

nima_pw

RE: How Check Result Of Connect() If Server Not Found?

29 Jun, 2009 - 07:09 AM
Post #3

New D.I.C Head
*

Joined: 8 Jun, 2009
Posts: 9


My Contributions
Meaning in C# we have not local variable to found error like C language? (errno variable and perror() are to find bad result of socket function)
Only way for solve this problem is Try/Catch?
User is offlineProfile CardPM
+Quote Post

eclipsed4utoo

RE: How Check Result Of Connect() If Server Not Found?

29 Jun, 2009 - 07:16 AM
Post #4

Not Your Ordinary Programmer
Group Icon

Joined: 21 Mar, 2008
Posts: 1,846



Thanked: 205 times
Dream Kudos: 500
Expert In: .NET

My Contributions
try/catch is used to catch exceptions...

c#

try
{
socket.Open();
}
catch (SocketException socketEx)
{
MessageBox.Show("Socket Exception Occurred :: " + socketEx.Message);
}
catch(Exception ex)
{
MessageBox.Show("General Exception Occurred :: " + ex.Message);
}


you can also get the exception type by doing...

c#

MessageBox.Show(ex.GetType().ToString());


This post has been edited by eclipsed4utoo: 29 Jun, 2009 - 07:20 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 01:02PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month