HCI 13. Socket Programming
Uploaded by adrianwang2003 · 28 May 2024
Preview
Text from the first pagesSocket Programming CPDD Computer Education Unit Version: Oct 2018 1 Name: __________________________ ( ) Class: _________ Date: _________ Lesson: Socket Programming Instructional Objectives: By the end of this task, you should be able to: Define sockets as a general way for programs to communicate with each other State that each end of a socket is an IP address and port number Describe how servers differ from clients in that servers listen for incoming connections while clients initiate the connection Understand the difference between the Python types str and bytes Use str.encode() and bytes.decode() to convert a Unicode string to its UTF-8 encoding and vice versa Use the socket module in Python to send bytes between two Python programs Implement the client code given the server code for a given scenario (e.g., for a tic-tac-toe game) and vice-versa Sharing Data Between Programs Suppose you have two Python programs running at the same time. How would you send data from one prog ram to the other and vice versa? Most operating systems provide a powerful mechanism to do this called sockets. Program BProgram A bytes bytes Socket
Socket Programming CPDD Computer Education Unit Version: Oct 2018 2 You can picture a socket connection as a pipe between two running programs. The pipe is bidirectional and can carry data (represented by bytes) in both directions. There are many kinds of sockets, but the kind that is most often discussed is called an Internet socket. Internally, Internet sockets deliver data using the same Transmission Control Protocol and Internet Protocol suite (commonly abbreviated as TCP/IP) that is used to transmit data over the Internet. This means that Internet sockets can deliver data between any two programs, even programs that that are running on different computers, as long as the two computers can access each other over the network. For simplicity, we illustrate an Internet socket as a pipe that is only attached to the two computers. In reality, however, data that is transmitted through an Internet socket may pass through multiple devices before reaching its destination. You should be aware that any of these devices can steal or modify the data that passes through a socket unless you encrypt the data first . A more accurate illustration of a socket that shows how the data passes through multiple devices is shown below: As networks can become congested , we cannot assume that data sent over Internet sockets will be transmitted instantaneously. For instance, a program may receive only the first half of a message before the second half arrives some time late r. To avoid working with incomplete data, we will need to define a protocol (explained later) so that the start and end of messages can be detected unambiguously. Program BProgram A Computer X Computer Y bytes bytes Internet socket Program BProgram A Computer X Computer Y bytes bytes Intermediary Device Intermediary Device bytes bytes bytes bytes
Socket Programming CPDD Computer Education Unit Version: Oct 2018 3 1 Which of the following methods for sending data from one Python program to another does NOT work? A One program copies the data onto the system clipboard and the other program reads it from the system clipboard B One program writes the data into a file (that is readable to everyone) and the other program reads it from the same file C One programs assigns the data to a Python variable and the other program reads it from the same variable D The two programs set up at socket connection and transmit the data from one program to the other through the socket ( C ) IP Addresses and Ports Each end of a socket is associated with a running program and is uniquely identified by a combined IP address and port number. The IP address identifies which device that end of the socket is attached to and the port number identifies which program on that device is using the socket. There are two kinds of IP addresses in use today: IPv4 addresses and IPv6 addresses. IPv4 address es have 32 bits and are usually presented as 4 denary numbers separated by dots. Each denary number can range from 0 to 255 (inclusive) and corresponds to one byte (8 bits) of the IP address. Program BProgram A IP address: 192.168.1.20 IP address: 192.168.1.17 Port 4321 Port 1234
Socket Programming CPDD Computer Education Unit Version: Oct 2018 4 Some IPv4 addresses are reserved for special use and have specific meanings. Two important special IPv4 addresses are: 127.0.0.1 Refers to the local computer 0.0.0.0 Refers to all IP addresses for local computer IPv6 addresses, on the other hand, have 128 bits and are usually presented as 8 groups of 4 hexadecimal digits separated by colons. For compactness, leading zeros and up to one consecutive sequence of zero-only groups may be omitted. Currently, IPv4 addresses are more frequently encountered than IPv6 addresses, so to simplify our discussion, we will be working with IPv4 addresses only. On each device, port numbers are used to distinguish between attached sockets. The device also keeps track of which program is associated with each port and which port numbers are still available for use by new sockets. Port numbers can range from 0 to 65,535. However, the first 1,024 port numbers are reserved for specific kinds of programs and should not be used for other purposes. For instance, port 80 and port 443 are reserved for use by web server programs. On a Windows computer with access to PowerShell or Command Prompt, you can run the com mand netstat -n to list out the sockets that are currently open on your computer. Each socket will be displayed with a combined IP address and port number for each of its ends (i.e., its local and foreign addresses): Program BProgram A Port 443 Port 50203 172.16.184.131 204.79.197.229
Socket Programming CPDD Computer Education Unit Version: Oct 2018 5 To reveal which program is using each socket, you can run netstat -no to reveal the process ID (PID) associated with each socket. You can then open Task Manager and match each PID to the name of a running program: 2 Which of the following statements is true? A We can uniquely identify a program and which machine it is running on using only an IPv4 address. B We can uniquely identify a program and which machine it is running on using only an IPv4 or IPv6 address. C We can uniquely identify a program and which machine it is running on using only a port number. D We can uniquely identify a program and which machine it is running on using only an IPv4 or IPV6 address and a port number. ( D ) Creating a Socket Connection Creating a socket connection is a multi-step process that requires one program to be the server and another program to be the client. The server's IP address and port number for accepting connections must also be known ahead of time by the client. First, the server creates a passive socket, binds it to the pre-chosen port number and listens for an incoming connection . (A passive socke t is not connected and merely waits for an incoming connection.)
Socket Programming CPDD Computer Education Unit Version: Oct 2018 6 Next, the client initiates a connection request using the server's IP address and port number. If no server is listening on the chosen port, the connection will be refused. On the other hand, if the connection request reaches an IP address and port number that a server is listening on, the server accepts and creates a new socket for the requesting client using a dynamically assigned port number. The passive socket goes back to listenin g for new connections while the client and server can now exchange data using the newly-created socket
Content continues in the PDF. Download PDF
Related notes
- NYJC 2026 Prelim P2Exam Papers · 2026
- NYJC 2026 Prelim P1Exam Papers · 2026
- DHS 2026 Y6 H2 Computing Prelim Paper 2_finalExam Papers · 2026
- ACJC 2026 JC2 Computing Prelim Paper 2 (Practical)Exam Papers · 2026
- 2026_NJC Prelim_Computing_P2.pdfExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P2_finalExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P1_markschemeExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P1_finalExam Papers · 2026
- 2026 ACJC Prelim Computing Paper 2Exam Papers · 2026
- 2024 ACJC Computing PromoExam Papers · 2024
- 2023 ACJC Promo QPExam Papers · 2023
- 2022 ACJC Computing Promo Paper 2Exam Papers · 2022
- See all H2 Computing notes

