Handout - Basic Networking Assignment - Unity Client and Console Server (1)
Handout - Basic Networking Assignment - Unity Client and Console Server (1)
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.
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();
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.”
using System;
using System.Net.Sockets;
using System.Text;
using UnityEngine;
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.
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!