Client-server architecture helps computers talk to each other. One computer (server) shares information, and others (clients) ask for it.
Client-server architecture in Unity
using UnityEngine; using System.Net.Sockets; using System.Text; public class SimpleClient : MonoBehaviour { TcpClient client; NetworkStream stream; void Start() { client = new TcpClient("127.0.0.1", 5500); stream = client.GetStream(); byte[] data = Encoding.ASCII.GetBytes("Hello Server"); stream.Write(data, 0, data.Length); byte[] responseData = new byte[256]; int bytes = stream.Read(responseData, 0, responseData.Length); string response = Encoding.ASCII.GetString(responseData, 0, bytes); Debug.Log("Server says: " + response); } void OnApplicationQuit() { stream?.Close(); client?.Close(); } }
This example shows a simple client connecting to a server using TCP.
Unity uses Debug.Log to print messages in the Console window.
TcpClient client = new TcpClient("server_ip", port_number);NetworkStream stream = client.GetStream();
byte[] data = Encoding.ASCII.GetBytes("message"); stream.Write(data, 0, data.Length);
int bytes = stream.Read(buffer, 0, buffer.Length); string response = Encoding.ASCII.GetString(buffer, 0, bytes);
This Unity script connects to a server at localhost on port 5500, sends a greeting, and prints the server's reply in the Console.
using UnityEngine; using System.Net.Sockets; using System.Text; public class SimpleClient : MonoBehaviour { TcpClient client; NetworkStream stream; void Start() { try { client = new TcpClient("127.0.0.1", 5500); stream = client.GetStream(); byte[] data = Encoding.ASCII.GetBytes("Hello Server"); stream.Write(data, 0, data.Length); byte[] responseData = new byte[256]; int bytes = stream.Read(responseData, 0, responseData.Length); string response = Encoding.ASCII.GetString(responseData, 0, bytes); Debug.Log("Server says: " + response); } catch (SocketException e) { Debug.Log("SocketException: " + e.Message); } } void OnApplicationQuit() { stream?.Close(); client?.Close(); } }
Always close your network streams and clients to free resources.
Client-server communication needs matching server code running to accept connections.
Network errors can happen; handle exceptions to avoid crashes.
Client-server architecture lets your game talk to a central server.
Clients send requests; servers send back responses.
Unity uses TcpClient and NetworkStream for network communication.
Practice
Solution
Step 1: Understand client role in client-server
The client initiates communication by sending requests to the server.Step 2: Understand server response
The server processes requests and sends back responses to the client.Final Answer:
Send requests to the server and receive responses -> Option AQuick Check:
Client = Request sender [OK]
- Thinking client stores all data permanently
- Confusing client with server hardware control
- Assuming clients manage other clients
Solution
Step 1: Recall TcpClient constructor syntax
In C#, creating a new TcpClient requires the 'new' keyword and constructor with IP and port.Step 2: Check each option's syntax
Only TcpClient client = new TcpClient("127.0.0.1", 8080); uses 'new TcpClient("127.0.0.1", 8080);' correctly.Final Answer:
TcpClient client = new TcpClient("127.0.0.1", 8080); -> Option DQuick Check:
Use 'new' keyword with constructor [OK]
- Omitting 'new' keyword
- Using incorrect method names like connect or connectTo
- Wrong constructor call syntax
using System;
using System.Net.Sockets;
using System.Text;
TcpClient client = new TcpClient("127.0.0.1", 9000);
NetworkStream stream = client.GetStream();
byte[] data = new byte[256];
int bytes = stream.Read(data, 0, data.Length);
string response = Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine(response);Solution
Step 1: Understand data reading from NetworkStream
The code reads bytes from the stream into a byte array and counts bytes read.Step 2: Convert bytes to string
It converts the bytes read to a string using ASCII encoding, which matches the server's sent string.Final Answer:
Hello Client -> Option BQuick Check:
Read bytes + decode = server message [OK]
- Printing byte array directly instead of decoding
- Ignoring number of bytes read
- Assuming null if no data
TcpClient client = new TcpClient();
client.Connect("192.168.1.10", 7000);
NetworkStream stream = client.GetStream();
byte[] message = Encoding.ASCII.GetBytes("Ping");
stream.Write(message, 0, message.Length);
stream.Close();
client.Close();Solution
Step 1: Review TcpClient creation and connection
Creating TcpClient with default constructor and then calling Connect is valid.Step 2: Check for error handling
Network operations can fail; missing try-catch means exceptions are unhandled.Final Answer:
Missing try-catch block for network exceptions -> Option AQuick Check:
Always handle network exceptions [OK]
- Assuming stream.Close() before client.Close() is wrong
- Thinking TcpClient must have IP in constructor
- Ignoring exception handling
Solution
Step 1: Check conditional connection and sending
TcpClient client = new TcpClient(); if(score > 1000) { client.Connect("server.com", 5000); NetworkStream stream = client.GetStream(); byte[] data = Encoding.ASCII.GetBytes(score.ToString()); stream.Write(data, 0, data.Length); stream.Close(); client.Close(); } creates TcpClient without parameters, connects only if score > 1000, then sends data.Step 2: Verify data conversion and method calls
Converts score to string before encoding to bytes, uses correct Connect() and stream methods.Final Answer:
new TcpClient(); client.Connect() inside if(score > 1000) -> Option CQuick Check:
Connect and send inside if condition [OK]
- Sending data without converting score to string
- Connecting outside the if condition
- Using score directly as bytes without encoding
Ready to prove it?
Pass the test and this pattern moves unknown
