3 /* Copyright (C) 1999 Cygnus Solutions
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
12 * @author Per Bothner <bothner@cygnus.com>
13 * @date January 6, 1999.
16 /** Written using on-line Java Platform 1.2 API Specification.
17 * Status: I believe all methods are implemented.
25 static SocketImplFactory factory
;
32 protected Socket (SocketImpl impl
) throws SocketException
37 public Socket (String host
, int port
)
38 throws UnknownHostException
, IOException
40 this(factory
== null ?
new PlainSocketImpl() : factory
.createSocketImpl());
41 SecurityManager s
= System
.getSecurityManager();
43 s
.checkConnect(host
, port
);
45 impl
.connect(host
, port
);
48 public Socket (InetAddress address
, int port
)
51 this(factory
== null ?
new PlainSocketImpl() : factory
.createSocketImpl());
52 SecurityManager s
= System
.getSecurityManager();
54 s
.checkConnect(address
.getHostName(), port
);
56 impl
.connect(address
, port
);
59 public Socket (String host
, int port
,
60 InetAddress localAddr
, int localPort
) throws IOException
62 this(factory
== null ?
new PlainSocketImpl() : factory
.createSocketImpl());
63 SecurityManager s
= System
.getSecurityManager();
65 s
.checkConnect(host
, port
);
67 impl
.bind(localAddr
, localPort
);
68 impl
.connect(host
, port
);
71 public Socket (InetAddress address
, int port
,
72 InetAddress localAddr
, int localPort
) throws IOException
74 this(factory
== null ?
new PlainSocketImpl() : factory
.createSocketImpl());
75 SecurityManager s
= System
.getSecurityManager();
77 s
.checkConnect(address
.getHostName(), port
);
79 impl
.bind(localAddr
, localPort
);
80 impl
.connect(address
, port
);
84 * @deprecated Use DatagramSocket instead for UDP transport.
86 public Socket (String host
, int port
, boolean stream
) throws IOException
88 impl
= factory
== null ?
new PlainSocketImpl()
89 : factory
.createSocketImpl();
91 SecurityManager s
= System
.getSecurityManager();
93 s
.checkConnect(host
, port
);
94 impl
.connect(host
, port
);
98 * @deprecated Use DatagramSocket instead for UDP transport.
100 public Socket (InetAddress host
, int port
, boolean stream
) throws IOException
102 impl
= factory
== null ?
new PlainSocketImpl()
103 : factory
.createSocketImpl();
105 SecurityManager s
= System
.getSecurityManager();
107 s
.checkConnect(host
.getHostName(), port
);
108 impl
.connect(host
, port
);
111 public InetAddress
getInetAddress ()
113 return impl
.getInetAddress();
116 public InetAddress
getLocalAddress ()
118 InetAddress localAddress
;
121 localAddress
= (InetAddress
)impl
.getOption(SocketOptions
.SO_BINDADDR
);
123 catch (SocketException x
)
125 // (hopefully) shouldn't happen
126 System
.err
.println(x
);
127 throw new java
.lang
.InternalError("Error in PlainSocketImpl.getOption");
132 public int getPort ()
134 return impl
.getPort();
137 public int getLocalPort ()
139 return impl
.getLocalPort();
142 public InputStream
getInputStream () throws IOException
144 return impl
.getInputStream();
147 public OutputStream
getOutputStream () throws IOException
149 return impl
.getOutputStream();
152 public void setTcpNoDelay (boolean on
) throws SocketException
154 impl
.setOption( SocketOptions
.TCP_NODELAY
, new Boolean(on
) );
157 public boolean getTcpNoDelay() throws SocketException
159 Boolean bool
= (Boolean
)impl
.getOption( SocketOptions
.TCP_NODELAY
);
160 return bool
.booleanValue();
163 public void setSoLinger(boolean on
, int linger
) throws SocketException
165 if ( on
&& (linger
>= 0) )
169 impl
.setOption( SocketOptions
.SO_LINGER
, new Integer(linger
) );
171 else if ( on
&& (linger
< 0) )
172 throw new IllegalArgumentException("SO_LINGER must be >= 0");
174 impl
.setOption( SocketOptions
.SO_LINGER
, new Boolean(false) );
177 public int getSoLinger() throws SocketException
179 Object linger
= impl
.getOption(SocketOptions
.SO_LINGER
);
180 if (linger
instanceof Integer
)
181 return ((Integer
)linger
).intValue();
186 public synchronized void setSoTimeout (int timeout
) throws SocketException
189 throw new IllegalArgumentException("Invalid timeout: " + timeout
);
191 impl
.setOption(SocketOptions
.SO_TIMEOUT
, new Integer(timeout
));
194 public synchronized int getSoTimeout () throws SocketException
196 Object timeout
= impl
.getOption(SocketOptions
.SO_TIMEOUT
);
197 if (timeout
instanceof Integer
)
198 return ((Integer
)timeout
).intValue();
204 public void setSendBufferSize (int size
) throws SocketException
207 throw new IllegalArgumentException("Invalid buffer size: " + size
);
209 impl
.setOption(SocketOptions
.SO_SNDBUF
, new Integer(size
));
213 public int getSendBufferSize () throws SocketException
215 Integer buf
= (Integer
)impl
.getOption(SocketOptions
.SO_SNDBUF
);
216 return buf
.intValue();
220 public void setReceiveBufferSize (int size
) throws SocketException
223 throw new IllegalArgumentException("Invalid buffer size: " + size
);
225 impl
.setOption(SocketOptions
.SO_RCVBUF
, new Integer(size
));
229 public int getReceiveBufferSize () throws SocketException
231 Integer buf
= (Integer
)impl
.getOption(SocketOptions
.SO_RCVBUF
);
232 return buf
.intValue();
235 public synchronized void close () throws IOException
240 public String
toString ()
242 return "Socket" + impl
.toString();
245 public static synchronized void setSocketImplFactory (SocketImplFactory fac
)