Experiment 6
Experiment 6
SOCKET
PROGRAMMING
EXPERIMENT-6
CVM University IOT Minor Degree Programme 12 August 2022
3 WHAT IS SOCKETS?
• Domain : Family of protocols which are used as transport mechanism. These values are
constants such as AF_INET, PF_INET, PF_UNIX, PF_X25, and so on.
• Type: Type of communications between the two endpoints, typically SOCK_STREAM for
connection-oriented protocols and SOCK_DGRAM for connectionless protocols
• Protocol: Typically, zero. Used to identify a variant of a protocol within a domain and type.
• Hostname: The identifier of a network interface −
• A string, which can be a host name, a dotted-quad address, or an IPV6 address in colon (and
possibly dot) notation
• . Port: Each server listens for clients calling on one or more ports. A port may be a Fixnum port
number, a string containing a port number, or the name of a service.
CVM University IOT Minor Degree Programme 12 August 2022
6
SERVER SOCKET METHODS GENERAL SOCKET METHODS
• s.recv(): Receives TCP message
• s.bind() : binds address (hostname,
port number pair) to socket. • s.send() : Transmits TCP message
• s.listen() : sets up and start TCP
• s.recvfrom() : Receives UDP message
listener
• s.accept() : accept TCP client • s.sendto() : Transmits UDP message
connection, wait until connection
• s.close(): closes socket
arrives (blocking).
Client Socket Methods • socket.gethostname(): Returns the
• s.connect() : Actively initiates TCP
hostname
server connection.
CVM University IOT Minor Degree Programme 12 August 2022
7 A SIMPLE SERVER
• host = socket.gethostname() # Get local machine • print 'Got connection from', addr
name • c.send('Thank you for connecting')
• port = 12345 # Reserve a port for your service. • c.close() # Close the connection
• s.bind((host, port)) # Bind to the port
• s.listen(5) # Now wait for client
connection.
CVM University IOT Minor Degree Programme 12 August 2022
8 A SIMPLE CLIENT