0% found this document useful (0 votes)
119 views23 pages

Pi On Network: Abhishek Kumar Srivastava Asst. Professor

This notes would tell about accessing pi over network, networking protocols basics and and finally the socket programming in Raspberry pi, where pi can be treated as client or server. This socket programming makes people understand abut various stages of socket programming and exactly how communication takes places between client and server.

Uploaded by

Abhishek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views23 pages

Pi On Network: Abhishek Kumar Srivastava Asst. Professor

This notes would tell about accessing pi over network, networking protocols basics and and finally the socket programming in Raspberry pi, where pi can be treated as client or server. This socket programming makes people understand abut various stages of socket programming and exactly how communication takes places between client and server.

Uploaded by

Abhishek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

Pi on network

Abhishek Kumar Srivastava


Asst. Professor
Introduction

Wifi config from desktop to search wifi network using wifi dongle
in pi

DHCP=Dynamic Host Connection Protocol; gives IP address


dynamically when you plug in device

Firewallblocks traffic coming from certain address or network


application

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

Pi at home and user in office-- can access pi from office using


shell remotely. pi process request using shell (ex: Lxterminal)
and send result to shell running in office or vice versa.

Two machine involved:


1) ssh servermachine you wish to access remotely
2)ssh clientmachine you use to access the server

Telnet is predecessor of secure shell but it is not secure....it
does not encrypt before transmitting data

Must have an account on the server machine since


username/password is necessary to access server from client

running the ssh client:


ssh <username>@<domainname>
username:username on the server machine
Domain name: address of the server machine
ex:raspberrypi

When for 1st time server connects, it gives error; address cannot be
authenticated; when you authenticate, it stores address in file and save for
future.

Once access is granted, it feels you are sitting on your system, gets
command prompt

ssh server is running by default on your pi so just power it up and use

To connect to server, know IP address of server; pi does not have domain


name generally to serve as an address for the client, so IP address of pi must
be known. Instead of domain name, we can use IP address of pi and it still
works.

Pi is connecting to network using DHCP so it can different address every time


it connects; practically, it is same everytime but u may get! How to get IP
address of pi ?
Introduction

Use $ifconfig command on pi to get ip address

eth0wired ethernet hardware jack

lo local network (127.0.0.1): when you connect to this


address, you are connecting back to your own machine

wlan wifi adapter

inet addressinternet address

How to interact network with code ?

we want some program to access to network instead of manual


access.

Internet structure is really adhoc in nature; when laptop is opened and
connected to internet, it acts as node on internet and when laptop is
closed, node is removed, Internet structure is not centrally controlled,
controlled locally; can buy router and connect multiple devices to that
router.....Internet--->routers--->multiple routers---bridges---switches----
>multiple LANS (can have multiple hosts)

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.

Protocol is set of rules defining how data is transferred and what


data is contained in each packet. Ex: over and out protocol in military
radio communication

Every machine in network must implement a common set of protocol to
communicate to each other over the internet.

1st machine from x local area network want to talk to 2nd machine in y local
area network, must adhere to common protocol.

Encapsulates the data as guided by internet protocol

Features of internet protocols:


1) unique name: every node have a unique address
2) message (packet) sent to internet have structure; consist of header and
payload

header: protocol info (ex: destination address, data type, data size, etc.)

payload: contains data to be transmitted



Internet protocol defines by 3 protocols

IP protocol: defines host naming scheme/IP address; host to host connection;


helps to connect one host to another host on network and IP protocol header have
all details which is required to connect between host to host.

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 (Unreliable Datagram Protocol): process to process communication;


every machine might be running multiple process so in UDP, connection between
web client process is done with web server process; if some other process like LAN
game using same network, still it would not be accepted by UDP since LAN game is
a different process and web client is different process even though both are running
on same machine.

IP handles machine to machine. In UDP, each process is named and unreliable



TCP (Transmission contro protocol): process to process communication;
process naming; reliable connection since message is guaranteed
delivered using multiple retries!

We will be using TCP with IP........

TCP and UDP called transport layer protocol

TCP is connection oriented, connections are long lasting; packet sequencing


is supported i.e. while sending many messages from one process to another,
packets are sent in specific ordering and received also in same order; import
for many application; Error detection/correction can be done

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

IP address is divided into 2 parts conceptually:

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)

Ipv6 does some changes to header. Not widely adopted.

Still Ipv4 is more popular

Every host have IP address but TCP/UDP (process to process communication


so each process must be assigned to individual port number) uses port to
address specific application on a machine.

Port numbers are assigned to each network application layer protocol



ex: web traffic uses protocol called HTTP, which is assigned to
port 80 Web browser sends requests on port 80 and server
listens on port 80 for request

port number is 16-bit value in TCP and UDP headers gives


2^16 i.e. 65536 different port addresses which is enough as of
now
Domain name

Each host on internet has a unique IP address ex:


128.2.203.179

IP address are not easy for human to memorize so domain


names. Ex: www.google.com

Each domain name must be resolved or converted into an IP


address that can be included into packet header and
message/packet can be send.

DNS is a hierarchical naming system used to determine IP


address from domain names.

Gigantic distributed tables across globe, are accessed by


performing a DNS lookup

Browser perform a DNS lookup in DNS server to find the IP address of any
domain name and return back with the appropriate address.

If this DNS server knows address it tells the address, if it doesnt it to to


find the address in the higher DNS server and ask for address.

If it knows it send it back, else goes to other higher in hierarch server

Rather than looking up the address everytime when we try to access


any website, IP address are stored in local cache to save time.

Using tool called nslookup on linux box, Ip address of any domain can be
found

ex: abhi@ubuntu:~$ nslookup www.google.com


Server: 127.0.1.1 -------DNS server address for my machine
Address: 127.0.1.1#53 ------- 53 is port address associate with DNS
Non-authoritative answer:
Name: www.google.com
Address: 216.58.220.196
Client-Server model
Flow

client .....request.................>server:<------->resource
...........response....................

Server is guarding a resource ex. Printer or web page content, request coming from client to server

and server listen to request, accepts request, and completes


Sockets:

Client and server communicate over connections, Socket is the endpoint of a connection. If client and
server are connected with connections, there will be two sockets, one for client and other for server and
they both are listening to sockets.
In network code, we need to create socket, open socket, closing socket, etc.

So socket is a programming concept which tells how user will connect on network. Socket is

associated with IP address and port address of individual host machine.


associate an end-
point address with a
socket; called the
listening socket

Need the server's address and


port number

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

In real client ex.web client, we dont close socket


Creating 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()

You might also like