More classes for galois
[IRC.git] / Robust / src / ClassLibrary / MGC / Socket.java
1 public class Socket {
2   /* File Descriptor */
3   int fd;
4   SocketInputStream sin;
5   SocketOutputStream sout;
6
7   public Socket() {
8     sin=new SocketInputStream(this);
9     sout=new SocketOutputStream(this);
10   }
11
12   public InputStream getInputStream() {
13     return sin;
14   }
15
16   public OutputStream getOutputStream() {
17     return sout;
18   }
19
20   public Socket(String host, int port) {
21     InetAddress address=InetAddress.getByName(host);
22     fd=nativeBind(address.getAddress(), port);
23     nativeConnect(fd, address.getAddress(), port);
24     sin=new SocketInputStream(this);
25     sout=new SocketOutputStream(this);
26   }
27
28   public Socket(InetAddress address, int port) {
29     fd=nativeBind(address.getAddress(), port);
30     nativeConnect(fd, address.getAddress(), port);
31     sin=new SocketInputStream(this);
32     sout=new SocketOutputStream(this);
33   }
34
35         public int connect(String host, int port) {
36     InetAddress address=InetAddress.getByName(host);
37                 if (address != null) {
38                         fd=nativeBind(address.getAddress(), port);
39                         nativeConnect(fd, address.getAddress(), port);
40                         return 0;
41                 }
42                 else {
43                         return -1;
44                 }
45         }
46
47   public static native int nativeBind(byte[] address, int port);
48
49   public static native int nativeConnect(int fd, byte[] address, int port);
50
51   int setFD(int filed) {
52     fd=filed;
53   }
54
55   public int read(byte[] b) {
56     return nativeRead(b);
57   }
58   public int write(byte[] b) {
59     nativeWrite(b, 0, b.length);
60     if(fd==-1) {
61       System.out.println("here: " + "fd= " + fd);
62       return -1;
63     } else { 
64       return 0;
65     }
66   }
67
68   public int write(byte[] b, int offset, int len) {
69     nativeWrite(b, offset, len);
70     if(fd==-1)
71       return -1;
72     else 
73       return 0;
74   }
75
76   private native int nativeRead(byte[] b);
77   private native void nativeWrite(byte[] b, int offset, int len);
78   private native void nativeClose();
79
80   public void close() {
81     nativeClose();
82   }
83 }