File: socket.java

package info (click to toggle)
libaria 2.8.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 13,628 kB
  • ctags: 16,574
  • sloc: cpp: 135,490; makefile: 925; python: 597; java: 570; ansic: 182
file content (54 lines) | stat: -rw-r--r-- 2,044 bytes parent folder | download | duplicates (2)
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


import com.mobilerobots.Aria.*;

public class socket {

  static {
    try {
        System.loadLibrary("AriaJava");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library libAriaJava failed to load. Make sure that its directory is in your library path; See javaExamples/README.txt and the chapter on Dynamic Linking Problems in the SWIG Java documentation (http://www.swig.org) for help.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[]) {
    Aria.init();
    ArSocket s1 = new ArSocket();         // server socket. accepts clients.
    ArSocket s2 = new ArSocket();         // client
    ArSocket s1client = new ArSocket();   // used by server to communicate with client
    if (!s1.open(6789, ArSocket.Type.TCP)) {
      System.err.println("Error opening first TCP socket on port 6789.");
      Aria.exit(1);
    }
    System.out.println("First socket opened and listening on port 6789.");
    System.out.println("Connecting second socket to localhost port 6789...");
    if (!s2.connect("localhost", 6789)) {
      System.err.println("Error connecting second TCP socket to local host port 6789");
      Aria.exit(2);
    }
    if (!s1.accept(s1client)) {
      System.err.println("Error accepting client on first socket.");
      Aria.exit(3);
    }
    System.out.println("Connected.");
    System.out.println("Second socket is sending \"hello\" to first...");
    s2.write("hello");
    String text = new String("");
    text = s1client.read(512, 5000); // arguments are buffer size and timeout to wait
    /*
    while(text.length() == 0)
    {
      text = s1client.read(512, 5000); // arguments are buffer size and timeout to wait
    }
    */
    System.out.println("=> First socket recieved \""+text+"\" ("+text.length()+")");
    System.out.println("First socket is sending \"hello\" to second...");
    s1client.write("hello");
    text = s2.read(512, 5000);
    System.out.println("=> Second socket recieved \""+text+"\"");
    s2.close();
    Aria.exit(0);
  }
}