Socket programming using python [UDP]

What is Sockets?
Sockets are the endpoints of a bidirectional communications channel. Sockets may communicate within a process, between processes on the same machine, or between processes on different continents.
Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP, UDP, and so on. The socket library provides specific classes for handling the common transports as well as a generic interface for handling the rest.
Sockets have their own vocabulary −
Sr.No. Term & Description
1 Domain
The family of protocols that is used as the transport mechanism. These values are constants such as AF_INET, PF_INET, PF_UNIX, PF_X25, and so on.
2 type
The type of communications between the two endpoints, typically SOCK_STREAM for connection-oriented protocols and SOCK_DGRAM for connectionless protocols.
3 protocol
Typically zero, this may be used to identify a variant of a protocol within a domain and type.
4 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
A string “”, which specifies an INADDR_BROADCAST address.
A zero-length string, which specifies INADDR_ANY, or
An Integer, interpreted as a binary address in host byte order.
5 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.
The socket Module
To create a socket, you must use the socket.socket() function available in socket module, which has the general syntax −
s = socket.socket (socket_family, socket_type, protocol=0)
Here is the description of the parameters −
socket_family − This is either AF_UNIX or AF_INET, as explained earlier.
socket_type − This is either SOCK_STREAM or SOCK_DGRAM.
protocol − This is usually left out, defaulting to 0.
Once you have socket object, then you can use required functions to create your client or server program. Following is the list of functions required −
Server Socket Methods
Sr.No. Method & Description
1 s.bind()
This method binds address (hostname, port number pair) to socket.
2 s.listen()
This method sets up and start TCP listener.
3 s.accept()
This passively accept TCP client connection, waiting until connection arrives (blocking).
Client Socket Methods
Sr.No. Method & Description
1 s.connect()
This method actively initiates TCP server connection.
General Socket Methods
Sr.No. Method & Description
1 s.recv()
This method receives TCP message
2 s.send()
This method transmits TCP message
3 s.recvfrom()
This method receives UDP message
4 s.sendto()
This method transmits UDP message
5 s.close()
This method closes socket
6 socket.gethostname()
Returns the hostname.
A Simple Server
To write Internet servers, we use the socket function available in socket module to create a socket object. A socket object is then used to call other functions to setup a socket server.
Now call bind(hostname, port) function to specify a port for your service on the given host.
Next, call the accept method of the returned object. This method waits until a client connects to the port you specified, and then returns a connection object that represents the connection to that client.

!/usr/bin/python

# This is server.py file

import socket # Import socket module

s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port

s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept() # Establish connection with client.
print ‘Got connection from’, addr
c.send(‘Thank you for connecting’)
c.close() # Close the connection
A Simple Client
Let us write a very simple client program which opens a connection to a given port 12345 and given host. This is very simple to create a socket client using Python’s socket module function.
The socket.connect(hosname, port ) opens a TCP connection to hostnameon the port. Once you have a socket open, you can read from it like any IO object. When done, remember to close it, as you would close a file.
The following code is a very simple client that connects to a given host and port, reads any available data from the socket, and then exits −

!/usr/bin/python

# This is client.py file

import socket # Import socket module

s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.

s.connect((host, port))
print s.recv(1024)
s.close() # Close the socket when done
Now run this server.py in background and then run above client.py to see the result.

Following would start a server in background.

$ python server.py &

Once server is started run client as follows:

$ python client.py
This would produce following result −
Got connection from (‘127.0.0.1’, 48437)
Thank you for connecting

VLAN Configuration Using Cisco Packet Tracer

Creating a simple topology using packet tracer

Creating VLAN

Initial topology for the practice of VLAN, VTP, DTP and Router on Stick

SWITCH1

S1(config)#vlan 10
S1(config-vlan)#exit
S1(config)#vlan 20
S1(config-vlan)#exit
S1(config)#

Assigning VLAN Membership

switch1

S1(config)#interface fastEthernet 0/1
S1(config-if)#switchport access vlan 10
S1(config-if)#interface fastEthernet 0/2
S1(config-if)#switchport access vlan 20

switch2

S2(config)#interface fastEthernet 0/1
S2(config-if)#switchport access vlan 10
S2(config-if)#interface fastEthernet 0/2
S2(config-if)#switchport access vlan 20

switch3

S3(config)#interface fastEthernet 0/1
S3(config-if)#switchport access vlan 10
S3(config-if)#interface fastEthernet 0/2
S3(config-if)#switchport access vlan 20

Testing VLAN configuration

1] Access PC’s command prompt to test VLAN configuration. Double click PC-PT and click Command Prompt

A] VLAN10

VLAN Test

B] VLAN20

Test VLAN

Configure Router

Access command prompt of Router

To configure Router on Stick we have to access CLI prompt of Router. Click Router and Click CLI from menu items and Press Enter key to access the CLI

Run following commands in same sequence to configure Router

Router>enable
Router#configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
Router(config)#interface fastEthernet 0/0
Router(config-if)#no ip address
Router(config-if)#no shutdown
Router(config-if)#exit
Router(config)#interface fastEthernet 0/0.10
Router(config-subif)#encapsulation dot1Q 10
Router(config-subif)#ip address 10.0.0.1 255.0.0.0
Router(config-subif)#exit
Router(config)#interface fastEthernet 0/0.20
Router(config-subif)#encapsulation dot1Q 20
Router(config-subif)#ip address 20.0.0.1 255.0.0.0
Router(config-subif)#exit

 

Network Devices

Cisco devices and Packet Tracer devices

Selecting Switches or Routers from the device-type selection box lists both Cisco devices and some devices labeled Generic. These are custom Packet Tracer devices running on Cisco IOS, but the slots that hold the modules are different.

Routers

A router provides connectivity between two logical networks. Every router in Packet Tracer can be switched on or off by using the provided power button.

The power switch is required to make a device simulate its real counterpart. Modules can be added or removed only after powering off the device. If the running configuration is not saved, power cycling a device will make it lose its configuration.

The following routers are available in Packet Tracer:

  • Cisco 1841: This is an Integrated Service Router (ISR) having two Fast Ethernet ports, two slots for High Speed WAN Interface Cards (HWICs), and one slot for Advanced Integration Module (AIM)
  • Cisco 1941: This is similar to the previous model but runs on Cisco IOS Version 15. It has two ports that operate at Gigabit Ethernet speeds.
  • Cisco 2620XM: This is a multiservice router with one Fast Ethernet port, two slots for WAN Interface cards, and one slot for AIM.
  • Cisco 2621XM: This is similar to the previous model, except that this router has two Fast Ethernet ports.
  • Cisco 2811: This ISR comes with two Fast Ethernet ports, four WIC slots, and a dual slot for AIM.
  • Cisco 2901: This router has two Gigabit Ethernet ports, four WIC slots, and two Digital Signal Processor (DSP) slots. This router uses Cisco  IOS Version 15.
  • Cisco 2911: This router has three Gigabit Ethernet ports and all the other features of the previous router. It runs on IOS Version 15.
  • Generic Router-PT: This is a custom router running on Cisco IOS. It contains 10 slots and has separate modules with a naming  convention beginning with PT.

    Switches

    A switch, also called a multiport bridge, connects more than two end devices together. Each switch port is a collision domain. The following switches are  available in Packet Tracer:

    • Cisco 2950-24: This managed switch comes with 24 Fast Ethernet ports.
    • Cisco 2950T-24: This switch is a member of the Catalyst 2590 Intelligent Switch family and has two Gigabit Ethernet ports in addition to the 24 Fast Ethernet ports.
    • Cisco 2960-24TT: This is another 24 port switch; the previous switch has Gigabit Interface Converter (GBIC) for Gigabit Ethernet ports,  whereas this switch has Small Form-factor Pluggable (SFP) modules  for the same. Note that this is a difference only on real switches,  it has no impact on Packet Tracer.
    • Cisco 3560-24PS: This switch is different from the others because it is a layer 3 switch that can be used to perform routing in addition to switching. The PS suffix implies support for Power over Ethernet (PoE), which can be used to power up IP phones without using power adapters.
    • Bridge PT: This is a device used to segment a network and it has only two ports (which is why it is a bridge; if it had more, it’d be called a switch).
    • Generic Switch PT: This is a Packet-Tracer-designed switch running on Cisco IOS. This is the only customizable switch with 10 slots and several modules.