Categories
Latest
Popular

Socket Programming using Python #3

 

Python

Socket Programming

Tutorial #3

 

Hi guys!!!! today were going to start doing some the most basic and most important parts of network programming .

How to send data???

To send data over networks using TCP protocol we use send(string[,flags]), this sends the given string o bytes to the remote socket. While To do the same using UDP protocol we use the sendto(string[,flags],address) it sends the string of bytes to a particular address. But if you have used the connect() function to associate it with a particular destination then you can call send() instead of sendto(). Both send() and sendto() return the number of bytes that has been transmitted to the destination.

When you send large amounts of data , you may want to ensure that the entire thing has reached the destination. To ensure this you can use a function like this .

     Source Side

   

     Destination side

    

How to get data????

To access incoming data we use the recv(bufsize[,flags]) function, it returns the buffer size which the program has to read from the buffer. recvfrom(data,address) can also be used to do the same , but the only difference between them is the return type. It returns the address from which the data has originated(this is useful for connectionless socket).

 

All the sending (send(),sendto()) and receiving (recv(),recvfrom()) method take an optional flags parameter that defaults to 0. The values available vary from platform to platform, but some of the most common are :

                   Flag values for send() and recv()

Flags

Description

MSG_OOB

Process out-of-band data

MSG_DONTROUTE

Don’t use routing tables; send directly to the interface

MSG_PEEK

Return the waiting data without removing from queue

 

 

 

 

USING SOCKET OPTIONS

To attain the 2-tuple containing an IP address and a port (just as you’d pass to connect or bind) you can use the getsockname() and getpeername() methods. getpeername() returns the address of the remote socket to which you are connected and getsockname()  returns the address of the local socket .

 

Now there are two types of socket blocking and non-blocking type. by default all sockets are blocking type , which means that socket method calls don’t return until the action is completed. for example the send() method , this will wait until it can put more data in the buffer. You can change the behavior of the socket to a non-blocking type one ,by calling the setblocking(flag) method with a value 0.  When socket is non-blocking ,it will raise an error if the requested action would cause it to block .

 

Here you can see that both accept() and send() are causing error’s as the method is tending to block the system. This happens because of the request of these methods are not being completed instantly . i.e in accept() due to the absence of incoming request and in send() due to absence of recv() method on the destination side.

This behavior of the socket can be used to create a server.

 

This is an simple example of a server , this server waits until a connection is made between system’s. If it was a real world server you could have closed the server by changing the value of loop to 0. We will design more complex server’s in the upcoming tutorial’s.

Other socket options can be set and retrieved with the setsockopt(level , name ,value) and getsockopt(level ,name [,buflen]) methods .socket represent several layers of protocol stack and the level parameter specifies at what level the option should be applied. The values of level start with a prefix of SOL_ (SOL_TCP,SOL_SOCKET and so on) . The name of the option identifies exactly which option you’re talking about, and the socket module defines whatever options are available on your platform.

 

Option

Value

Description

Option for SOL_SOCKET

SOL_TYPE

(get only)

socket type(SOCK_STREAM)

SOL_ERROR

(get only)

socket last error

SOL_LINGER

Boolean

Linger on close if data present

SOL_RCVBUF

Number

input(recive) buffer size

SOL_SNDBUF

Number

output(send) buffer size

SOL_RCVTIMEO

Time struct

input(recieve) timeout delay

SOL_SNDTIMEO

Time struct

output(send) timeout delay

SOL_REUSEADDR

Boolean

enable multiple users of a local address/port

Option for SOL_TCP

SOL_NODELAY

Boolean

send data immediately instead of waiting for minimum send amount

Option for SOL

IP_TTL

0-255

maximum number of hops a packet can travel

IP_MULTICAST_TTL

0-255

maximum number of hops a packet can travel

IP_MULTICAST_IF

INET_ATON(IP)

Select interface over which to transmit

IP_MULTICAST_LOOP

Boolean

enable sender to recieve a copy of multicast packets it sends out

IP_ADD_MEMBERSHIP

ip_mreq

join a multicast group

IP_DROP_MEMBERSHIP

ip_mreq

leave a multicast group

  

With this we wrap up this session , next time we will be learning about Socket Servers.