Socket function in Python

Function Name:

socket.socket(family=AF_INET, type= SOCK_STREAM, proto=0, fileno=None)

Overview:

The socket function returns a new socket object. A socket is an end point of communication. A socket returned by this method can be made either a server socket or a client socket by further customizing it.

 

For example, regardless of whether a socket was created with option SOCK_STREAM or SOCK_DGRAM the socket can be made as a server by calling bind() and accept() methods or bind() and recvfrom() methods. The first sequence is required for a TCP server and the later is required for an UDP server.

 

In case of TCP clients they can connect with TCP servers using connect() method. In case of UDP clients Data transmission from client to server or server to client is done by the methods send(), recv(), sendall(), sendto(), recvfrom() methods.

Parameters:

family – The valid values for addressFamily are AF_INET,  

         AF_INET6, AF_UNIX, AF_CAN, AF_PACKET and AF_RDS. The   

                   meaning of these address families and the scenarios under  

                   which they are used are given below:

 

AF_INET

For protocols based on IP addresses using IPv4. An IPv4 address consists of four numbers each ranging from 0 to 255 and each of them separated by a dot. IPv4 is a 32-bit number and supports up to 2 to the power 32 IP addresses.

AF_INET6

For protocols using IPv6 addresses.

IPv6 addresses solve the limitation of number of IP addresses in IPv4 as they are represented as a 128-bit number.

AF_UNIX

Used for Unix domain protocols.

AF_CAN

Very shortly, various controllers and applications in automotive vehicles can talk using CANSockets and using the protocol family PF_CAN.AF_CAN is used while creating CANSockets.

AF_PACKET Allows to create a packet socket. Packet sockets are used while implementing new higher level protocols.

AF_RDS

Used for reliable datagram sockets.

 

 

type:  The valid values for socketType are socket.SOCK_STREAM,      

  socket.SOCK_DGRAM, socket.SOCK_RAW,   

     socket.SOCK_RDM and socket.SOCK_SEQPACKET.

     The meaning of these socket types and when to use which type 

     are given below:

 

socket.SOCK_STREAM

Specifies a stream socket. Needs to be provided when a streaming connection based client or server is needed which will be used for reliable communication

socket.SOCK_DGRAM

Specifies an UDP socket.  This option needs to be provided while creating a datagram socket, which will be used in datagram based unreliable communication.

socket.SOCK_RAW

Raw sockets allow customizing the TCP and IP layer headers of the packet. The kernel will not modify the packets from a raw socket.

socket.SOCK_RDM

Used in reliable datagram based communication though same ordering of packets are not guaranteed.

socket.SOCK_SEQPACKET

Used for reliable, sequence ordered datagram based communication.

 

             

proto: The default value is zero. It is typically omitted and used in cases like

                dealing with raw sockets as per the address family specified. If AF_CAN is specified as address family then either CAN_RAW or CAN_BCM can be passed.

 

Fileno: File descriptor of a socket. When this parameter is specified, the other

                parameters are ignored and a socket object as given by the File

                descriptor is returned. The socket object returned is not a duplicate of

                the original socket. 


Copyright 2024 © pythontic.com