TCPClient.java 841 B

123456789101112131415161718192021222324
  1. import java.io.*;
  2. import java.net.*;
  3. public class TCPClient {
  4. public static void startClient() {
  5. String serverAddress = "127.0.0.1"; // Make sure this matches your TypeScript server
  6. int port = 12345;
  7. try (Socket socket = new Socket(serverAddress, port);
  8. BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  9. PrintWriter writer = new PrintWriter(socket.getOutputStream(), true)) {
  10. // Send a message to the TypeScript server
  11. writer.println("Hello, Server from Java!");
  12. // Receive response from the TypeScript server
  13. String response = reader.readLine();
  14. System.out.println("Server says: " + response);
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. }