• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

Testing if a firewall blocks a port

Hello:

I would like to write a small module that tests if a firewall is blocking a port.  For instance, my main application requires that the SQL server port is open.  If a firewall is blocking the port, my application hangs.  I'd like to exit gracefully and notify the user to open the port.

I seem to remember a really long time ago writing a winsock application that has the server listening on a port.  I was writing on Unix and I had the server fork a child process.  If the child process is blocked from connecting to the parent's listening port, the parent will kill everything after a certain amount of time.

Is this method sound and what's the Windows equivalent of fork()?  CreateThread?  If anyone can direct me to a sample code of something like this (usually it's separate server/client programs), I'd appreciate it.
0
wym
Asked:
wym
1 Solution
 
rooster_0429Commented:
You should avoid using fork() for network application programming for multiple reasons.
1. multithreading is better. check out the posix thread library.
2. there is no equivilent to fork() in windows because fork() is a unix system call and not part of C.
3. fork can get hairy if you need to share memory between processses. With threads you simply use mutexes.


As for checking if a port is open look at this:

 int sockfd, rc;
 struct hostent* he;
 struct sockaddr_in their_addr;

 he = gethostbyname(SERVER);
 their_addr.sin_family = AF_INET;
 their_addr.sin_port = htons(SERVERPORT);
 their_addr.sin_addr = *((struct in_addr*)he->h_addr);
 memset(&(their_addr.sin_zero),'\0',8);
 sockfd = socket(AF_INET, SOCK_STREAM, 0);
 rc = connect(sockfd,(struct sockaddr*)&their_addr, sizeof(struct sockaddr));

basically you should have some code that might look like this. check the return value on connect().

if(rc == -1)
 //then connect failed i.e. broken pipe.
 //you can also look at the error codes to see exactly why connect failed.
0
 
jmcgOwnerCommented:
Nothing has happened on this question in more than 10 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by rooster_0429 [grade B] (sketchy, does not address hard parts of the problem).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now