?
Solved

Testing if a firewall blocks a port

Posted on 2003-03-03
3
Medium Priority
?
268 Views
Last Modified: 2013-11-15
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
Comment
Question by:wym
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 

Accepted Solution

by:
rooster_0429 earned 200 total points
ID: 8065536
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
 
LVL 20

Expert Comment

by:jmcg
ID: 10058639
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
This video shows how use content aware, what it’s used for, and when to use it over other tools.
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question