Собственно вопрос понятен из темы.
Есть клиент и сервер - нужно протестировать с помощью jUnit Socket-соединение, ну или если у кого есть другие примеры то все-равно с помощью чего, главное понять принцип как это сделать.
В инете нашел материал, но что-то под свой пример заточить не смог.
Спасибо за подсказки.
Client
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.
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) throws UnknownHostException,
IOException {
new Client();
}
private Socket clientSocket;
private InputStream inputStream;
private OutputStream outputStream;
private DataInputStream dataInputStream;
private DataOutputStream dataOutputStream;
private final String hostName = "localhost";
private final int PORT = 6700;
private BufferedReader bufReader;
private String requestMessage = null;
private String responseMessage = null;
public Client() {
System.out.println("-CLIENT-");
try {
this.clientSocket = new Socket(this.hostName, this.PORT);
this.bufReader = new BufferedReader(
new InputStreamReader(System.in));
this.inputStream = clientSocket.getInputStream();
this.outputStream = clientSocket.getOutputStream();
this.dataInputStream = new DataInputStream(inputStream);
this.dataOutputStream = new DataOutputStream(outputStream);
do {
this.requestMessage = bufReader.readLine();
sendMessage(this.requestMessage);
this.responseMessage = dataInputStream.readUTF();
System.out.println(this.responseMessage);
} while ((!(this.requestMessage.compareTo("quit") == 0)));
System.out.println("ClientStopped!");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
this.inputStream.close();
this.outputStream.close();
this.clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void sendMessage(String msg) {
try {
this.dataOutputStream.writeUTF(msg);
this.dataOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Server
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.
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
new Server();
}
private ServerSocket serverSocket;
private Socket connectionSocket;
private InputStream inputStream;
private OutputStream outputStream;
private DataInputStream dataInputStream;
private DataOutputStream dataOutputStream;
private final int PORT = 6700;
private int parkPlatzCounter = 5;
private String responseMessage = null;
public Server() {
System.out.println("-SERVER-");
try {
this.serverSocket = new ServerSocket(this.PORT);
this.connectionSocket = serverSocket.accept();
this.inputStream = connectionSocket.getInputStream();
this.outputStream = connectionSocket.getOutputStream();
this.dataInputStream = new DataInputStream(inputStream);
this.dataOutputStream = new DataOutputStream(outputStream);
do {
this.responseMessage = this.dataInputStream.readUTF();
if (this.responseMessage.compareTo("quit") == 0) {
sendMessage("Server Stopped!");
} else if (this.responseMessage.compareTo("in") == 0) {
autoEin();
} else if (this.responseMessage.compareTo("out") == 0) {
autoAus();
} else if (this.responseMessage.compareTo("free") == 0) {
free();
} else {
sendMessage("---Falsche Eingabe!---");
}
} while ((!(this.responseMessage.compareTo("quit") == 0)));
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
this.inputStream.close();
this.outputStream.close();
this.serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void free() {
sendMessage("> " + this.parkPlatzCounter);
}
private void autoEin() {
if (this.parkPlatzCounter > 0) {
this.parkPlatzCounter--;
sendMessage("> ok");
} else {
sendMessage("> fail");
}
}
private void autoAus() {
if (this.parkPlatzCounter < 5) {
this.parkPlatzCounter++;
sendMessage("> ok");
} else {
sendMessage("> fail");
}
}
private void sendMessage(String msg) {
try {
this.dataOutputStream.writeUTF(msg);
this.dataOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}