Browse Source

TCP client testing

enzo 2 weeks ago
parent
commit
868a08af20
3 changed files with 44 additions and 0 deletions
  1. 3 0
      Main.java
  2. 17 0
      README.md
  3. 24 0
      TCPClient.java

+ 3 - 0
Main.java

@@ -1,5 +1,8 @@
 public class Main {
     public static void main(String[] args) {
         System.out.println("Hello, Java in VS Code!");
+
+        // Call the TCP client
+        TCPClient.startClient();
     }
 }

+ 17 - 0
README.md

@@ -0,0 +1,17 @@
+# Java Project
+
+This is a simple Java project set up in **VS Code**.
+
+## 🚀 Getting Started
+
+### Prerequisites
+- Java 21.0.6 (or any compatible version)
+- Git (optional, for version control)
+- VS Code with the Java extension pack
+
+### 📌 Setup Instructions
+
+1. Clone the repository (if using GitHub):
+   ```sh
+   git clone <repo-url>
+   cd <your-project-folder>

+ 24 - 0
TCPClient.java

@@ -0,0 +1,24 @@
+import java.io.*;
+import java.net.*;
+
+public class TCPClient {
+    public static void startClient() {
+        String serverAddress = "127.0.0.1"; // Make sure this matches your TypeScript server
+        int port = 12345;
+
+        try (Socket socket = new Socket(serverAddress, port);
+             BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
+             PrintWriter writer = new PrintWriter(socket.getOutputStream(), true)) {
+
+            // Send a message to the TypeScript server
+            writer.println("Hello, Server from Java!");
+
+            // Receive response from the TypeScript server
+            String response = reader.readLine();
+            System.out.println("Server says: " + response);
+
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+}