Download presentation
Presentation is loading. Please wait.
Published byGyles Ryan Modified over 9 years ago
1
COMP2322 Lab 4 Socket Programming Toby Lam March 2, 2016
2
2 Requirement Experience in Python Programming Python for Programmers https://wiki.python.org/moin/BeginnersGuide/Pr ogrammers https://wiki.python.org/moin/BeginnersGuide/Pr ogrammers Learn Python Online https://www.codecademy.com/learn/python https://www.codecademy.com/learn/python http://www.learnpython.org/ http://www.learnpython.org/
3
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?add ress=The Hong Kong Polytechnic University&sensor=false®ion=Hong Kong
4
4
5
5 Client/Server Model
6
6 Examples of Client Google Chrome, Firefox Examples of Server Apache, IIS
7
7 Make a Raw HTTP Connection
8
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 strings
9
9 Make a Request By Socket
10
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)
11
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 layer
12
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 ???)
13
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 This socket is bounded to one remote conversation partner Example: A busy web server, there is a thousand active sockets all bound to its public IP address at port 80
14
14 A Simple TCP Client and Server
15
15 A Simple TCP Client and Server
16
16
17
17 Limitation At the moment, the message must be fixed in 16 characters (octets)! Solution: Client side, before sending the message 1)Determine the length of the message L [Assume max. number of length is 255] – Add L at the beginning of the message 2) Send the new message to server Server side, after received the message 1)Extract the length of the message (first 3 characters) 2)Read the rest of the message with the proper length
18
18 Modified TCP Client and Server
19
19 Modified TCP Client and Server
20
20
21
21 Lab Exercise (Total mark: 50) [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 (10 marks) – At the beginning of each python program, by using program comment, provide the following information Your full name Your student ID
22
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 4. Submission (5 marks) – Submit two python programs to LEARN@POLYU before the submission deadline (Monday, 14 March 2016, at noon) It is not required to zip the files! 22
23
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: 1. Server is waiting for connection 2. Client is connected to Server 3. Client sends message 4. Server receives message 5. Server sends message 6. Client receives message 7. Go back to (3) until Client/Server sends message “BYEBYE”
24
24
25
25
26
26 Reference J. Goerzen and B. Rhodes, Foundations of Python Network Programming: The comprehensive guide to building network applications with Python, Second Edition, Apress, 2010
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.