[C ] Simple TCP Port Scanner Coded by Me




Posts: 5

Joined: Tue Jun 17, 2008 8:14 pm

Post Tue Jun 17, 2008 8:24 pm

[C ] Simple TCP Port Scanner Coded by Me

This is a simple port scanner coded in c. It uses a simple socket and a for loop. The port scanner uses TCP Connect  to check whether the port is opened or closed.

This is for beginners who are trying to grasp simple sockets in C.

By the way this is for linux platform you can easily compile this on win32 using cygwin.

/* A TCP port scanner created by billy www.softhardware.co.uk*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <errno.h>

/* Main programs starts*/
int main(int argc, char **argv)
  int  sd;        //socket descriptor
  int    port;        //port number
  int  start;        //start port
  int    end;        //end port
  int    rval;        //socket descriptor for connect 
  char    responce[1024];      //to receive data
  char  *message="shell";      //data to send
  struct hostent *hostaddr;  //To be used for IPaddress
  struct sockaddr_in servaddr;  //socket structure
  if (argc < 4 )
      printf("------Created By www.Softhardware.co.uk-----------\n");
      printf("Usage: ./tscan <IPaddress> <Start Port> <End Port>\n");
      return (EINVAL);
  start = atoi(argv[2]);
  end  = atoi(argv[3]);
  for (port=start; port<=end; port++)

        //portno is ascii to int second argument     

  sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); //created the tcp socket
  if (sd == -1)
    return (errno);

  memset( &servaddr, 0, sizeof(servaddr));

  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(port); //set the portno
  hostaddr = gethostbyname( argv[1] ); //get the ip 1st argument
  memcpy(&servaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length);
  //below connects to the specified ip in hostaddr

  rval = connect(sd, (struct sockaddr *) &servaddr, sizeof(servaddr));
  if (rval == -1)
  printf("Port %d is closed\n", port);
  printf("Port %d is open\n",port);
  close(sd);        //socket descriptor



Jr. Member
Jr. Member

Posts: 80

Joined: Sun Nov 04, 2007 2:24 pm

Post Wed Jun 18, 2008 12:33 am

Re: [C ] Simple TCP Port Scanner Coded by Me

@billy786: Make it multithreaded.

@others:Read beej's guide to understand socket programming in C.
http://beej.us/guide/bgnet/output/html/ ... index.html

There is no rule, law or tradition that apply universally... including this one.


User avatar

Hero Member
Hero Member

Posts: 928

Joined: Fri Jan 04, 2008 8:54 am

Location: UK

Post Wed Jun 18, 2008 3:47 am

Re: [C ] Simple TCP Port Scanner Coded by Me


looks promising, might make a nice base for a project I've had in mind for a while. Thanks for sharing.

shakuni wrote:@billy786: Make it multithreaded.

I'd suggest this could be left as 'an excercise for the reader'. As Billy states this is good for beginners trying to grasp the basics, too many advanced features may just confuse matters (aka me ;) )


User avatar

Full Member
Full Member

Posts: 180

Joined: Fri Oct 26, 2007 12:45 pm

Location: Peoria, IL

Post Wed Jun 18, 2008 4:03 pm

Re: [C ] Simple TCP Port Scanner Coded by Me

@Billy786: Thanks for the post!

@shakuni: Thanks for the link!

Looks like great information for a beginning programmer like me.  :)
Last edited by geekyone on Wed Jun 18, 2008 4:07 pm, edited 1 time in total.

Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

Powered by phpBB® Forum Software © phpBB Group.
Designed by ST Software