-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.java
70 lines (57 loc) · 2.11 KB
/
Client.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*********************
* Socket Client
* @file Client.java
* @author Wangai
**********************/
import java.net.*;
import java.io.*;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class Client {
public static void main(String [] args) {
Socket client = null;
String host = args[0];
int port = Integer.parseInt(args[1]);
try {
client = new Socket(host, port);
// Set up streams for server I/O
InputStream inFromServer = client.getInputStream();
DataInputStream input = new DataInputStream(inFromServer);
OutputStream outToServer = client.getOutputStream();
DataOutputStream output = new DataOutputStream(outToServer);
//Set up stream for keyboard entry
Scanner userEntry = new Scanner(System.in);
// System.out.println("Server says " + input.readUTF());
System.out.println(input.readUTF());
int service, input1, input2;
do {
System.out.print("\nservice: ");
service = userEntry.nextInt();
System.out.print("input1 : ");
input1 = userEntry.nextInt();
System.out.print("input2 : ");
input2 = userEntry.nextInt();
//send the numbers
output.writeInt(service);
output.writeInt(input1);
output.writeInt(input2);
// System.out.println("\nSERVER> " + input.readUTF());
System.out.println(input.readUTF());
} while (service != 0);
} catch (IOException e) {
e.printStackTrace();
}
catch (NoSuchElementException ne){ //This exception may be raised when the server closes connection
System.out.println("Connection closed");
}
finally {
try {
System.out.println("\n* Closing connection *");
client.close();
} catch (IOException ioEx) {
System.out.println("Unable to disconnect");
System.exit(1);
}
}
}
}