Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to do reconnect socket when port closed or idled?

Posted on 2007-03-30
6
Medium Priority
?
1,213 Views
Last Modified: 2008-05-10
Hi Experts,
I am using winsock to work with Socket for MFC Window application.
I used port 8000 to connect to socket server,

Can you give some suggestion about how to do re-connect to server when server port 8000 is closed or idled for some reason?

Many thanks!

0
Comment
Question by:CongUan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 11

Accepted Solution

by:
Deepu Abraham earned 500 total points
ID: 18822475
You could use closesocket and try reconnecting.Also make sure that you have set tcp linger option (using setoption()) off while creating the socket.
Best Regards,
DeepuAbrahamK
0
 
LVL 11

Expert Comment

by:Deepu Abraham
ID: 18822484
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 500 total points
ID: 18836625
>>>> how to do re-connect to server when server port 8000 is closed or idled
I never heard of a "idled" port.

From my experience you must do nothing special. A  port or network device deactivated by an administrative action should be available again after it activated again. That may last a little time as TCP/IP is an asynchronously running subssystem.

The main problem maybe is that your prog needs to be able to handle such cases, i. e. instead of leaving a read or write operation in an undefined state it must identify the error returned a previous action as an error which might be temporary only and do periodically recalls, e. g. like that:

    int count = 0;
    while (true)
    {
           if (!readFromSocket())
           {
                 if (lastError == FATAL || ++count > MAX_RETRY)
                       exit(1);         // leave prog in case of a fatal error
                Sleep(1000);      // sleep a second
           }
    }

The lastError and readFromSocket used above are assumed to be members of an appropriate class.

Regards, Alex
0
 
LVL 6

Expert Comment

by:SeanDurkin
ID: 21466803
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup Zone:
      Split: DeepuAbrahamK {http:#18822475} & itsmeandnobodyelse {http:#18836625}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Sean
EE Cleanup Volunteer
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question