Repository navigation
Expand file tree
/
Copy pathServer.java
More file actions
109 lines (96 loc) · 4.06 KB
/
Copy pathServer.java
File metadata and controls
109 lines (96 loc) · 4.06 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package networking.p2p;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
/**
*
* @author rafiul islam
*/
public class Server {
/**
* Server class will create a SocketServer with a defined port.
* To connect with this server, Client must use this same port.
* After client send a request to this server to make a P2P connection,
* server must accept the request before any communication between them.
* After Server accept the request a Socket will return by the system.
* Then Server's Socket and Client's Socket can communicate.
* @see networking.p2p.Client
*
*/
private static final int PORT = 6789;
private static final int MAX_WAIT = 100000; // sec
private static final String SENDER = "CLIENT: ";
public static void main(String[] args) {
/**
* Scanner for input user message from console.
*/
Scanner scan = new Scanner(System.in);
try{
/**
* A ServerSocket is created with a defined port and set maximum time for
* routing client.
* When server accept a client, it will send a greeting message via OutputStream.
*/
ServerSocket ss = new ServerSocket(PORT);
ss.setSoTimeout(MAX_WAIT);
System.out.println("Server will waiting 100 sec for client with port "+PORT);
Socket socket = ss.accept();
System.out.println("Server is connected with "+socket.getRemoteSocketAddress());
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF("Welcome !");
/**
* 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();
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();
ss.close();
} catch(IOException ex){
System.err.println(ex);
}
}
}