H4cKL0rD
H4cKL0rD

Reputation: 5508

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?

Upvotes: 1

Views: 888

Answers (1)

Brian R. Bondy
Brian R. Bondy

Reputation: 347206

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 their examples page.

Upvotes: 5

Related Questions