Created
July 21, 2014 14:56
-
-
Save rodrigocnascimento/d8f84dbccba93c5d6306 to your computer and use it in GitHub Desktop.
Check if port are opened to connections in C maybe a future php module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <netdb.h> | |
int main(int argc, char *argv[]) | |
{ | |
int portno = 0000; | |
char *hostname = "IP"; | |
int sockfd; | |
struct sockaddr_in serv_addr; | |
struct hostent *server; | |
sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
if (sockfd < 0) { | |
error("ERROR opening socket"); | |
} | |
server = gethostbyname(hostname); | |
if (server == NULL) { | |
fprintf(stderr,"ERROR, no such host\n"); | |
exit(0); | |
} | |
bzero((char *) &serv_addr, sizeof(serv_addr)); | |
serv_addr.sin_family = AF_INET; | |
bcopy((char *)server->h_addr, | |
(char *)&serv_addr.sin_addr.s_addr, | |
server->h_length); | |
serv_addr.sin_port = htons(portno); | |
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) { | |
printf("Port is closed"); | |
} else { | |
printf("Port is active"); | |
} | |
close(sockfd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment