Terminate a process and close all its opened listening ports





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















On my Qt application, I run an external executable on a separate process like this:



QProcess *server = new QProcess();
server->start("./server.exe");


External executable starts a server on 127.0.0.1 listening on port 18383.



Then at some point, I terminate the process like this:



server->terminate();


The problem is that by terminating the server process, many times its used port i.e. 18383 remains open. Consequently, when I try to start the server again, I receive these errors:




"Server -2 Could not setup server. listen tcp :18383: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.




How can I terminate the process and close all its opened listening ports?










share|improve this question























  • Does the terminate() call actually kill the process?

    – Michael Chourdakis
    Jan 3 at 7:20











  • @Michael Well, sometimes I receive these errors while terminating the process: QProcess: Destroyed while process (" ... server.exe ...") is still running

    – user3405291
    Jan 3 at 7:23






  • 1





    In that case, the process object is destroyed while the actual process is still running. You should try a graceful exit instead.

    – Michael Chourdakis
    Jan 3 at 7:35











  • @Michael Thanks! I'm going to study QProcess documentation more to see if I can use its API for a graceful exit.

    – user3405291
    Jan 3 at 7:39






  • 2





    Try opening your port with SO_REUSEADDR, you might just be encountering the situation where the listening port is kept open for a short while to allow for in flight tcp packets to be received

    – Alan Birtles
    Jan 3 at 7:40


















-1















On my Qt application, I run an external executable on a separate process like this:



QProcess *server = new QProcess();
server->start("./server.exe");


External executable starts a server on 127.0.0.1 listening on port 18383.



Then at some point, I terminate the process like this:



server->terminate();


The problem is that by terminating the server process, many times its used port i.e. 18383 remains open. Consequently, when I try to start the server again, I receive these errors:




"Server -2 Could not setup server. listen tcp :18383: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.




How can I terminate the process and close all its opened listening ports?










share|improve this question























  • Does the terminate() call actually kill the process?

    – Michael Chourdakis
    Jan 3 at 7:20











  • @Michael Well, sometimes I receive these errors while terminating the process: QProcess: Destroyed while process (" ... server.exe ...") is still running

    – user3405291
    Jan 3 at 7:23






  • 1





    In that case, the process object is destroyed while the actual process is still running. You should try a graceful exit instead.

    – Michael Chourdakis
    Jan 3 at 7:35











  • @Michael Thanks! I'm going to study QProcess documentation more to see if I can use its API for a graceful exit.

    – user3405291
    Jan 3 at 7:39






  • 2





    Try opening your port with SO_REUSEADDR, you might just be encountering the situation where the listening port is kept open for a short while to allow for in flight tcp packets to be received

    – Alan Birtles
    Jan 3 at 7:40














-1












-1








-1








On my Qt application, I run an external executable on a separate process like this:



QProcess *server = new QProcess();
server->start("./server.exe");


External executable starts a server on 127.0.0.1 listening on port 18383.



Then at some point, I terminate the process like this:



server->terminate();


The problem is that by terminating the server process, many times its used port i.e. 18383 remains open. Consequently, when I try to start the server again, I receive these errors:




"Server -2 Could not setup server. listen tcp :18383: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.




How can I terminate the process and close all its opened listening ports?










share|improve this question














On my Qt application, I run an external executable on a separate process like this:



QProcess *server = new QProcess();
server->start("./server.exe");


External executable starts a server on 127.0.0.1 listening on port 18383.



Then at some point, I terminate the process like this:



server->terminate();


The problem is that by terminating the server process, many times its used port i.e. 18383 remains open. Consequently, when I try to start the server again, I receive these errors:




"Server -2 Could not setup server. listen tcp :18383: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.




How can I terminate the process and close all its opened listening ports?







c++ qt port






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 6:54









user3405291user3405291

1,94312141




1,94312141













  • Does the terminate() call actually kill the process?

    – Michael Chourdakis
    Jan 3 at 7:20











  • @Michael Well, sometimes I receive these errors while terminating the process: QProcess: Destroyed while process (" ... server.exe ...") is still running

    – user3405291
    Jan 3 at 7:23






  • 1





    In that case, the process object is destroyed while the actual process is still running. You should try a graceful exit instead.

    – Michael Chourdakis
    Jan 3 at 7:35











  • @Michael Thanks! I'm going to study QProcess documentation more to see if I can use its API for a graceful exit.

    – user3405291
    Jan 3 at 7:39






  • 2





    Try opening your port with SO_REUSEADDR, you might just be encountering the situation where the listening port is kept open for a short while to allow for in flight tcp packets to be received

    – Alan Birtles
    Jan 3 at 7:40



















  • Does the terminate() call actually kill the process?

    – Michael Chourdakis
    Jan 3 at 7:20











  • @Michael Well, sometimes I receive these errors while terminating the process: QProcess: Destroyed while process (" ... server.exe ...") is still running

    – user3405291
    Jan 3 at 7:23






  • 1





    In that case, the process object is destroyed while the actual process is still running. You should try a graceful exit instead.

    – Michael Chourdakis
    Jan 3 at 7:35











  • @Michael Thanks! I'm going to study QProcess documentation more to see if I can use its API for a graceful exit.

    – user3405291
    Jan 3 at 7:39






  • 2





    Try opening your port with SO_REUSEADDR, you might just be encountering the situation where the listening port is kept open for a short while to allow for in flight tcp packets to be received

    – Alan Birtles
    Jan 3 at 7:40

















Does the terminate() call actually kill the process?

– Michael Chourdakis
Jan 3 at 7:20





Does the terminate() call actually kill the process?

– Michael Chourdakis
Jan 3 at 7:20













@Michael Well, sometimes I receive these errors while terminating the process: QProcess: Destroyed while process (" ... server.exe ...") is still running

– user3405291
Jan 3 at 7:23





@Michael Well, sometimes I receive these errors while terminating the process: QProcess: Destroyed while process (" ... server.exe ...") is still running

– user3405291
Jan 3 at 7:23




1




1





In that case, the process object is destroyed while the actual process is still running. You should try a graceful exit instead.

– Michael Chourdakis
Jan 3 at 7:35





In that case, the process object is destroyed while the actual process is still running. You should try a graceful exit instead.

– Michael Chourdakis
Jan 3 at 7:35













@Michael Thanks! I'm going to study QProcess documentation more to see if I can use its API for a graceful exit.

– user3405291
Jan 3 at 7:39





@Michael Thanks! I'm going to study QProcess documentation more to see if I can use its API for a graceful exit.

– user3405291
Jan 3 at 7:39




2




2





Try opening your port with SO_REUSEADDR, you might just be encountering the situation where the listening port is kept open for a short while to allow for in flight tcp packets to be received

– Alan Birtles
Jan 3 at 7:40





Try opening your port with SO_REUSEADDR, you might just be encountering the situation where the listening port is kept open for a short while to allow for in flight tcp packets to be received

– Alan Birtles
Jan 3 at 7:40












1 Answer
1






active

oldest

votes


















1















sometimes I receive these errors while terminating the process:
QProcess: Destroyed while process (" ... server.exe ...") is still
running




It seems you are not waiting for the process to gracefully terminate.
Here is a generic way to terminate a process you launched :



server->terminate(); 
server->waitForFinished(timeoutMS);
if (server->state() == QProcess::Running) {
server->kill();
}


Terminate will send a polite "can you please stop" signal, kill will abruptly stop the target process. How much time does it take for the server to shut down (it may be several seconds)? so you should have a generous timeout period which take this in account.






share|improve this answer



















  • 1





    Thanks! The error QProcess: Destroyed while process is still running got resolved :) However, I'm not sure if I will receive the Server -2 error on tcp port again. I hope I won't!

    – user3405291
    Jan 3 at 10:00













  • if the server is not leaking network resources it should be fine.

    – UmNyobe
    Jan 3 at 10:14












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017612%2fterminate-a-process-and-close-all-its-opened-listening-ports%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1















sometimes I receive these errors while terminating the process:
QProcess: Destroyed while process (" ... server.exe ...") is still
running




It seems you are not waiting for the process to gracefully terminate.
Here is a generic way to terminate a process you launched :



server->terminate(); 
server->waitForFinished(timeoutMS);
if (server->state() == QProcess::Running) {
server->kill();
}


Terminate will send a polite "can you please stop" signal, kill will abruptly stop the target process. How much time does it take for the server to shut down (it may be several seconds)? so you should have a generous timeout period which take this in account.






share|improve this answer



















  • 1





    Thanks! The error QProcess: Destroyed while process is still running got resolved :) However, I'm not sure if I will receive the Server -2 error on tcp port again. I hope I won't!

    – user3405291
    Jan 3 at 10:00













  • if the server is not leaking network resources it should be fine.

    – UmNyobe
    Jan 3 at 10:14
















1















sometimes I receive these errors while terminating the process:
QProcess: Destroyed while process (" ... server.exe ...") is still
running




It seems you are not waiting for the process to gracefully terminate.
Here is a generic way to terminate a process you launched :



server->terminate(); 
server->waitForFinished(timeoutMS);
if (server->state() == QProcess::Running) {
server->kill();
}


Terminate will send a polite "can you please stop" signal, kill will abruptly stop the target process. How much time does it take for the server to shut down (it may be several seconds)? so you should have a generous timeout period which take this in account.






share|improve this answer



















  • 1





    Thanks! The error QProcess: Destroyed while process is still running got resolved :) However, I'm not sure if I will receive the Server -2 error on tcp port again. I hope I won't!

    – user3405291
    Jan 3 at 10:00













  • if the server is not leaking network resources it should be fine.

    – UmNyobe
    Jan 3 at 10:14














1












1








1








sometimes I receive these errors while terminating the process:
QProcess: Destroyed while process (" ... server.exe ...") is still
running




It seems you are not waiting for the process to gracefully terminate.
Here is a generic way to terminate a process you launched :



server->terminate(); 
server->waitForFinished(timeoutMS);
if (server->state() == QProcess::Running) {
server->kill();
}


Terminate will send a polite "can you please stop" signal, kill will abruptly stop the target process. How much time does it take for the server to shut down (it may be several seconds)? so you should have a generous timeout period which take this in account.






share|improve this answer














sometimes I receive these errors while terminating the process:
QProcess: Destroyed while process (" ... server.exe ...") is still
running




It seems you are not waiting for the process to gracefully terminate.
Here is a generic way to terminate a process you launched :



server->terminate(); 
server->waitForFinished(timeoutMS);
if (server->state() == QProcess::Running) {
server->kill();
}


Terminate will send a polite "can you please stop" signal, kill will abruptly stop the target process. How much time does it take for the server to shut down (it may be several seconds)? so you should have a generous timeout period which take this in account.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 9:46









UmNyobeUmNyobe

17.9k73875




17.9k73875








  • 1





    Thanks! The error QProcess: Destroyed while process is still running got resolved :) However, I'm not sure if I will receive the Server -2 error on tcp port again. I hope I won't!

    – user3405291
    Jan 3 at 10:00













  • if the server is not leaking network resources it should be fine.

    – UmNyobe
    Jan 3 at 10:14














  • 1





    Thanks! The error QProcess: Destroyed while process is still running got resolved :) However, I'm not sure if I will receive the Server -2 error on tcp port again. I hope I won't!

    – user3405291
    Jan 3 at 10:00













  • if the server is not leaking network resources it should be fine.

    – UmNyobe
    Jan 3 at 10:14








1




1





Thanks! The error QProcess: Destroyed while process is still running got resolved :) However, I'm not sure if I will receive the Server -2 error on tcp port again. I hope I won't!

– user3405291
Jan 3 at 10:00







Thanks! The error QProcess: Destroyed while process is still running got resolved :) However, I'm not sure if I will receive the Server -2 error on tcp port again. I hope I won't!

– user3405291
Jan 3 at 10:00















if the server is not leaking network resources it should be fine.

– UmNyobe
Jan 3 at 10:14





if the server is not leaking network resources it should be fine.

– UmNyobe
Jan 3 at 10:14




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017612%2fterminate-a-process-and-close-all-its-opened-listening-ports%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith