I wrote a program that opens a port and listens to it. Here's simplified version:
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow) {
WORD sockVersion;
WSADATA wsaData;
int nret;
other code consists of error checking and reporting.
What could couse the program to stay running and what to do to close it? I have to close it manualy through task manager now. If i don't do it, the port doesn't open, becouse it is in use.
You have to manually close it because it's blocked in accept, waiting for a connection. If comething connected, accept would return the new socket connection, you'd then close it and terminate. You can test it with telnet.
If you want to maintain control, you can use select with a timeout. select will return if it receives a connection or the timeout occurs. You'd then have to loop until you decide that you're ready to stop.