简体   繁体   中英

How to restart a sockets program?

I need my server to stay connected to the server. Does anyone know how to do this? Or post links tutorials anything?

Also it says when it restarts 'could not accept client' so how would I clear everything and make it accept it?

Server code:

For your server side code, do a loop wrapping the accept call. For the accepted socket that is created create a new thread, so that the next accept will be called right away.

On server startup you may also want to use the SO_REUSEADDR flag. That way if you had a crash, or even a fast restart of the program, then your server will be able to use the same port again without a problem.

Client code:

For your client code you would just check for a socket error and if that occurs just establish a new connection.

Other resources:

Other options:

Instead of plain bsd-style sockets, you could also try using boost asio for easier socket programming. You could check out theirexamples page .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM