-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientTCP.java
58 lines (48 loc) · 1.81 KB
/
ClientTCP.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
import java.io.*;
import java.net.*;
// import java.lang.*;
public class ClientTCP {
public static void main(String[] args) throws IOException {
Socket clientSocket = null;
PrintWriter out = null;
BufferedReader in = null;
int port;
port = Integer.parseInt(args[1]);
System.out.println("port : " + port);
try {
clientSocket = new Socket(args[0], port);
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + args[0]);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for " + "the connection to: "
+ args[0]);
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
String userInput;
char[] buf = new char[64];
int r;
// Lecture de la socket
r = in.read(buf, 0, 64); // Attention, pas de \n envoye par le serveur
// donc pas de messageServeur = in.readLine();
System.out.print(buf); // Affichage
while ((userInput = stdIn.readLine()) != null) {
// Envoi sur la socket
out.println(userInput);
out.flush();
// Lecture de la socket et affichage
// Lecture de la socket
r = in.read(buf, 0, 64); // Attention, pas de \n envoye par le serveur
// donc pas de messageServeur = in.readLine();
System.out.print(buf); // Affichage
}
out.close();
in.close();
stdIn.close();
clientSocket.close();
}
}