-
Notifications
You must be signed in to change notification settings - Fork 0
/
IO7_client.java
167 lines (136 loc) · 4.54 KB
/
IO7_client.java
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//BASE CODE:
// File: IO7_client.java (Module 13)
//
// Author: Rahul Simha
// Created: Nov 28, 1998
// Modified: Nov 13, 2000.
//
// Producer as client, using sockets.
/*
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
////////// Abigail H. Shriver //////////
////////// CSci 4243 Senior Design Project //////////
////////// Self-Tracking E-currency //////////
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
*/
//I have edited and implemented this code to fit the needs of my project
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.net.ssl.*;
class Producer extends JFrame implements Runnable {
JLabel L; // A place to write stuff.
OutputStream outStream;
public Producer (OutputStream outStream)
{
// Store the reference to the output stream.
this.outStream = outStream;
// Create the frame.
this.setSize (700,100);
this.setLocation (0,100);
this.setTitle ("Producer");
Container cPane = this.getContentPane();
// cPane.setLayout (new BorderLayout());
// This is where we will write to.
L = new JLabel ("");
cPane.add (L, BorderLayout.CENTER);
this.setVisible (true);
}
// Must implement the run() method.
public void run ()
{
// Write Currency to buffer
for (int i=1; i<=25; i++) {
// Create a random byte
//byte k = (byte) UniformRandom.uniform (1, 100);
Currency testCurrency1 = new Currency(
"sender2",
"testReciever1",
new ArrayList<String> (),
100.00,
0.00,
new ArrayList<String> (Arrays.asList("Restrictive Tree","entertainment","newspaperMagazines")),
"currency1"
);
// Write to label first.
L.setText (L.getText() + " " + testCurrency1);
//L.setText (L.getText() + " " + testCurrency1);
// Write it to the screen.
System.out.println ("Producer: writing " + testCurrency1);
//System.out.println ("Producer: writing " + testCurrency1);
// Write integer to buffer.
try {
outStream.write (1);
}catch (IOException e) {
System.out.println (e);
}
// Sleep for a while.
try {
Thread.sleep ((int)UniformRandom.uniform(100,1000));
} catch (InterruptedException e) {
System.out.println (e);
}
}
// Write EOF and close output stream.
try {
outStream.write (-1);
outStream.close ();
}catch (IOException e) {
System.out.println (e);
}
L.setText (L.getText() + " Done!");
}
}
// This is an independent quit button to quit the application.
class QuitButton extends JFrame {
public QuitButton ()
{
this.setSize (80,50);
this.setLocation (0, 0);
this.setTitle ("Quit button");
Container cPane = this.getContentPane();
// cPane.setLayout (new BorderLayout());
JButton quitb = new JButton ("QUIT");
quitb.setBackground (Color.red);
quitb.addActionListener (
new ActionListener () {
public void actionPerformed (ActionEvent a){
System.exit (0);
}
}
);
cPane.add (quitb, BorderLayout.CENTER);
this.setVisible (true);
}
}
public class IO7_client {
public static void main (String[] argv)
{
// Create an independent quit button.
QuitButton q = new QuitButton ();
try {
// Open a socket to the server.
// Address: hobbes.seas.gwu.edu, port 5010.
Socket soc = new Socket ("localhost",5010);
// Socket soc = new Socket ("shell01.seas.gwu.edu",5010);
InetAddress remoteMachine = soc.getInetAddress();
System.out.println ("Producer as client: attempting connection"
+ " to " + remoteMachine);
// Note: server must be fired up first!
// Now create the output stream and hand off to producer.
OutputStream outStream = soc.getOutputStream ();
// Create a producer instance and thread.
Producer p = new Producer (outStream);
Thread pthread = new Thread (p);
// Start the threads.
pthread.start();
} catch (IOException e) {
System.out.println (e);
}
}
}