You are on page 1of 5

LABORATORY #3

IT Systems Security Lab


University of Petroleum and Energy Studies, Dehradun
Lab : How to write Make-files and sockets.
Date (Assign) : 14/02/2019 Marks : 10
Last Date (Checking) : 21/02/2019 Last Date (Late Checking) : 28/02/2019
Date of Solution to be posted : Total Time : 2hrs.
Week : Second (14 Feb. to 21 Feb)
_______________________________________________________________________________________________
INSTRUCTIONS:
1. Unprofessional conduct, such as an abuse of USF computer privileges (unauthorized access), or a Violation of academic integrity
(plagiarism or fraud), will result in the student receiving a failing grade.
2. Work in singles.
3. Late submission will be evaluated from half of total marks.
5. Platform:
Python Either on window platform or Linux platform
6. 7. You can eitherwork on Linux or Unix/Window environment
__________________________________________________________________________________________________________________

PART I: NETWORK PROGRAMMING


……………………………………………………
Tutorials:
1. Internet Protocol version 4 is of 32 bits (4 blocks each of 8 bits) and assigned in decimal format.
2. Internet Protocol Version 6 is of 128 bits (8 blocks each of 16 bits) and assigned in hexadecimal format.
3. TCP stands for Transmission Control Protocol , SCTP stands for Stream Control Transmission
Protocol and UDP stands for User Datagram Protocol.
4. TCP is connection-oriented protocol and UDP is connectionless protocol.
5. Difference between connection oriented and connectionless is : in connection oriented before
transmitting data there is need to establish a connection between two ends (source and destination). In
connectionless uses passive approach i.e. when there is need to transfer data then there is establishment
of path.
6. Coming back to Socket function , syntax of socket function is:
 Int Socket (int family, int type, int protocol)
There are three parameters in socket function: family (address and protocol family), type (mainly four
type: stream sockets (TCP or SCTP), datagram sockets, sequenced packet sockets and raw sockets),
protocols (like: TCP, UDP, SCTP etc.)
Family Description
AF_INET IPv4 protocols
AF_INET6 IPv6 protocols
AF_LOCAL Unix domain protocols
AF_ROUTE Routing Sockets
AF_KEY Key Sockets
Table #1: - Protocol family constants for socket function.
Type Description
SOCK_STREAM Stream Socket
SOCK_DGRM Datagram Socket
SOCK_SEQPACKET Sequenced Packet Socket
SOCK_RAW Raw Socket
Table #2: - Type of socket for socket function.
Protocol Description
IPPROTO_TCP TCP transport protocol
IPPROTO_UDP UDP transport protocol
IPPROTO_SCTP SCTP transport protocol
Table #3: - Combination of family and type for the socket function.

AF_INET AF_INET6 AF_LOCAL AF_ROUTE AF_KEY


SOCK_STREAM TCP | SCTP TCP | SCTP Yes
SOCK_DGRM UDP UDP Yes
SOCK_SEQPACKET SCTP SCTP Yes
SOCK_RAW IPv4 IPv6 Yes Yes
Table #4: - Combination of family and type for the socket function.
13 Function flows for Transmission Control Protocol at Client and Server sides:
TCP Client TCP Server
Socket ( ) Socket ( )
Connect ( ) bind ( )
Write ( ) listen ( )
Read ( ) accept ( )
Close ( ) Read ( )
Write ( )
Close ( )
14 Function flows for Transmission Control Protocol at Client and Server sides:
UDP Client UDP Server
Socket() socket()
sendto() bind()
recvfrom() recvfrom()
close() sendto()
close()
----------------------------------------------------------------------------------------------------------------------------------------------
Problem #1.1:Write an Echo Server and Client program for Message sending.
----------------------------------------------------------------------------------------------------------------------------------------------
Solution:
Python -------------------------------------------------------------------------------------------------------------------------------------

(i) Server open a passive open connection


(ii) Client opens an active open connection and sends a request to server for connection establishment.
(iii) Once a connection is established, client side will send message to server side.
(iv) Server will receive the message and echo back the same message to client side.
(v) Client will receive the message and terminate the connection.
(vi) After receiving request from client side, server will also terminate its connection.
Server.py Client.py

from socket import * from socket import *


myHost='' import sys
myPort=50007 serverHost='localhost'
sock=socket() serverPort=50007
sock.bind((myHost,myPort))
sock.listen(5) sock=socket()
while True: sock.connect((serverHost,serverPort))
conn,address=sock.accept()
print('Server Connected by',address) sock.send(b"Hello")
while True: data=sock.recv(1024)
data=conn.recv(1024) print('Client received:',repr(data))
if not data:
break sock.close()
conn.send(b'Echo=>'+data)
conn.close()
sock.close()
_______________________________________________________________________________________________
Question #1[5 Marks][Python/Window][Network Programming]: Modify the above code for following output:
Prompt#1>python Server.py
Prompt#2>python Client.py
Prompt#1>Network ProgrammingNetwork ProgrammingNetwork ProgrammingNetwork ProgrammingNetwork Programming
Network ProgrammingNetwork ProgrammingNetwork ProgrammingNetwork ProgrammingNetwork Programming
Network ProgrammingNetwork ProgrammingNetwork ProgrammingNetwork ProgrammingNetwork Programming

Question #2[5 Marks][Python/Window] [Network Programming]:- Design and implement a client/server


arrangement for text based chat-application. Server will start the process and multiple clients can connect to this
server. When connection is established between client and server, A Menu with following details will be displayed on
all client machines:.

1. Get Local Hostname


2. Get Local Internet Protocol Address
3. Get Server Hostname
4. Get Server IP Address
5. Get Service Name running at local or remote machine?
6. Get port numbers, of any service running on local or remote machine?
7. exit
Enter your choice: 1
Enter IP Address: 192.14.45.23
Hostname is : machine_name.ac.in
Enter your choice:2
Enter Hostname:localhost
IP Address is: 127.0.0.1
Enter your choice: 3
Enter IP Address: 192.14.45.24
Hostname is: server_machine_name.ac.in
Enter your choice: 4
Enter hostname: server_machine_name
IP Address is: 192.14.45.24
Enter your choice: 5
Enter port number: 21
Enter protocol TCP
Service Name is: ftp
Enter your choice:6
Enter Service Name: ftp
Enter Protocol: TCP
Port Number is: 21
Enter your choice: 7
Disconnecting with server……….

Question #3:[5 Marks][Python/Window][Network Programming]: Write a program to check whether a machine is


little endian machine or Big endian machine.(help: import sys package)

Question #4:[5 Marks][Python/Window][Network Programming]: Write a program to convert Host byte order to
network byte order and vice versa. (Without use of struct.pack( ), socket.htonl( ),htons( ) or any in-build function)

Menu:
1. Convert Host byte order to Network byte order.
2. Convert Network byte order to Host byte order.
3. Exit.
Enter your choice: 1
Enter data in host byte order:123
Network byte order is: '\x01\x00\x02\x00\x03\x00\x00\x00'
Enter your choice:
_______________________________________________________________________________________________

You might also like