Client-Server Architecture
A socket is a two-way connection between two programs that allows both reading and writing of data, similar to a file but connecting to a remote program rather than local storage.
When Communication Waits
Two programs can communicate through a socket. The socket gives both programs a way to read data and write data, but reading is not always an immediate operation. If a program tries to read when the other program has not sent data, the reading program blocks and waits. Understanding that waiting behavior is central to understanding client-server architecture.
What do you think happens?
A client and a server are connected, but neither has sent data. What can happen if both programs try to read?
Reveal answer
Answer: Both programs can wait for the other to send data.
Reading from a socket when no data is available causes the program to block. If both programs do this at the same time, neither sends the data that would allow the other to continue. This situation is a communication deadlock.
Sockets Between Programs
A socket is a two-way connection between two programs that allows both reading and writing of data. It is similar to a file because a program can read from it and write to it, but it connects to a remote program rather than to local storage.
| Connection | What the program can do | What it connects to |
|---|---|---|
| Socket | Read and write data | A remote program |
| File | Read and write data | Local storage |
The Waiting State
A read from a socket depends on the other program. When data is available, the program can read it. When no data is available, the program blocks instead of continuing as though it had received something. The program remains waiting for the other program to send data.
A request and a response
A client and server are connected. The client wants information from the server. How can their actions be ordered so that neither program waits forever?
Client sends: The client writes its request to the socket instead of trying to read first.
Server reads: The server reads the request after the client has sent it.
Server sends: The server writes a response to the socket.
Client reads: The client reads the response after the server has sent it.
The order gives each read an expected preceding send, so the communication can proceed instead of leaving both programs waiting.
Protocols Set the Order
A protocol is a set of precise rules that determines the sequence of communication. It specifies who sends first, what they send, what response is expected, and who sends next.
Without an agreed order, both programs might choose to read first. Because no data is available, both can block. A protocol prevents this by assigning the next communication action. For example, a protocol can require the client to send a request first, require the server to read that request and send a response, and then require the client to read the response. The important idea is not the particular words in the messages; it is the precise ordering of sending, reading, responding, and sending again.
A Complete Exchange
A successful socket exchange can be understood as a sequence rather than as one isolated read or write. Two programs first have a socket connection. They then follow an agreed protocol: one program sends, the other reads, the other sends a response, and the first program reads that response. Because the order is known, each program has a reason to wait only at the point where the protocol says data should arrive.
- Establish a socket connection between the two programs.
- Use a protocol to decide which program sends first.
- Have the receiving program read after data is expected to be sent.
- Send the response required by the protocol.
- Continue the agreed sequence for any further exchange.
Mistakes That Cause Waiting
Assuming that a read always returns immediately.
When no data is available, the program blocks and waits.
Fix:
Follow a protocol that identifies when data should be sent before it is read.Letting both programs read first.
Both programs can block because neither sends the data the other is waiting for.
Fix:
Specify a sender and a receiver for each stage of the exchange.Treating the socket like local storage.
A socket connects programs; data must be written by one program before the other can read it.
Fix:
Think of the socket as a two-way connection whose data exchange depends on the other program.
Practice the Sequence
Imagine a protocol with this rule: the client sends a request, the server reads it, the server sends a response, and the client reads the response. Explain what happens if the client reads before sending its request. Then explain why the same problem can occur if both programs read first.
Hints
- Ask whether data is available at the moment of each read.
- Identify which program is responsible for sending the data that the other program expects.
- Use the word blocked to describe the state when a read has no available data.
Checking an exchange
Determine whether this order avoids the described deadlock: client reads, server reads, client sends, server sends.
Client reads first: No earlier send has been specified, so the client may block while waiting for data.
Server reads second: The server may also block because the client has not sent data.
Later sends cannot help immediately: If both programs are blocked at their reads, neither has reached the later send action.
Change the order: A protocol should assign a send before the corresponding read, such as client sends followed by server reads.
The proposed order can deadlock. An order with an agreed first sender allows the receiving program to read data after it has been sent.
Practical Programming Note
Python provides built-in support through its socket module. This support abstracts away the complexity of network communication and makes it straightforward to create network connections and exchange data. Even when a programming language provides that support, the communication still depends on the same ideas: a two-way socket, reads and writes, and a protocol that establishes the order.
Key Takeaways
- A socket is a two-way connection that lets two programs read and write data.
- A socket is similar to a file, but it connects to a remote program rather than local storage.
- Reading from a socket with no available data blocks the reading program.
- If both programs wait to read, they can deadlock.
- A protocol prevents this by specifying who sends first, what is sent, what response is expected, and who sends next.
Key Takeaways
- A socket connects two programs and supports data flow in both directions.
- A socket resembles a file in its read and write operations, but it connects to a remote program instead of local storage.
- A read can block when the other program has not sent data.
- A protocol provides the communication sequence needed to avoid deadlock.
- Successful communication requires an agreed order of connection, sending, reading, responding, and further exchange.