0% found this document useful (0 votes)
5 views

Handout - Basic Networking Assignment - Unity Client and Console Server (1)

Basic networking

Uploaded by

farbulousdee
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Handout - Basic Networking Assignment - Unity Client and Console Server (1)

Basic networking

Uploaded by

farbulousdee
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Handout: Basic Networking Assignment - Unity Client &

Console Server
Objective:

In this assignment, you will set up a basic network communication system. You’ll create:

1. A Console Server that listens for incoming connections and sends a greeting message.
2. A Unity Client that connects to the server, receives the greeting message, and displays it
in Unity’s Console.

Background Concepts:
1. Client: The application that initiates a connection (Unity).
2. Server: The application that listens for incoming connections and sends a response
(Console Application).
3. Socket: The endpoint for sending/receiving data over a network connection.
4. IP Address and Port: The address and communication channel where the server listens
for client requests.

Assignment Requirements
Console Server: Create a C# console application that listens on a specified port and
sends a message to any client that connects.
Unity Client: Create a Unity script that connects to the console server, reads the
message, and displays it in Unity’s Console.

Part 1: Create the Console Server


1. Open Visual Studio:
Create a new Console App (.NET Core) project. Name it SimpleServer .
2. Write the Server Code:
Copy and paste the following code into the Program.cs file:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class Program
{
static void Main()
{
TcpListener listener = new TcpListener(IPAddress.Any, 8080);
listener.Start();
Console.WriteLine("Server started... Waiting for
connection.");

while (true)
{
TcpClient client = listener.AcceptTcpClient();
NetworkStream stream = client.GetStream();

string message = "Hello from the server!";


byte[] data = Encoding.ASCII.GetBytes(message);
stream.Write(data, 0, data.Length);

Console.WriteLine("Sent message to client.");


client.Close();
}
}
}

This code listens on port 8080 and sends a “Hello from the server!” message to any
client that connects.
3. Run the Server:
Start the console application (server). It should display “Server started... Waiting for
connection.”

Part 2: Create the Unity Client


1. Open Unity:
Create a new Unity 3D project or open your existing project.
2. Create a New Script:
In Unity, create a new C# script named NetworkClient .
Attach this script to an empty GameObject in your scene.
3. Write the Client Code:
Replace the contents of the NetworkClient.cs file with the following:

using System;
using System.Net.Sockets;
using System.Text;
using UnityEngine;

public class NetworkClient : MonoBehaviour


{
void Start()
{
try
{
TcpClient client = new TcpClient("127.0.0.1", 8080);
NetworkStream stream = client.GetStream();

byte[] data = new byte[256];


int bytes = stream.Read(data, 0, data.Length);
string responseData = Encoding.ASCII.GetString(data, 0,
bytes);
Debug.Log("Received from server: " + responseData);

client.Close();
}
catch (Exception e)
{
Debug.LogError("Connection error: " + e.Message);
}
}
}

This script connects to the server on 127.0.0.1 (localhost) at port 8080 , receives
the greeting message, and logs it to Unity’s Console.
4. Run the Client:
Start the Unity project. The client script should automatically connect to the console
server, receive the message, and display it in Unity’s Console.
Part 3: Testing and Submitting
1. Testing:
Ensure the console server is running before starting the Unity client.
Verify that the message “Hello from the server!” appears in Unity’s Console after
running the client.
2. Submission:
Submit the following:
A screenshot of Unity’s Console showing the message received from the server.
A brief reflection on any issues you encountered and how you resolved them.

Bonus Challenge (Optional)


1. Modify the Server:
Update the console server to send a response based on the message received from
the client. For example:
If the client sends “Hello, Server!”, the server responds with “Hello, Unity!”.
Implement code in the Unity client to send an initial message to the server, then
receive and display the response.

Troubleshooting Tips
Connection Error: If you see a connection error, ensure the server is running and that the
IP address and port match on both client and server.
Firewall Issues: Firewalls or security software may block network connections.
Temporarily disable these if you experience issues.
Console Output: Remember to check both the console application and Unity’s Console
for errors and output.

This assignment will help you understand basic client-server networking and reinforce concepts
of TCP connections, IP addresses, and data exchange over a network. Let me know if you have
any questions during the assignment!

You might also like