Skip to content

Latest commit

 

History

History
359 lines (286 loc) · 9.59 KB

File metadata and controls

359 lines (286 loc) · 9.59 KB

Socket Programming - Compilation Guide

Overview

This guide provides comprehensive instructions for compiling and running all three socket programming tasks on Windows 10 using Java 17 and IntelliJ IDEA.

Prerequisites

Required Software

  • Java Development Kit (JDK): Version 17.0.14 LTS or higher
  • Operating System: Windows 10 (instructions adaptable for other OS)
  • IDE: IntelliJ IDEA (Community or Ultimate Edition)
  • Git: For version control and repository management

Verification Commands

# Verify Java installation
java -version
javac -version

# Should display Java 17.x.x

Directory Structure Verification

socket-programming/
├── task1-hostname-converter/
│   └── src/
│       └── HostnameToIP.java
├── task2-ping-pong/
│   ├── src/
│   │   ├── server/
│   │   │   └── PingPongServer.java
│   │   └── client/
│   │       └── PingPongClient.java
└── task3-uppercase-converter/
    └── src/
        ├── server/
        │   └── UppercaseServer.java
        └── client/
            └── UppercaseClient.java

Compilation Methods

Method 1: IntelliJ IDEA (Recommended)

Project Setup

  1. Open IntelliJ IDEA
  2. Open Project: File → Open → Select socket-programming folder
  3. Verify Project Structure: Ensure all three modules are loaded
  4. Check SDK: File → Project Structure → Project → Ensure Java 17 is selected

Compilation in IDE

  1. Build All: Build → Build Project (Ctrl + F9)
  2. Verify Compilation: Check that no compilation errors appear
  3. Module-Specific Build: Right-click module → Build Module

Run Configurations Setup

Task 1 Configuration:

  • Name: HostnameToIP
  • Main class: HostnameToIP
  • Module: task1-hostname-converter

Task 2 Server Configuration:

  • Name: PingPongServer
  • Main class: server.PingPongServer
  • Module: task2-ping-pong

Task 2 Client Configuration:

  • Name: PingPongClient
  • Main class: client.PingPongClient
  • Module: task2-ping-pong

Task 3 Server Configuration:

  • Name: UppercaseServer
  • Main class: server.UppercaseServer
  • Module: task3-uppercase-converter

Task 3 Client Configuration:

  • Name: UppercaseClient
  • Main class: client.UppercaseClient
  • Module: task3-uppercase-converter

Method 2: Command Line Compilation

Task 1: Hostname to IP Converter

# Navigate to task directory
cd task1-hostname-converter

# Compile
javac src/HostnameToIP.java

# Run
java -cp src HostnameToIP

Task 2: Ping-Pong Application

# Navigate to task directory
cd task2-ping-pong

# Compile server
javac src/server/PingPongServer.java

# Compile client
javac src/client/PingPongClient.java

# Run server (Terminal 1)
java -cp src server.PingPongServer

# Run client (Terminal 2)
java -cp src client.PingPongClient

Task 3: Uppercase Converter

# Navigate to task directory
cd task3-uppercase-converter

# Compile server
javac src/server/UppercaseServer.java

# Compile client
javac src/client/UppercaseClient.java

# Run server (Terminal 1)
java -cp src server.UppercaseServer

# Run client (Terminal 2)
java -cp src client.UppercaseClient

Method 3: Batch Compilation Scripts

Windows Batch Scripts

Create file: compile-all.bat

@echo off
echo ========================================
echo Compiling Socket Programming Assignment
echo ========================================

echo.
echo Compiling Task 1: Hostname to IP Converter...
cd task1-hostname-converter
javac src/HostnameToIP.java
if %errorlevel% neq 0 (
    echo ERROR: Task 1 compilation failed
    exit /b 1
)
echo Task 1 compiled successfully
cd ..

echo.
echo Compiling Task 2: Ping-Pong Application...
cd task2-ping-pong
javac src/server/PingPongServer.java
if %errorlevel% neq 0 (
    echo ERROR: Task 2 server compilation failed
    exit /b 1
)
javac src/client/PingPongClient.java
if %errorlevel% neq 0 (
    echo ERROR: Task 2 client compilation failed
    exit /b 1
)
echo Task 2 compiled successfully
cd ..

echo.
echo Compiling Task 3: Uppercase Converter...
cd task3-uppercase-converter
javac src/server/UppercaseServer.java
if %errorlevel% neq 0 (
    echo ERROR: Task 3 server compilation failed
    exit /b 1
)
javac src/client/UppercaseClient.java
if %errorlevel% neq 0 (
    echo ERROR: Task 3 client compilation failed
    exit /b 1
)
echo Task 3 compiled successfully
cd ..

echo.
echo ========================================
echo All tasks compiled successfully!
echo ========================================

Create file: run-servers.bat

@echo off
echo Starting all servers...
echo.
echo Starting Ping-Pong Server on port 12345...
start "PingPong Server" cmd /k "cd task2-ping-pong && java -cp src server.PingPongServer"

timeout /t 2 /nobreak >nul

echo Starting Uppercase Server on port 54321...
start "Uppercase Server" cmd /k "cd task3-uppercase-converter && java -cp src server.UppercaseServer"

echo.
echo Both servers started in separate windows.
echo Press any key to exit this window...
pause >nul

Compilation Verification

Success Indicators

  1. No Compilation Errors: All .java files compile without errors
  2. Class Files Generated: Corresponding .class files created
  3. Application Startup: Programs start without exceptions
  4. Network Binding: Servers successfully bind to their ports

Verification Commands

# Task 1 verification
java -cp task1-hostname-converter/src HostnameToIP
# Should display welcome message and prompt

# Task 2 verification
java -cp task2-ping-pong/src server.PingPongServer
# Should display "Ping-Pong Server Started" message

# Task 3 verification  
java -cp task3-uppercase-converter/src server.UppercaseServer
# Should display "Uppercase Message Converter Server" message

Common Compilation Issues

Issue 1: "javac is not recognized"

Cause: Java not in system PATH Solution:

  1. Verify Java installation: java -version
  2. Add Java to PATH or use full path to javac
  3. Set JAVA_HOME environment variable

Issue 2: "Cannot find symbol" errors

Cause: Incorrect classpath or package structure Solution:

  1. Verify directory structure matches package declarations
  2. Use correct classpath: -cp src for source compilation
  3. Check import statements

Issue 3: Package declaration issues

Cause: Mismatch between package declaration and directory structure Solution:

  1. Ensure server and client subdirectories exist
  2. Verify package declarations in source files
  3. Compile from correct base directory

Issue 4: "Address already in use" at runtime

Cause: Previous server instance still running Solution:

  1. Stop previous server instance
  2. Wait 30 seconds for port release
  3. Use different port with command line argument

Issue 5: Class not found at runtime

Cause: Incorrect classpath for execution Solution:

  1. Use -cp src when running from base directory
  2. Verify the class file exists in expected location
  3. Check package name in java command

Advanced Compilation Options

Debug Compilation

# Compile with debug information
javac -g src/HostnameToIP.java

# Compile with verbose output
javac -verbose src/server/PingPongServer.java

Performance Optimization

# Compile with optimizations
javac -O src/client/UppercaseClient.java

# Compile with specific encoding
javac -encoding UTF-8 src/server/UppercaseServer.java

Documentation Generation

# Generate JavaDoc documentation
javadoc -d docs/api -sourcepath src -subpackages server,client

IDE-Specific Instructions

IntelliJ IDEA Troubleshooting

  1. Refresh Project: File → Reload Gradle/Maven Project
  2. Clear Cache: File → Invalidate Caches and Restart
  3. Rebuild: Build → Rebuild Project
  4. Check Module Dependencies: Project Structure → Modules

Eclipse Instructions (Alternative)

  1. Import Project: File → Import → Existing Projects into Workspace
  2. Set Build Path: Right-click project → Build Path → Configure Build Path
  3. Run Configuration: Run → Run Configurations → Java Application

Testing Compilation

Quick Test Sequence

# 1. Compile everything
./compile-all.bat

# 2. Test Task 1
java -cp task1-hostname-converter/src HostnameToIP
# Enter: google.com
# Expected: IP address displayed

# 3. Test Task 2 (two terminals)
# Terminal 1:
java -cp task2-ping-pong/src server.PingPongServer

# Terminal 2:
java -cp task2-ping-pong/src client.PingPongClient
# Enter: ping
# Expected: pong response

# 4. Test Task 3 (two terminals)
# Terminal 1:
java -cp task3-uppercase-converter/src server.UppercaseServer

# Terminal 2:  
java -cp task3-uppercase-converter/src client.UppercaseClient
# Enter: hello world
# Expected: HELLO WORLD response

Performance Considerations

Compilation Performance

  • Parallel Compilation: IntelliJ automatically uses multiple cores
  • Incremental Builds: Only modified files recompiled
  • Memory Settings: Increase heap size for large projects

Runtime Performance

  • JVM Optimization: Use -server flag for production
  • Memory Tuning: Adjust -Xmx and -Xms parameters
  • Garbage Collection: Consider G1GC for better performance

Conclusion

This compilation guide ensures successful building and execution of all socket programming tasks. The multiple compilation methods (IDE, command line, batch scripts) provide flexibility for different development preferences and deployment scenarios.

For troubleshooting specific issues not covered here, refer to the main troubleshooting guide or the task-specific documentation.