Untitled
import java.io.*; import java.net.*; public class TCPClient { private static final String SERVER_ADDRESS = "127.0.0.1"; private static final int SERVER_PORT = 12345; public static void main(String[] args) { try (Socket socket = new Socket(SERVER_ADDRESS, SERVER_PORT); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in))) { System.out.println("Connected to server at " + SERVER_ADDRESS + ":" + SERVER_PORT); // Read a message from the user System.out.print("Enter message to send: "); String message = userInput.readLine(); out.println(message); // Read and print the response from the server String response = in.readLine(); System.out.println("Server response: " + response); } catch (IOException e) { e.printStackTrace(); } } }
Leave a Comment