A client/server implementation of Connect Four over raw TCP sockets: a JavaFX client where a person plays against a computer-controlled server.
  • Java 96.7%
  • Dockerfile 3.1%
  • CSS 0.2%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-10-15 12:12:08 -04:00
ConnectFour_Client Make the project runnable, by building with Docker 2026-10-15 12:03:21 -04:00
ConnectFour_Library Make the project runnable, by building with Docker 2026-10-15 12:03:21 -04:00
ConnectFour_ParentPom Add explanatory file for parent pom installation 2018-10-02 09:11:39 -04:00
ConnectFour_Server Add AI-generated code for server to print correct local IP address 2026-10-15 12:04:50 -04:00
.gitignore Incorporate changes in mine 2018-10-26 00:58:22 -04:00
Dockerfile Make the project runnable, by building with Docker 2026-10-15 12:03:21 -04:00
README.md Add README.md explaining how to run/understand the project 2026-10-15 12:04:03 -04:00

Connect Four — TCP client/server in Java

A client/server implementation of Connect Four over raw TCP sockets: a JavaFX client where a person plays against a computer-controlled server.

Course project for 420-544-DW Data Communications and Networking (2018, team of students) at Dawson College.

Summary

Context

The assignment (Project-01.docx) was to write a TCP client and server in Java that let a person play Connect Four against the computer. The main requirements were:

  • The server listens on port 50000, prints its IP address at start-up, and serves one client at a time (an iterative server). When the client closes its socket, the server goes back to listening for a new one.
  • The client asks the user for the server's IP address, then lets them start games, play, and quit.
  • All messages are fixed-length, multi-byte packets designed by the team.
  • The server is the referee: it validates moves, detects wins and draws, and plays the black markers.

Modules

Module Jar Role
ConnectFour_ParentPom — Parent Maven POM (dependencies, build configuration)
ConnectFour_Library ConnectFour_Library.jar Classes shared by both sides: Board, Move, Colour, Status
ConnectFour_Server ConnectFour_Server.jar The iterative TCP server, game rules and computer opponent
ConnectFour_Client ConnectFour_Client.jar JavaFX GUI: connection screen and game board

How it works

The client opens a TCP connection to the server on port 50000. Every message in either direction is exactly 3 bytes: [type / status] [colour] [column].

Direction Bytes Meaning
server → client 06 00 00 Game start (sent as soon as the client connects)
client → server 01 cc col Play a marker in column col (0–6)
server → client 01 cc col OK — the server answers with its own move in column col
server → client 02 .. The player won
server → client 03 cc col The server won (with this move)
server → client 04 .. Draw
server → client 05 .. Invalid move (defined in the protocol; the client also checks that the column isn't full before sending)
client → server 07 00 00 Game refused

A new game is a new connection (the Restart button reconnects); closing the socket ends the session.

The server keeps the authoritative board. The client keeps a 6×7 copy only to draw it. On the server the 6×7 playing area sits inside a 12×13 array with a 3-cell border, so the win-checking code can look three cells in any direction without bounds checks (columns are shifted by 3 on arrival).

The server's opponent is a simple heuristic: it scans the board for lines of four where it (or the player) already has three markers, plays there to win or block, and otherwise plays a random column near its previous move.

Topics covered

  • TCP socket programming — ServerSocket/Socket, the accept loop of an iterative server, reading and writing byte streams, treating a closed socket as the end of a session.
  • Application-layer protocol design — fixed-length packets, status codes, a start-of-game handshake, and keeping client and server in sync turn by turn.
  • Server-authoritative design — the server validates every move and decides wins and draws; the client is a thin view.
  • Game logic and a heuristic opponent — win detection in four directions, the padded-board technique, and brute-force scanning to find winning or blocking moves.
  • Layered, object-oriented architecture — application / business / presentation packages, a shared library module for the data model, UML class diagram.
  • Event-driven GUI with JavaFX/FXML — controllers, mouse-move and click handlers, scene switching between the connection screen and the game.
  • Maven multi-module builds — a parent POM, inter-module dependencies, and shading dependencies into freestanding executable jars.
  • Logging — SLF4J facade with Log4j 2 (console and rolling file).
  • Teamwork — group work on a private GitLab project.

How to run

Requirements

  • Java 17 or newer (a JRE is enough to run the jars).
  • To run the client on Linux, GTK 3 must be installed. Windows (x64), Linux (x64) and macOS (Apple Silicon) are supported out of the box; see Other platforms.
  • To build the jars: Docker (recommended), or JDK 17+ and Maven 3.9+.

1. Build the jars

From the repository root (the folder containing this file and the Dockerfile):

docker build -o dist .

This builds everything inside a container and copies the results to ./dist:

dist/ConnectFour_Library.jar
dist/ConnectFour_Server.jar
dist/ConnectFour_Client.jar

All three are freestanding: dependencies (including JavaFX and logging) are bundled, so nothing else needs to be installed.

2. Start the server

java -jar dist/ConnectFour_Server.jar

It prints its address and waits for a client:

Server Address [192.168.1.20:50000]

3. Start the client

java -jar dist/ConnectFour_Client.jar
  1. Enter the server's IP address and port (50000) and click Connect.
  2. Move the mouse over the board to pick a column and click to drop a red marker. The server answers with a black one.
  3. When the game ends, a message says whether you won, lost or drew. Click Restart for a new game or Quit to return to the connection screen.

The server handles one client at a time; once the client disconnects it accepts the next one.

Notes

  • Other platforms. The client jar bundles the JavaFX native libraries for Linux x64, Windows x64 and macOS Apple Silicon. Only one macOS flavour and one Linux flavour fit in a single jar, so for an Intel Mac or Linux on ARM, rebuild with:
    docker build -o dist --build-arg MAVEN_ARGS="-Djavafx.mac.classifier=mac" .
    docker build -o dist --build-arg MAVEN_ARGS="-Djavafx.linux.classifier=linux-aarch64" .
    
  • Warnings on startup. The client may print a JavaFX warning about being loaded from the classpath, and recent JDKs may print native-access warnings. They are harmless.
  • Logs. Logging is set to the most verbose level, so the console is chatty. A log file is also written to log/c4/Standard.log in the directory you run from. Adjust src/main/resources/log4j2.xml in each module to change this.

What changed from the 2018 code

The sources are unchanged apart from one added class, so the project builds and runs on current Java:

  • JavaFX is no longer part of the JDK, so it is now a bundled Maven dependency (ConnectFour_Client/pom.xml). A small Launcher class was added as the jar's entry point, since a JavaFX Application subclass can't be started directly from a jar without the module path.
  • Log4j 2.11.0 / SLF4J 1.7 were upgraded to Log4j 2.25.0 / SLF4J 2.0 (2.11.0 is affected by the Log4Shell vulnerability).
  • The shade plugin was upgraded and its configuration moved into the parent POM; the build targets Java 17 instead of Java 8.