Solved

How to do reconnect socket when port closed or idled?

Posted on 2007-03-30
6
1,212 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:
DeepuAbrahamK earned 125 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:DeepuAbrahamK
ID: 18822484
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 125 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

622 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