-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
184 lines (159 loc) · 7.15 KB
/
Server.java
File metadata and controls
184 lines (159 loc) · 7.15 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Server {
ArrayList clientOutputStreams;
ArrayList <String>servSocket = new ArrayList<>(); //info about clients
Map<String, String> h1 = new HashMap<String, String>();
String msgFromClient;
BufferedReader inFromClient = null;
Socket connectedSocket;
String allNewsHere = "NEWS! : ";
public class SocketDispatcher implements Runnable {
Socket connectedSocket;
public SocketDispatcher(Socket connectedSocket) {
this.connectedSocket = connectedSocket;
}
@Override
public void run(){
try {
inFromClient = new BufferedReader(new InputStreamReader(connectedSocket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
try {
PrintWriter outToClient1 = new PrintWriter(connectedSocket.getOutputStream(), true);
while ((msgFromClient = inFromClient.readLine()) != null) {
System.out.println("Client send message: " + msgFromClient);
if(msgFromClient.matches("echo")) {
outToClient1.println("Write any sentence to echo!");
if((msgFromClient = inFromClient.readLine())!=null)
tellEveryone(msgFromClient);
else
outToClient1.println("no symbols");
}
else if(msgFromClient.matches("info")){
outToClient1.println(getInfoAboutClients());
}
else if(msgFromClient.matches("getNews")){
if(allNewsHere.equals(""))
outToClient1.println("no news");
else
outToClient1.println(allNewsHere);
}
else if(msgFromClient.matches("setNews")){
outToClient1.println("Write your news please!");
if((msgFromClient = inFromClient.readLine())!=null)
{
tellEveryone("News changed");
allNewsHere += " " + msgFromClient;
}
}
else if(msgFromClient.matches("loginMe")){
outToClient1.println("Write login!");
String userLogin, userPassword;
if ((msgFromClient = inFromClient.readLine())!=null){
userLogin = msgFromClient;
outToClient1.println("Write password!");
if ((msgFromClient = inFromClient.readLine())!=null){
userPassword = msgFromClient;
if( h1.containsKey(userLogin) && h1.containsValue(userPassword)){
outToClient1.println("You are welcome to enter the system as user!");
}
else{
outToClient1.println("You don't have login or password, want to add? (y,n)");
if((msgFromClient = inFromClient.readLine())!=null){
if(msgFromClient.equals("y")){
outToClient1.println("Write login!");
if((msgFromClient = inFromClient.readLine())!=null){
userLogin = msgFromClient;
outToClient1.println("Write password!");
if((msgFromClient = inFromClient.readLine())!=null){
userPassword = msgFromClient;
h1.put(userLogin,userPassword);
outToClient1.println("added succesfully");
}
}
}
}
}
}
}
}
else{
outToClient1.println("No usages, your sentence is " + msgFromClient + ", Please use echo, info, getNews, setNews, loginMe instead");
}
System.out.println("Writed " + msgFromClient + " to client");
}
} catch (IOException e) {
e.printStackTrace();
}
try {
inFromClient.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
connectedSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void start() throws IOException {
clientOutputStreams = new ArrayList();
ServerSocket serverSocket = new ServerSocket(5555);
while (true) {
connectedSocket = serverSocket.accept();
PrintWriter outToClient = new PrintWriter(connectedSocket.getOutputStream(),true);
System.out.println("Connected " + connectedSocket.getInetAddress() + " " + connectedSocket.getPort() + " " + connectedSocket.getTrafficClass());
clientOutputStreams.add(outToClient);
String s1 = "ip = " + connectedSocket.getInetAddress().toString() + " port = " + connectedSocket.getPort() + " traffic class = " + connectedSocket.getTrafficClass() + " ";
if(!s1.equals(""))
servSocket.add(s1);
else
servSocket.add("none");
new Thread(new SocketDispatcher(connectedSocket)).start();
}
}
public String getInfoAboutClients() {
Iterator it = servSocket.iterator();
String s = "";
while (it.hasNext()){
s+= it.next();
}
return s;
}
public void tellEveryone(String message) {
Iterator it = clientOutputStreams.iterator();
while (it.hasNext()) {
try {
PrintWriter outToClient = (PrintWriter)it.next();
outToClient.println(message);
outToClient.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String []argv){
Server s = new Server();
System.out.print("Server started " + LocalTime.now() + "\n");
try {
s.start();
} catch (IOException e) {
System.out.print("Connection reset");
}
}
}