Untitled

mail@pastecode.io avatar
unknown
plain_text
22 days ago
1.2 kB
2
Indexable
Never
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();
        }
    }
}
Leave a Comment