Está en la página 1de 2

ASSIGNMENT NO .

Aim: Implement Port Scanner Progam in c/c++.

Theory:

Use AF_INET Address family:

AF_INET address family sockets can be either connection-oriented (type SOCK_STREAM) or


they can be connectionless (type SOCK_DGRAM). Connection-oriented AF_INET sockets use
TCP as the transport protocol. Connectionless AF_INET sockets use UDP as the transport
protocol. When you create an AF_INET domain socket, you specify AF_INET for the address
family in the socket program. AF_INET sockets can also use a type of SOCK_RAW. If this type
is set, the application connects directly to the IP layer and does not use either the TCP or UDP
transports.

AF_INET address structure:

sin_len :This field contains the length of the address for UNIX specifications. sin_len field is
only provided for BSD 4.4 compatibility. It is not necessary to use this field even when using
BSD 4.4/UNIX 98 compatibility. The field is ignored on input addresses.

sin_family :This field contains the address family, which is always AF_INET when TCP or
UDP is used.

sin_port :This field contains the port number. sin_addr This field contains the Internet address.

sin_zero :This field is reserved. Set this field to hexadecimal zeros.

htons() function converts the port number into a Big Endian short integer

Method: accept

Name: accept - accept a connection on a socket

Syntax: int accept(int s, struct sockaddr *addr, socklen_t *addrlen);

Description: The accept function is used with connection-based socket types


(SOCK_STREAM, SOCK_SEQPACKET and SOCK_RDM). It extracts the first connection
request on the queue of pending connections, creates a new connected socket with mostly the
same properties as s, and allocates a new file descriptor for the socket, which is returned. The
newly created socket is no longer in the listening state. The original socket s is unaffected by this
call. Note that any per file descriptor flags (everything that can be set with the F_SETFL fcntl,
like non blocking or async state) are not inherited across an accept.
Method :connect

Name: connect - initiate a connection on a socket

Syntax: int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen);

The file descriptor sockfd must refer to a socket. If the socket is of type SOCK_DGRAM then
the serv_addr address is the address to which datagrams are sent by default, and the only address
from which datagrams are received. If the socket is of type SOCK_STREAM or
SOCK_SEQPACKET, this call attempts to make a connection to another socket. The other
socket is specified by serv_addr, which is an address (of length addrlen) in the communications
space of the socket. Each communications space interprets the serv_addr parameter in its own
way.

Return Value: If the connection or binding succeeds, zero is returned. On error, -1 is


returned, and errno is set appropriately.

También podría gustarte