0
0
Unityframework~10 mins

Client-server architecture in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Client-server architecture
Client sends request
Server receives request
Server processes request
Server sends response
Client receives response
Client uses data
The client sends a request to the server, the server processes it and sends back a response, which the client then uses.
Execution Sample
Unity
using UnityEngine;
using System.Net.Sockets;

// Client sends message to server
TcpClient client = new TcpClient("127.0.0.1", 5500);
NetworkStream stream = client.GetStream();
byte[] data = System.Text.Encoding.ASCII.GetBytes("Hello Server");
stream.Write(data, 0, data.Length);

// Remember to close the stream and client after use
stream.Close();
client.Close();
This code shows a Unity client connecting to a server and sending a message.
Execution Table
StepActionDetailsResult
1Client creates TcpClientConnects to 127.0.0.1:5500Connection established
2Client gets NetworkStreamPrepares to send dataStream ready
3Client encodes message"Hello Server" to bytesByte array created
4Client writes to streamSends bytes to serverMessage sent
5Server receives messageReads bytes from streamMessage received
6Server processes messageInterprets "Hello Server"Ready to respond
7Server sends responseSends reply bytesResponse sent
8Client reads responseReads from streamResponse received
9Client uses responseDisplays or processes dataInteraction complete
10Connection closedClient or server closes connectionCommunication ended
💡 Communication ends after client and server exchange messages and close connection.
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 8Final
clientnullTcpClient connectedTcpClient connectedTcpClient connectedClosed or disposed
streamnullNetworkStream readyNetworkStream readyNetworkStream readyClosed
datanullnullByte array with messagenullnull
responsenullnullnullByte array with server replyUsed or stored
Key Moments - 3 Insights
Why do we convert the message string to bytes before sending?
Network communication sends bytes, not strings. See execution_table step 3 where the string is encoded to bytes before sending.
What happens if the server is not running when the client tries to connect?
The client cannot establish a connection (step 1 fails). This stops communication early, so no stream or data is sent.
Why do we need to close the connection after communication?
To free resources and avoid hanging connections. See execution_table step 10 where connection is closed to end communication.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of 'client' after step 1?
AConnection closed
BTcpClient connected
Cnull
DByte array created
💡 Hint
Check variable_tracker row for 'client' after Step 1.
At which step does the client send the message bytes to the server?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at execution_table action descriptions for sending data.
If the client never closes the connection, what happens according to the flow?
AResources remain used, connection hangs
BServer closes connection automatically
CCommunication ends normally
DClient reconnects automatically
💡 Hint
Refer to key_moments about closing connection and execution_table step 10.
Concept Snapshot
Client-server architecture:
- Client sends requests to server
- Server processes and replies
- Communication uses network streams
- Messages sent as bytes
- Connections must be closed after use
Full Transcript
Client-server architecture means one program (client) asks another (server) for data or service. The client opens a connection to the server's address and port. It sends a message by converting text to bytes and writing to the network stream. The server reads the bytes, processes the request, and sends back a response. The client reads the response bytes and uses the data. Finally, both close the connection to free resources. This flow ensures clear communication between two programs over a network.