Pi On Network: Abhishek Kumar Srivastava Asst. Professor
Pi On Network: Abhishek Kumar Srivastava Asst. Professor
Wifi config from desktop to search wifi network using wifi dongle
in pi
Allow web traffic but not allow any game; need internet
connection; to unblock need to call administration
Secure shell (ssh): allows to access machine remotely; shell
accepts your command and interprets your command ex: ls or
cd commands are taken by shell and shell print out the result;
two programs talking to each other across network
Once access is granted, it feels you are sitting on your system, gets
command prompt
All these random LAN talk together through set of internet protocols.
There is no control on individual network but they still need to
communicate somehow using Internet protocol.
1st machine from x local area network want to talk to 2nd machine in y local
area network, must adhere to common protocol.
header: protocol info (ex: destination address, data type, data size, etc.)
Unreliable protocol since no guarantee that data would reach the destination;
if may fail for many reasons (ex: data may be dropped by intermediate
machine, or machine crashes, etc. )...
UDP connects for short interval of time; no long data transmission is done
here; no packet sequencing, unreliable since data may be lost during
transmission, simple and fast than TCP since no sequencing or error
detection/correction done.
IP address is a unique number for all machines on network.
Ipv4 uses 32-bit number; 4 number, each number is 8 bit each, separated by
period ex: 192.0.0.0
Network addresscommon to all machine inside your local network (High byte
(1st byte) of IP address common across an entire LAN/WAN)
Host address---- Low byte of IP address which is unique to every host in network
Ipv6
Ipv4 problem is it is 32-bit long address whereas Ipv6 uses 128 bit address
(much longer address)
IPV4 only supports only 2^32 address which is limited. Internet will exceed this
number easily these limits
This is actually not enough address since every device have address these
days so as a solution we got IPv6 which supports 2^128 address which is
enough bigger.
Ipv6 also integrates security (does encryption using IPSec protocol which
allow encryption of data in transit just like ssh)
Using tool called nslookup on linux box, Ip address of any domain can be
found
client .....request.................>server:<------->resource
...........response....................
Server is guarding a resource ex. Printer or web page content, request coming from client to server
So socket is a programming concept which tells how user will connect on network. Socket is
http://phobos.ramapo.edu/~vmiller/AdvancedUnix/berkeley_socket.htm
Socket interface:
Can be used in many languages, C, C++, python. We use python
Socket on the client
creating generic network client
1. Create a socket on local client
2. Connect the socket to servers socket
3. Send some data (request) after connection established
4. wair for response; receive some data (response)
5. after reception, close socket
import socket
mysocket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
###creating socket function socket.socket() creates socket , AF_INET declares Address
Family Internet because we are talking on internet, SOCK_STREAM indicates we are
using TCP, connection based communication instead of UDP (connection less)
host=socket.gethostbyname(www.google.com)
### returns the IP address
####gethostbyname() peforms DNS lookup
mysocket.connect(host,80)
###mysocket.connect() connect mysock socket at client side to the listening socket on
server side at host IP address (of google.com) and port number 80 (web traffic).
Message = GET / HTTP/1.1\r\n\r\n ..........HTTP GET request asking for web page
contents of google.com
mysock.sendall(message)
###sendall() send the data to server; tries until it succedd
data = mysock.recv(1000)
###recv () function argument is the max byte to received in buffer; useful in C but not in
python; blocking method (wait until data is received)
print(data)
mysock.close() .........free port so that any another process can use the same port
Sockets on server : it listens or wait for request,
1. Create a socket
2. Bind the socket to an IP address and port
3. Listen for a connection
4. Accept the connection with client
5. Receive the request
6. Process the request and send the response
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.bind(,1234)
####bind() the socket to a IP address and port from which client request may come.
####First argument is empty so that it can receive request from any host since server is
still waiting for any host to connect; allows to receive from any host (client)
mysock.listen(5) ...............server start listening to establish a connection
conn,addr=mysock.accept() ..............accepting the call to connect from
client; addr contains client IP address
###listen() means waiting until some client calls connect(), trying to connect
socket
###listen(5) ---- 5 tells about backlog (number of clients allowed to wait for
server to get serviced); in single threaded code, only 1 service request is
handled at a time but how many can be waiting in queue until server finish
serving and they get served? Here, the count is 5
###conn argument is the connection which is used to receive data on
and send request back.
Full Server Program
import socket
import sys
mysock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
mysock.bind(,1234)
except socket.error: Use netcat (nc) program to use it as client or server
print (Failed to bind) nc -l 1234 ----------act as server which listen on port 1234
sys.exit() client will connect to its loopback address so that machine
can talk to itself
mysock.listen(5)
while True:
conn,addr=mysock.accept()
data=conn.recv(1000)
if not data:
break
conn.sendall(data)
conn.close()
mysock.close()