Package com.germancoding.packetapi.udp
Class UnreliableSocket
- java.lang.Object
-
- com.germancoding.packetapi.udp.UnreliableSocket
-
public class UnreliableSocket extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field Description static int
MAX_PACKET_SIZE
-
Constructor Summary
Constructors Constructor Description UnreliableSocket(java.net.DatagramSocket socket)
Creates a new UnreliableSocket with the givensocket
as the underlying socket.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes the underlying socket.void
connect(java.net.InetAddress address, int port)
"Connects" (Remember, UDP is a connection-less protocol) the socket to another socket.java.io.InputStream
getInputStream()
java.io.OutputStream
getOutputStream()
java.net.InetAddress
getRemoteAddress()
int
getRemotePort()
java.net.DatagramSocket
getSocket()
void
setRemoteAddress(java.net.InetAddress remoteAddress)
void
setRemotePort(int remotePort)
-
-
-
Field Detail
-
MAX_PACKET_SIZE
public static final int MAX_PACKET_SIZE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
UnreliableSocket
public UnreliableSocket(java.net.DatagramSocket socket) throws java.net.SocketException
Creates a new UnreliableSocket with the givensocket
as the underlying socket. The socket can be in any state (connected, data flow already started...) as long as it isn't closed.
After calling this constructor, you should call the connect() method if the underlying socket isn't already connected
If necessary, the receiver buffer size of the underlying socket is changed to receive large packets. ASocketException
is thrown if this fails.- Parameters:
socket
- The underlying socket where data is send and received.- Throws:
java.net.SocketException
- If buffer resizing fails
-
-
Method Detail
-
connect
public void connect(java.net.InetAddress address, int port)
"Connects" (Remember, UDP is a connection-less protocol) the socket to another socket. This method doesn't send any data to the given socket, it just remembers the given values.
You HAVE TO call this method if you want to send data (over the OutputStream provided by this socket).
Once connected, the InputStream will only accept data received from this remote address.
This method doesn't call the connect() methods of the underlying socket. If you call the connect() methods of the underlying socket manually, don't forget to call the connect() method of this class!
It's possible to call the connect() method multiple times, sender and receiver will immediatly update- Parameters:
address
- Remote address to rememberport
- Remote port to remember
-
getSocket
public java.net.DatagramSocket getSocket()
- Returns:
- The underlying socket where data is actually send and received
-
close
public void close() throws java.io.IOException
Closes the underlying socket.- Throws:
java.io.IOException
- When closing of a stream fails. Should never happen.
-
getRemoteAddress
public java.net.InetAddress getRemoteAddress()
- Returns:
- The remote address to which this socket is connected (Set by the connect() method).
-
setRemoteAddress
public void setRemoteAddress(java.net.InetAddress remoteAddress)
- See Also:
connect(InetAddress, int)
-
getRemotePort
public int getRemotePort()
- Returns:
- The remote port to which this socket is connected (Set by the connect() method).
-
setRemotePort
public void setRemotePort(int remotePort)
- See Also:
connect(InetAddress, int)
-
getOutputStream
public java.io.OutputStream getOutputStream()
- Returns:
- An OutputStream provided by this instance. The OutputStream writes all data into UDP packets and sends them over the underlying socket. As usual in UDP, packets can be lost or received out of order.
Note: Call the connect() method first before using this OutputStream. Always call the flush() method, otherwise data is only buffered!
-
getInputStream
public java.io.InputStream getInputStream()
- Returns:
- An InputStream provided by this instance. The InputStream reads data from UDP packets which are received from the underlying socket. As usual in UDP, packets can be lost or received out of order.
Note: If connected, the InputStream will only accept data send from the connected remote address.
-
-