Listening for events

Top  Previous  Next

For an object to listen for events published by the IpClient class the following steps are required:

 

1. Set object to implement IpClientListener

2. Overload event handling methods.

3. Subscribe object to receive events published by IpClient instance.

 

Example

 

The example below demonstrates using the IpClientListener class.

 

import com.jscape.inet.ipclient.*;

import java.io.*;

 

public class MyIpClientListener implements IpClientListener {

 

 public void connected(IpClientConnectedEvent event) {

         System.out.println("Connected to host: " + event.getHostname());

 }

 

 public void disconnected(IpClientDisconnectedEvent event) {

         System.out.println("Disconnected from host: " + event.getHostname());

 }

 

 public static void main(String[] args) {

         try {

                 // create new IpClient instance

                 IpClient client = new IpClient("www.yahoo.com",80);

                 

                 // subscribe listener

                 client.addIpClientListener(new MyIpClientListener());

                 

                 // establish connection

                 client.connect();

                 

                 // get output stream

                 OutputStream out = client.getOutputStream();

                 

                 // send data

                 String command = "GET / HTTP/1.0\r\n\r\n";

                 out.write(command.getBytes());

                 out.flush();

                 

                 // get input stream

                 InputStream in = client.getInputStream();

                                 

                 // read data from server

                 int i = 0;

                 while((i = in.read()) != -1) {

                         System.out.print((char)i);

                 }                

                 

                 // disconnect

                 client.disconnect();

                 

         } catch(Exception e) {

                 e.printStackTrace();

         }

 }

 

 

}

 

 

 





Home | Company | Products | Solutions | Purchase | Support | Services | Blog

© 2021 JSCAPE LLC