View Javadoc
1   /*
2    * junixsocket
3    *
4    * Copyright 2009-2024 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  import java.nio.charset.StandardCharsets;
28  
29  import org.newsclub.net.unix.AFUNIXSocket;
30  import org.newsclub.net.unix.AFUNIXSocketAddress;
31  
32  /**
33   * A simple demo client.
34   *
35   * Reads a server greeting string, then sends a "Hello Server" string as a response.
36   *
37   * Finally, reads integers (via {@link DataInputStream}), then sends twice the sent value each,
38   * unless a "-123" magic number is read to indicate the end of the conversation.
39   *
40   * @author Christian Kohlschütter
41   * @see SimpleTestServer
42   */
43  public class SimpleTestClient {
44    public static void main(String[] args) throws IOException {
45      final File socketFile = new File(new File(System.getProperty("java.io.tmpdir")),
46          "junixsocket-test.sock");
47  
48      boolean connected = false;
49      try (AFUNIXSocket sock = AFUNIXSocket.connectTo(AFUNIXSocketAddress.of(socketFile));
50          InputStream is = sock.getInputStream(); //
51          OutputStream os = sock.getOutputStream();
52          DataInputStream din = new DataInputStream(is);
53          DataOutputStream dout = new DataOutputStream(os);) {
54        System.out.println("Connected");
55        connected = true;
56  
57        byte[] buf = new byte[128];
58  
59        int read = is.read(buf);
60        System.out.println("Server says: " + new String(buf, 0, read, StandardCharsets.UTF_8));
61  
62        System.out.println("Replying to server...");
63        os.write("Hello Server".getBytes(StandardCharsets.UTF_8));
64        os.flush();
65  
66        System.out.println("Now reading numbers from the server...");
67        while (!Thread.interrupted()) {
68          int number = din.readInt();
69          if (number == -123) {
70            // by convention of this demo, if the number is -123, we stop.
71            // If we don't do this, we'll get an EOFException upon the next unsuccessful read.
72            break;
73          }
74          System.out.println(number);
75  
76          int ourNumber = number * 2;
77  
78          System.out.println("Sending back " + ourNumber);
79          dout.writeInt(ourNumber);
80        }
81      } catch (SocketException e) {
82        if (!connected) {
83          System.out.println("Cannot connect to server. Have you started it?");
84          System.out.println();
85        }
86        throw e;
87      }
88  
89      System.out.println("End of communication.");
90    }
91  }