forked from sparrowxiao/ConcurrentTest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProduceClient.java
More file actions
76 lines (61 loc) · 2.06 KB
/
ProduceClient.java
File metadata and controls
76 lines (61 loc) · 2.06 KB
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
import java.io.*;
import java.net.*;
import java.lang.*;
/**
*
* @author sparrow
*/
public class ProduceClient extends Thread {
private Socket socket = null;
private int portNo = 0;
private PrintWriter out = null;
private BufferedReader in = null;
private String host = null;
private int count = 0;
// constructor
public ProduceClient( String host, int portNo,int sizeofMsg) {
//super( "ServerThread");
this.host = host;
this.portNo = portNo;
this.sizeofMsg = sizeofMsg;
} // constructor
public void run() {
try {
socket = new Socket(host,portNo);
out = new PrintWriter(socket.getOutputStream(),true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
catch( UnknownHostException e)
{
System.err.println("Dnot't know host named: " + host);
System.exit(1);
}
catch(IOException e)
{
System.err.println("Couldn't get I/O for " + "the connection to: " + host);
System.exit(1);
}
try{
BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in));
String userInput;
do {
//System.out.print( "Enter some text to echo (END = exit): ");
System.out.print( "Test the multi-thread methods (END = exit): ");
//System.out.flush();
//userInput = stdIn.readLine();
userInput = "thread" + (count++);
out.write(usrInput);
//out.println(userInput);
//System.out.println("\techo: " + in.readLine());
} while( !userInput.equals( "END")); // while forever waiting for a client connection
out.close();
in.close();
stdIn.close();
socket.close();
}
catch(IOException ioE)
{
System.err.println("connection error, maybe cannot produce this client.");
}
}
}