-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStagClient.java
More file actions
35 lines (33 loc) · 1.25 KB
/
StagClient.java
File metadata and controls
35 lines (33 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.io.*;
import java.net.*;
public class StagClient
{
public static void main(String args[])
{
if(args.length != 1) System.out.println("Usage: java StageClient <player-name>");
else {
String playerName = args[0];
BufferedReader commandLine = new BufferedReader(new InputStreamReader(System.in));
while(true) handleNextCommand(commandLine, playerName);
}
}
private static void handleNextCommand(BufferedReader commandLine, String playerName)
{
try {
String incoming;
System.out.print("\n" + playerName + ": ");
String command = commandLine.readLine();
Socket socket = new Socket("127.0.0.1", 8888);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.write(playerName + ": " + command + "\n");
out.flush();
while((incoming = in.readLine()) != null) System.out.println(incoming);
in.close();
out.close();
socket.close();
} catch(IOException ioe) {
System.out.println(ioe);
}
}
}