sasappclient.java.txt
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:2k
源码类别:

SNMP编程

开发平台:

C/C++

  1. /* $Id: sasappclient.java,v 1.3 2002/09/09 05:39:09 parasuraman Exp $ */
  2. /*
  3.  * @(#)sasappclient.java
  4.  * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
  5.  * Please read the associated COPYRIGHTS file for more details.
  6.  */
  7. /**
  8.   * This Applet invokes the  SASClient userSyncSend() method
  9.   * to send a datas to the Server to perform some operation.
  10.   * It receives the response data and print it.
  11.   * It sends and receives the data continuously.
  12. */
  13. import java.applet.*;
  14. import java.lang.*;
  15. import java.awt.*;
  16. import java.awt.event.*;
  17. import com.adventnet.snmp.snmp2.*;
  18. public class sasappclient extends Applet implements ActionListener {
  19. TextArea text;
  20. TextField field;
  21. SASClient sasclient;
  22. public void init() {
  23. text = new TextArea( 10, 40);
  24. text.setEditable(false);
  25. Panel northPanel = new Panel();
  26. northPanel.add(new Label("Enter Message to Send: "));
  27. northPanel.add(field = new TextField(20));
  28. Panel southPanel = new Panel();
  29. Button sendButton = new Button("Send");
  30. southPanel.add(sendButton);
  31. sendButton.addActionListener(this);
  32. Button clearButton = new Button("Clear");
  33. southPanel.add(clearButton);
  34. clearButton.addActionListener(this);
  35. setLayout(new BorderLayout());
  36. add("North", northPanel);
  37. add("Center", text);
  38. add("South", southPanel);
  39. initSnmp();
  40. }
  41. void initSnmp() {
  42. // establish connection with the SAServer
  43. try {
  44. sasclient = new SASClient(this, true);
  45. sasclient.start();
  46. }
  47. catch (Exception e) {
  48. text.append("Error : " + e.getMessage() + "n");
  49. }
  50. }
  51. void sendData(String msg) {
  52. // Now let us use the UserSyncSend to send and receive
  53. // data to the application on the SAServer end
  54. byte data[] = msg.getBytes();
  55. byte resp_data[] = null;
  56. /* userSyncSend invoked .
  57.    @param requestType = knownSASTypes+1;
  58.    return Response data
  59. */
  60. if(sasclient != null) {
  61. int req_type = (sasclient.getMaxSasTypes())+1;
  62. resp_data = sasclient.userSyncSend (req_type, data);
  63. }
  64. else {
  65. text.append("SAServer not connected"+"n");
  66. return;
  67. }
  68. if (resp_data == null) {
  69. text.append("No response received from Server"+"n");
  70. return;
  71. }
  72. String res_msg = new String(resp_data, 0);
  73. text.append("Response Message:n" + res_msg + "n");
  74. }
  75. public void actionPerformed(ActionEvent e) {
  76. if(e.getActionCommand().equals("Send")) {
  77. sendData(field.getText());
  78. }
  79. else if(e.getActionCommand().equals("Clear")) {
  80. text.setText("");
  81. }
  82. }
  83. public void stop() {
  84. if(sasclient != null) {
  85. sasclient.stop();
  86. sasclient = null;
  87. }
  88. }
  89. }