-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.java
More file actions
68 lines (63 loc) · 2.45 KB
/
client.java
File metadata and controls
68 lines (63 loc) · 2.45 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
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
package tcpclient;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
import java.net.*;
/**
*
* @author sishunw
*/
public class client {
public static void main(String[] args) throws Exception {
// TODO code application logic here
if(args.length != 2) {
System.out.println("Incorret arguments. Usage: client [serverURI] [port_number]");
return;
}
boolean exit = false;
//String uri = "sand.cise.ufl.edu";
String uri = args[0];
int port = Integer.parseInt(args[1]);
System.out.println("./client " + uri + " " + port);
Socket clientSocket = new Socket(uri, port);
String sentence;
String modifiedSentence;
int answer = 0;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String welcome = inFromServer.readLine();
System.out.println("receive: " + welcome);
while (!exit){
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
answer = Integer.parseInt(modifiedSentence);
//System.out.println("FROM SERVER: " + modifiedSentence);
switch (answer){
case -1:
System.out.println("receive: incorrect operation command.");
break;
case -2:
System.out.println("receive: number of inputs is less than two.");
break;
case -3:
System.out.println("receive: number of inputs is more than four.");
break;
case -4:
System.out.println("receive: one or more of the inputs contain(s) non-number(s).");
break;
case -5:
System.out.println("receive: exit.");
exit = true;
break;
default:
System.out.println("receive: " + answer);
}
}
clientSocket.close();
}
}