Yuvraj Sahni March 8 2017 2 Requirement Experience in Python Programming Python for Programmers httpswikipythonorgmoinBeginnersGuideProgrammers Learn Python Online https wwwcodecademycomlearnpython ID: 716487
Download Presentation The PPT/PDF document "COMP2322 Lab 4 Socket Programming" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
COMP2322 Lab 4Socket Programming
Yuvraj
Sahni
March 8, 2017Slide2
2
Requirement
Experience in Python Programming
Python for Programmers
https://wiki.python.org/moin/BeginnersGuide/Programmers
Learn Python Online
https://
www.codecademy.com/learn/python
http://www.learnpython.org
/Slide3
3
Simple Example
Suppose I am interested in knowing the
latitude
and
longitude
of The Hong Kong Polytechnic University
I can enter the following URL in the browser to obtain the information (in JSON format)
http://maps.google.com/maps/api/geocode/json?address=
The Hong Kong Polytechnic
University
&sensor
=
false®ion
=
Hong KongSlide4
4Slide5
5
Client/Server ModelSlide6
6
Client/Server Model
Examples of Client
Google Chrome, Firefox
Examples of Server
Apache, IISSlide7
7
Make a Raw HTTP ConnectionSlide8
8
socket
in Python
Instead of making
an
HTTP request through
httplib
package
We can make the request by using
socket
socket –
support basic network communications on an IP network
Low-level
approach
Raw network communication
A matter of sending and receiving stringsSlide9
9
Make a Request By SocketSlide10
10
Network Sockets
Used to identify particular processes (programs
)
on particular machines
Network Socket is composed of 2 numbers:
IP address – machine identifier
Port number – process identifier
Well-known ports:
25 – SMTP (email), 80 - HTTP (web), 110 – POP3 (email), 443 – HTTPS (secure web)Slide11
11
Network Sockets
Socket()
used in previous example is
NOT the lowest protocol level
The layers below the
socket()
are:
Transmission
Control
Protocol
(TCP)
One alternative to TCP is UDP (we focus on TCP in this lab)
Internet Protocol (IP)
Link layerSlide12
12
“Passive” Listening Socket VS Active “Connected” Socket
Server
The server is a machine that is ready to receive connections
It holds the
address
and the
port number
Passive Listening Socket
It
DOES NOT
represent any actual network
conversation
<= no data can be received / sent
by
this kind of port!
(Q: HOW it works then ???)Slide13
13
“Passive” Listening Socket
vs
Active “Connected” Socket
When the server get the request from the passive listening socket
Server checks with the OS willingness to receive incoming connections
If NO, drop the request
If YES, an
active connected socket
is then created
Bind this socket
to one remote conversation partner
Example: A busy web server,
a
thousand active sockets
bind to
its public IP address at port 80Slide14
14
A Simple TCP Client and ServerSlide15
15
A Simple TCP Client and ServerSlide16
16Slide17
17
Limitations
At the moment, the message must be
fixed
to
16
characters (octets)!
Solution
:
Client side, before sending the message
Determine
the length of the message
L
[Assume max. number of length is
255]
Add
L
at the beginning of the message
Send the new message to server
Server side, after
receiving
the message
Extract the length of the message (first 3 characters)
Read the rest of the message with the proper lengthSlide18
18
Modified TCP Client and ServerSlide19
19
Modified TCP Client and ServerSlide20
20Slide21
21
Lab
Exercise (Total mark
: 55)
[1 / 2]
1. Create 2
Python
files (
10 marks)
Client file - client_
studentID
.py
Server file - server_
studentID.
py
E.g. if my student ID is
12345678d
, name the client file
as
client_
12345678d
.py
2. Provide your full name and student ID
(
5
marks)
At
the beginning of each
Python
program,
by using program comment,
provide the following
information
Your full name
Your student IDSlide22
Lab Exercise (Total mark: 50) [2 / 2]
3. Create a simple instant messaging
system (25
marks)
The messaging
system should
let the client and server send the message in one by one approach
(20 marks) (
more
detail description is shown in
next page
)
Coding - easy to read (5 marks)
if there is any syntax error in your programs (i.e. cannot run), ZERO mark will be given in this
section.
22Slide23
23
Message
in one by one approach
Message in one by one approach IS NOT the same as common messaging platform!!
It works like this:
Server is waiting for connection
Client is connected to Server
Client sends
a message
Server receives
the message
Server sends
a message
Client receives
the message
Go back to
(3
) until Client/Server sends message “
BYEBYE.”Slide24
24Slide25
25Slide26
Remote case conversion
4. Create an application that will convert lowercase letters into upper case. (5 marks)
Client should send your name in lower case letters and it should receive your name back from the server converted in uppercase letters.
Create a different client and server file
26Slide27
27Slide28
5. Create a simple calculator using socket programming (5 marks)
It should implement four operations: Add, Subtract, Multiply, and Divide
Client should send two numbers and type of operation to
a server, and the server should reply with the result of the operation. If the client wants to exit, it should send
"BYEBYE"
to
the server.
Create a different client and server file
28
Remote calculationSlide29
29Slide30
Submission
6.
Submission (5 marks)
Submit two python program for each exercise (3,4, and 5)
to LEARN@POLYU before the submission deadline (
20 March 2017
,
at 5:00pm
)
Name the file as:
client1_
studentID
.py and
server1_
studentID.
py
for exercise 3
client2_
studentID
.py
and
server2_
studentID.
py
for exercise
4
client3_
studentID
.py
and
server3_
studentID.
py
for exercise
5
It is
not required
to zip the files!
30Slide31
31
Reference
J
.
Goerzen
and B. Rhodes,
Foundations of Python Network Programming: The comprehensive guide to building network applications with Python,
Second Edition,
Apress
,
2010.