0
0
Unityframework~5 mins

Client-server architecture in Unity

Choose your learning style9 modes available
Introduction

Client-server architecture helps computers talk to each other. One computer (server) shares information, and others (clients) ask for it.

When making a multiplayer game where players connect to a central game server.
When your game needs to save player progress on a remote server.
When you want to get live data like scores or chat messages from a server.
When you want to separate game logic on a server and show results on players' devices.
Syntax
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.

Examples
Connects the client to the server at the given IP and port.
Unity
TcpClient client = new TcpClient("server_ip", port_number);
Gets the stream to send and receive data.
Unity
NetworkStream stream = client.GetStream();
Sends a message to the server.
Unity
byte[] data = Encoding.ASCII.GetBytes("message");
stream.Write(data, 0, data.Length);
Reads the server's response.
Unity
int bytes = stream.Read(buffer, 0, buffer.Length);
string response = Encoding.ASCII.GetString(buffer, 0, bytes);
Sample Program

This Unity script connects to a server at localhost on port 5500, sends a greeting, and prints the server's reply in the Console.

Unity
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();
    }
}
OutputSuccess
Important Notes

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.

Summary

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.