summaryrefslogtreecommitdiff
path: root/java/tests/SOCK_SAP/SOCKConnectorTest.java
blob: f9d135753f2c4c3f4c68ab56dfe1a03ff09e4b9d (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
// ============================================================================
//
// = PACKAGE
//    tests.SOCK_SAP
// 
// = FILENAME
//    SOCKConnectorTest.java
//
// = AUTHOR
//    Prashant Jain
// 
// ============================================================================
package tests.SOCK_SAP;

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

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

  void processRequests (SOCKStream stream) throws IOException
    {
      DataInputStream in = new DataInputStream (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)
    {
      int port = ACE.DEFAULT_SERVER_PORT;
      SOCKConnectorTest client = new SOCKConnectorTest ();
      if (args.length == 2)
	{
	  try
	    {
	      port = Integer.parseInt (args[1]);
	    }
	  catch (NumberFormatException e)
	    {
	      client.print_usage_and_die ();
	    }
	}
      client.init (args[0], port);
    }
}