-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
102 lines (91 loc) · 3.79 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package networking.p2p;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
/**
*
* @author rafiul islam
*/
public class Client {
/**
* Client will try to connect with server using same port as server has.
* Here I define a constant port and other side username.
* Before client try to connect with server, server must opened before.
* @see networking.p2p.Server
*/
private static final int PORT = 6789;
private static final String SENDER = "SERVER: ";
public static void main(String[] args) {
/**
* A Scanner for input user message from console.
*/
Scanner scan = new Scanner(System.in);
try{
/**
* Client socket need an InedAdress for connection and a port that is
* also used by server.
*/
InetAddress host = InetAddress.getByName("localhost");
System.out.println("Trying to connect by"+host+" with port: "+PORT);
Socket socket = new Socket(host, PORT);
/**
* This thread will stream from remote side of the socket and display to user.
* socket will return an InputStream to DataInputStream and then DataInputStream
* will read the message as String and print it on console.
*/
new Thread(new Runnable(){
public void run(){
DataInputStream dis = null;
try{
dis = new DataInputStream(socket.getInputStream());
} catch(IOException ex){
System.err.println(ex);
}
String message = "";
while(!socket.isInputShutdown() || !socket.isClosed()){
try {
message = dis.readUTF();
if(message.equalsIgnoreCase("exit")){
System.out.println(SENDER+"Exited");
socket.shutdownInput();
socket.shutdownOutput();
socket.close();
}
else
System.out.println(SENDER+message);
} catch (IOException ex) {
System.err.println(ex);
}
}
}
}).start();
/**
* DataOutputStream will write by socket OutputStream while socket is opened.
* Scanner will take the message from console and pass it to DataOutputStream
* Connection will shutdown after any of user type 'exit' (ignore case).
*/
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
String yourMessage = "";
while(!socket.isOutputShutdown() || !socket.isClosed()){
yourMessage = scan.nextLine();
if(yourMessage.equalsIgnoreCase("exit")){
System.out.println("You are going to shut down this connection");
dos.writeUTF(yourMessage);
socket.shutdownOutput();
socket.shutdownInput();
break;
}
else
dos.writeUTF(yourMessage);
}
dos.flush();
dos.close();
socket.close();
} catch(IOException ex){
System.err.println(ex);
}
}
}