-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp2cli.java
More file actions
85 lines (62 loc) · 2.87 KB
/
Copy pathtcp2cli.java
File metadata and controls
85 lines (62 loc) · 2.87 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.io.*;
import java.util.*;
import java.net.*;
import java.util.Scanner;
import java.net.SocketTimeoutException;
public class tcp2cli {
public static void main (String [] args){ //java tcp2cli ip_address port_number
if (args.length!=2) {
System.out.println("You have to enter the parameters with this format: tcp2cli ip_address port_number");
} else {
try {
//to read
Scanner keyboard = new Scanner (System.in);
InetAddress ipAddress = InetAddress.getByName(args[0]); //args[0] is the ip_address
String port = args[1]; //args[1] is the port_number
int portServer = Integer.parseInt(port);
//to connect to the server
Socket socketClient = new Socket();
SocketAddress socketServer = new InetSocketAddress(ipAddress,portServer);
socketClient.connect(socketServer,15000); //we make the connection
while(true){ // to make the accumulator 0 in each client
System.out.println("Enter a row of numbers separated by spaces. The 0 or the enter means the end of the message:");
String clientNumbers= keyboard.nextLine();
String separatedNumbers[] = clientNumbers.split(" "); //numbers separated by spaces
if(Integer.parseInt(separatedNumbers[0])==0){
//System.out.println("The 0 means end of the message, so it cannot be the first number");
//we close the socket
break;
}
String numbersToSend = "";
//put the numbers into the string to send them
for (int i=0; i<separatedNumbers.length; i++) {
Integer number = Integer.parseInt(separatedNumbers[i]);
if (number != 0) {
numbersToSend+= separatedNumbers[i]+" ";
} else
break;
}
//we send the information to the server
DataOutputStream infoClientToSend = new DataOutputStream(socketClient.getOutputStream());
infoClientToSend.writeUTF(numbersToSend);
//we receive the accumulator from the server
DataInputStream inforServerToReceive = new DataInputStream(socketClient.getInputStream());
int answer = inforServerToReceive.readInt();
System.out.println("The accumulator received by the server is: " + answer);
} //end while
//we close the socket
socketClient.close();
} catch(SocketTimeoutException exception1) { //Error when the server has taken more than 15 seconds to answer
System.out.println("[ERROR] Timeout: The server has taken more than 15 seconds to connect");
System.exit(-1);
} catch (SocketException exception2) {
System.out.println("[ERROR] Socket: " + exception2.getMessage());
System.exit(-1);
} catch (IOException exception3) {
System.out.println("[ERROR] IO: " + exception3.getMessage());
System.exit(-1);
} catch(Exception e){
}
}//end else
}
}//end class