HCI 13. Socket Programming
Uploaded by adrianwang2003 · 28 May 2024
Preview
Socket 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 Comput
Content continues in the PDF.
Related notes
- VJC Chapter 21 SQLite with PythonNotes/Practices · 2025
- VJC Chapter 23 Web Applications PrinciplesNotes/Practices · 2025
- VJC Chapter 10 RecursionNotes/Practices · 2025
- VJC Chapter 20 SQLNotes/Practices · 2025
- VJC Chapter 16 Hash TableNotes/Practices · 2025
- VJC Chapter 22 NoSQLNotes/Practices · 2025

