SocketClient
unknown
plain_text
3 years ago
4.5 kB
9
Indexable
public class SocketClient {
public static void main(String[] args) {
// Define server host and port
String serverHost = "localhost";
int serverPort = 8080;
// Try with resources block to automatically close sockets and streams
try (Socket socket = new Socket(serverHost, serverPort);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in))) {
// Variable to store user request
String request;
// Loop to continuously process user input
while (true) {
// Prompt user for command
System.out.print("Enter command (ADDSONG, GETALLSONGS, GETARTIST, GETSONG, quit): ");
// Read user input from the console
request = consoleReader.readLine();
// Break the loop if user input is "quit"
if (request == null || "quit".equalsIgnoreCase(request)) {
break;
}
// Send user request to the server
out.println(request);
System.out.println("Sent request: " + request);
// Process server responses based on the user request
if ("ADDSONG".equalsIgnoreCase(request)) {
// Read the server response
String response = in.readLine();
System.out.println("Received response:");
System.out.println(response);
// Read song name from the user
String songName = consoleReader.readLine();
// Send the song name to the server
out.println(songName);
// Read the server response
response = in.readLine();
System.out.println("Received response:");
System.out.println(response);
// Read artist name from the user
String artistName = consoleReader.readLine();
// Send the artist name to the server
out.println(artistName);
// Read the server response
response = in.readLine();
System.out.println("Received response:");
System.out.println(response);
} else if ("GETALLSONGS".equalsIgnoreCase(request)) {
// Read the server response
String response = in.readLine();
System.out.println("Received response: " + response);
} else if ("GETARTIST".equalsIgnoreCase(request)) {
// Read the server response
String response = in.readLine();
System.out.println("Received response: " + response);
// Read artist name from the user
String artistName = consoleReader.readLine();
// Send the artist name to the server
out.println(artistName);
// Read the server response
response = in.readLine();
System.out.println("Received response: " + response);
} else if ("GETSONG".equalsIgnoreCase(request)) {
// Read the server response
String response = in.readLine();
System.out.println("Received response: " + response);
// Read song name from the user
String songName = consoleReader.readLine();
// Send the song name to the server
out.println(songName);
// Read the server response
response = in.readLine();
System.out.println("Received response: " + response);
} else {
// If the user enters an unknown request
System.out.println("Unknown request");
}
}
} catch (IOException e) {
// Handle exceptions related to server connection
System.err.println("Error connecting to server: " + e.getMessage());
}
}
}Editor is loading...