2021-03-27 15:33:25 -04:00

76 lines
1.6 KiB
Java

import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
// Server Host
final String serverHost = "localhost";
Socket socketOfClient = null;
BufferedWriter os = null;
BufferedReader is = null;
try {
//Send request to connect on 6666
socketOfClient = new Socket(serverHost, 6666);
// Create ouput stream at client to send out data
os = new BufferedWriter(new OutputStreamWriter(socketOfClient.getOutputStream()));
is = new BufferedReader(new InputStreamReader(socketOfClient.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host " + serverHost);
return;
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " + serverHost);
return;
}
try {
// write data to output client socket
os.write("10");
os.newLine();
os.write("20");
os.newLine();
os.write("11");
os.newLine();
os.write("75");
os.newLine();
os.write("QUIT");
os.newLine();
// Flush data.
os.flush();
// Read data from server
// by reading client input socket
String responseLine;
while ((responseLine = is.readLine()) != null) {
System.out.println("Server: " + responseLine);
if (responseLine.indexOf("OK") != -1) {
break;
}
}
os.close();
is.close();
socketOfClient.close();
} catch (UnknownHostException e) {
System.err.println("Trying to connect to unknown host: " + e);
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}