summaryrefslogtreecommitdiff
path: root/java/JACE/tests/SOCK_SAP/SOCKConnectorTest.java
blob: e14b63936f5d866585027e833e77dbbd42d9bb7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// ============================================================================
//
// = PACKAGE
//    tests.SOCK_SAP
// 
// = FILENAME
//    SOCKConnectorTest.java
//
// = AUTHOR
//    Prashant Jain
// 
// ============================================================================
package JACE.tests.SOCK_SAP;

import java.io.*;
import java.net.*;
import JACE.OS.*;
import JACE.SOCK_SAP.*;

public class SOCKConnectorTest
{
  static void print_usage_and_die ()
    {
      System.out.println ("Usage: SOCKConnectorTest <hostname> [<port>]");
      System.exit (0);
    }

  void processRequests (SOCKStream stream) throws IOException
    {
      // 1.0 JDK      DataInputStream in = new DataInputStream (System.in);
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      String msg;
      int ack_len;

      while (true)
 {
   StringBuffer ack = new StringBuffer ();
   ACE.DEBUG ("Enter input: ");
   ACE.FLUSH ();
   msg = in.readLine ();
   if (msg == null)
     break;
   stream.send (msg);
   ACE.DEBUG ("Waiting for ack...");
   ack_len = stream.recv (ack);
   if (ack_len == 0)
     break;
   else
     ACE.DEBUG (ack.toString ());
 }
    }

  public void init (String host, int port)
    {
      SOCKStream stream = new SOCKStream ();
      SOCKConnector connector = new SOCKConnector ();
      try
 {
   connector.connect (stream,
        host,
        port);
   processRequests (stream);
 }
      catch (IOException e)
 {
   ACE.ERROR (e);
 }
    }

  public static void main (String [] args)
    {
      ACE.enableDebugging ();

      int port = ACE.DEFAULT_SERVER_PORT;
      SOCKConnectorTest client = new SOCKConnectorTest ();

      // check arg count 
      if (args.length == 0 || args.length > 2) 
         print_usage_and_die(); 

      if (args.length == 2)
	{
	  try
	    {
	      port = Integer.parseInt (args[1]);
	    }
	  catch (NumberFormatException e)
	    {
	      client.print_usage_and_die ();
	    }
	}
      client.init (args[0], port);
	

    }
}