Untitled
unknown
plain_text
a year ago
1.2 kB
9
Indexable
import java.io.*; import java.net.*; public class Client { public static void main(String[] args) { String serverAddress = "10.20.5.254"; // Replace with your server's public IP address int serverPort = 4020; try { // Connect to the server Socket socket = new Socket(serverAddress, serverPort); System.out.println("Connected to server at " + serverAddress + ":" + serverPort); // Set up streams for communication PrintWriter toServer = new PrintWriter(socket.getOutputStream(), true); BufferedReader fromServer = new BufferedReader(new InputStreamReader(socket.getInputStream())); // Send "hello" to the server String messageToSend = "hello"; toServer.println(messageToSend); System.out.println("Sent message to server: " + messageToSend); // Receive response from the server String response = fromServer.readLine(); System.out.println("Received response from server: " + response); // Close the connection socket.close(); } catch (IOException e) { e.printStackTrace(); } } }
Editor is loading...
Leave a Comment