-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestClass.java
More file actions
121 lines (121 loc) · 4.5 KB
/
TestClass.java
File metadata and controls
121 lines (121 loc) · 4.5 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
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.DataInputStream;
import ru.geekbrains.chat.network.TCPConnection;
import ru.geekbrains.chat.network.TCPConnectionListener;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
public class TestClass {
public String getInfo(){
return "This text of class.";
}
}
public class NewServlet extends HttpServlet {
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
}
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
String name = request.getParameter("name");
String surname = request.getParameter("surname");
PrintWriter pw = response.getWriter();
pw.println("<html>");
pw.println("<h1> HELLO, " + name + " " + surname + " </h1>");
pw.println("</html>");
RequestDispatcher dispatcher = request.getRequestDispatcher("/testJsp.jsp");
dispatcher.forward(request, response);
}
}
public class TestASClient {
public static void main(String[] args) throws InterruptedException {
try(Socket socket = new Socket("localhost", 3345);
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
DataOutputStream oos = new DataOutputStream(socket.getOutputStream());
DataInputStream ois = new DataInputStream(socket.getInputStream()); )
{
System.out.println("Client connected to socke.");
System.out.println();
System.out.println("Client writing channel = oos & reading channel = ois initialized.");
while(!socket.isOutputShutdown()){
if(br.ready()){
System.out.println("Client start writing in channel...");
Thread.sleep(1000);
String clientCommand = br.readLine();
oos.writeUTF(clientCommand);
oos.flush();
System.out.println("Clien sent message " + clientCommand + " to server.");
Thread.sleep(1000);
if(clientCommand.equalsIgnoreCase("quit")){
System.out.println("Client kill connections");
Thread.sleep(2000);
if(ois.available()!=0) {
System.out.println("reading...");
String in = ois.readUTF();
System.out.println(in);
}
break;
}
System.out.println("Client wrote & start waiting for data from server...");
Thread.sleep(2000);
if(ois.available()!=0) {
System.out.println("reading...");
String in = ois.readUTF();
System.out.println(in);
}
}
}
System.out.println("Closing connections & channels on clentSide - DONE.");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class ChatServer implements TCPConnectionListener {
public static void main(String[] args) {
new ChatServer();
}
private final ArrayList<TCPConnection> connections = new ArrayList<>();
private ChatServer() {
System.out.println("Server running...");
try (ServerSocket serverSocket = new ServerSocket(8080)) {
while (true) {
try {
new TCPConnection(this, serverSocket.accept());
} catch (IOException e) {
System.out.println("TCPConnection exception: " + e);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void onConnectionReady(TCPConnection tcpConnection) {
connections.add(tcpConnection);
sendToAllConnections("Client connection: " + tcpConnection);
}
@Override
public void onReceiveString(TCPConnection tcpConnection, String value) {
sendToAllConnections(value);
}
@Override
public void onDisconnect(TCPConnection tcpConnection) {
connections.remove(tcpConnection);
sendToAllConnections("Client disconnection: " + tcpConnection);
}
@Override
public void onException(TCPConnection tcpConnection, Exception e) {
System.out.println("TCPConnection exception: " + e);
}
private void sendToAllConnections(String value) {
System.out.println(value);
final int cos = connections.size();
for (int i = 0; i < cos; i++) {
connections.get(0).sendString(value);
}
}
}