package
testing.i2p;
import
java.io.BufferedReader;
import
java.io.BufferedWriter;
import
java.io.IOException;
import
java.io.InputStreamReader;
import
java.io.InterruptedIOException;
import
java.io.OutputStream;
import
java.io.OutputStreamWriter;
import
java.net.ConnectException;
import
java.net.NoRouteToHostException;
import
net.i2p.I2PException;
import
net.i2p.client.streaming.I2PSocket;
import
net.i2p.client.streaming.I2PSocketManager;
import
net.i2p.client.streaming.I2PSocketManagerFactory;
import
net.i2p.data.DataFormatException;
import
net.i2p.data.Destination;
public
class
Client {
public
static
void
main(String[] args) {
I2PSocketManager manager =
I2PSocketManagerFactory.createManager();
System.out.println(
"Destination:"
);
BufferedReader br =
new
BufferedReader(
new
InputStreamReader(System.in));
String destinationString;
try
{
destinationString = br.readLine();
}
catch
(IOException ex) {
System.out.println(
"Failed to get the Destination."
);
return
;
}
Destination destination;
try
{
destination =
new
Destination(destinationString);
}
catch
(DataFormatException ex) {
System.out.println(
"Destination string incorrectly formatted."
);
return
;
}
I2PSocket socket;
try
{
socket = manager.connect(destination);
}
catch
(I2PException ex) {
System.out.println(
"General I2P exception occurred!"
);
return
;
}
catch
(ConnectException ex) {
System.out.println(
"Failed to connect!"
);
return
;
}
catch
(NoRouteToHostException ex) {
System.out.println(
"Couldn't find host!"
);
return
;
}
catch
(InterruptedIOException ex) {
System.out.println(
"Sending/receiving was interrupted!"
);
return
;
}
try
{
BufferedWriter bw =
new
BufferedWriter(
new
OutputStreamWriter(socket.getOutputStream()));
bw.write(
"Hello I2P!
"
);
BufferedReader br2 =
new
BufferedReader(
new
InputStreamReader(socket.getInputStream()));
String s =
null
;
while
((s = br2.readLine()) !=
null
) {
System.out.println(
"Received from server: "
+ s);
}
socket.close();
}
catch
(IOException ex) {
System.out.println("Error occurred
while
sending/receiving!");
}
}
}