Overview
The article will demonstrate how to use the ipclient component in Secure iNet Factory
to create SSL/TLS TCP/IP client socket connections.
Code Example
Download IPClientSSLEx.java
01 import java.io.*;
02
03 import com.jscape.inet.ipclient.IpClientConnectedEvent;
04 import com.jscape.inet.ipclient.IpClientDisconnectedEvent;
05 import com.jscape.inet.ipclient.IpClientListener;
06 import com.jscape.inet.ipclientssl.IpClientSsl;
07
08
09 public class IPClientSSLEx implements IpClientListener
10 {
11 public static void main(String[] args)
12 {
13 try
14 {
15 String hostname = "hostname";
16 int port = -1;
17 // create new IpClientSsl instance
18 IpClientSsl ssl = new IpClientSsl(hostname,port);
19
20 // establish a connection
21 ssl.connect();
22
23 // get input stream
24 InputStream in = ssl.getInputStream();
25
26 // read data from input stream
27 int i = 0;
28 while((i = in.read()) != -1) {
29 // do something with data
30 }
31
32 // get output stream for tunnel
33 OutputStream out = ssl.getOutputStream();
34 // write data to output stream
35 byte[] buffer = new byte[1024];
36 out.write(buffer);
37
38 ssl.disconnect();
39 }
40 catch (Exception e)
41 {
42 e.printStackTrace();
43 }
44 }
45
46 public void connected(IpClientConnectedEvent event)
47 {
48 System.out.println("Connected to host: " + event.getHostname());
49 }
50
51 public void disconnected(IpClientDisconnectedEvent event)
52 {
53 System.out.println("Disconnected from host: " + event.getHostname());
54 }
55 }
Lines 1-6 : Necessary import statements. Line 18 : Create IpClientSsl instance. Line 21 : Connect to the host. Lines 23-32 : Read the data from the ssl tunnel. Line 38 : Disconnect from the host.
|
Comments