Socket programming, not much software engineer take interest in this especially in my country don’t know about your. I am also new to this and the reason to start learning socket programming is, its my dream to hack at least one computer in my life, i tried to learn metaspolit but not a single exploit is successful if i have firewall on(the default firewall of windows xp or 7) and most of the exploit are very old and and there patch are updated on every system.

So instead of taking the approach of exploit i thought to create my own application which connect me to the remote system with a surety that if a user will do a mistake to download it my code will work at least in LAN.

I choose the python language to do the socket programming, it is simple and in term of looks also pretty cool. So this first tut0rial is about the basic of socket programming in python, how to create a socket what all arguments are there and there meaning.

Socket function is divided in three arguments :

syntax is : s= socket.socket(domain, type, protocol)

you have to import the socket module in your program, so first is domain it determines the nature of communication including the address format the constraint starts with AF_ (for address family) because each domain has its own format for defining an address.

AF_INET :-This is for IPv4 Internet Domain

AF_INET6 :- This is for IPv6 Internet Domain

AF_UNIX :- Unix domain

AF_UNSPEC :- unspecified.

These are the four domain types which you will use in defining the socket. The next is type it defines the type of socket which further determine the communication characteristic

SOCK_DGRAM :- fixed length,connection less, unreliable message

SOCK_RAW :- datagram interface to IP, to build your own protocol.

SOCK_SEQPACKET :- it is fixed length , reliable, connection oriented message

SOCK_STREAM :- sequence reliable bidirectional connection oriented.

These are the four types. Next is protocol this is usually zero to select the default protocol for a given domain and socket type. When a multiple protocol are supported for the same domain and socket type, we can define the protocol argument to select the particular protocol.

The default protocol for a SOCK_STREAM socket in the AF_INET communication domain is TCP. The default protocol for the SOCK_DGRAM socket in the AF_INET communication domain is UDP.

So far we know about the socket declaration, the proper syntax for TCP connection in IPv4 will be :-

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

It is very important to know the declaration of the socket and it goes same with C socket programming also. If you are aware of python or C programming then you will understand my this comparison, calling a socket is similar to calling a open, in both case you get a file descriptor that can be used for I/O. When you are done using the file descriptor you call the close and free up the file descriptor for reuse.

So as you open the socket you need to close the connection also, there are two ways to close the socket. first is socket.close() function. Close will release the resources associated with the connection but does not necessarily close the connection immediately. If you want to close the connection in a proper way then shutdown the socket before calling close.

syntax :- socket.shutdow(how)

how could be :

if how is SHUT_RD then reading from the socket is disabled.

if how is SHUT_WR then writing form the socket is disabled.

if how is SHUT_RDWR then it will disable both data transmission and reception.

There are couple of reason why shutdown is needed , close() will deallocate the network endpoints. only when the last reference is closed this means that if we duplicate the socket the socket wont be deallocate until we closed the last file descriptor referring to it. The shutdown function allow us to deactivate the socket independently of the number of active file descriptor referring to it,

Second reason is it sometimes convenient to shut a socket down in one direction only, for example, we can shut a socket down for writing if we want the process we are communicating with to be able to determine when we are done transmitting data, while still allowing us to use the socket to receive the data send to us by the process.

This is the very very basic of the socket, in the second post we will look at the socket bind function, listen, accept, and connect. Please follow us on facebook or via blog.

I hope this post will help you understand the basic of socket declaration.

Follow us on Facebook.

Thanks
Abhishek.

Comments
  1. rishidevdas says:

    Very few people are able to write such brilliant socket programming tutorials. Thnxs for the basic socket declaration explanation. God Bless.

  2. Gilbert says:

    Awesome tutorial. Even though its obvious that english isn’t ur first language u made an awesome tutorial. Thanks a lot for the basics!

Leave a comment