Debian Codegraph @

junixsocket-demo/src/main/java/org/newsclub/net/unix/demo/SimpleTestClient.java

raw
1 /*
2 * junixsocket
3 *
4 * Copyright 2009-2022 Christian Kohlschütter
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.newsclub.net.unix.demo;
19
20 import java.io.DataInputStream;
21 import java.io.DataOutputStream;
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.net.SocketException;
27
28 import org.newsclub.net.unix.AFUNIXSocket;
29 import org.newsclub.net.unix.AFUNIXSocketAddress;
30
31 /**
32 * A simple demo client.
33 *
34 * Reads a server greeting string, then sends a "Hello Server" string as a response.
35 *
36 * Finally, reads integers (via {@link DataInputStream}), then sends twice the sent value each,
37 * unless a "-123" magic number is read to indicate the end of the conversation.
38 *
39 * @author Christian Kohlschütter
40 * @see SimpleTestServer
41 */
42 public class SimpleTestClient {
43 public static void main(String[] args) throws IOException {
44 final File socketFile = new File(new File(System.getProperty("java.io.tmpdir")),
45 "junixsocket-test.sock");
46
47 boolean connected = false;
48 try (AFUNIXSocket sock = AFUNIXSocket.connectTo(AFUNIXSocketAddress.of(socketFile));
49 InputStream is = sock.getInputStream(); //
50 OutputStream os = sock.getOutputStream();
51 DataInputStream din = new DataInputStream(is);
52 DataOutputStream dout = new DataOutputStream(os);) {
53 System.out.println("Connected");
54 connected = true;
55
56 byte[] buf = new byte[128];
57
58 int read = is.read(buf);
59 System.out.println("Server says: " + new String(buf, 0, read, "UTF-8"));
60
61 System.out.println("Replying to server...");
62 os.write("Hello Server".getBytes("UTF-8"));
63 os.flush();
64
65 System.out.println("Now reading numbers from the server...");
66 while (!Thread.interrupted()) {
67 int number = din.readInt();
68 if (number == -123) {
69 // by convention of this demo, if the number is -123, we stop.
70 // If we don't do this, we'll get an EOFException upon the next unsuccessful read.
71 break;
72 }
73 System.out.println(number);
74
75 int ourNumber = number * 2;
76
77 System.out.println("Sending back " + ourNumber);
78 dout.writeInt(ourNumber);
79 }
80 } catch (SocketException e) {
81 if (!connected) {
82 System.out.println("Cannot connect to server. Have you started it?");
83 System.out.println();
84 }
85 throw e;
86 }
87
88 System.out.println("End of communication.");
89 }
90 }