blob: 924e6839348d9e5750a78b59922bb701e6e4dcd8 (
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
|
package Client;
import java.io.*;
import java.net.Socket;
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
public class ChatClientWriter extends Thread
{
private ChatClient client;
private Socket soc;
private BufferedWriter output;
private LinkedBlockingQueue<String> queue;
public static Scanner reader = new Scanner(System.in);
/** constructor
* @param the socket and the client info */
public ChatClientWriter(Socket soc ,ChatClient client)
{
this.soc = soc;
this.client = client;
this.queue = new LinkedBlockingQueue<String>();
}
public void sendLine (String line)
{
try
{
this.queue.put(line);
} catch (InterruptedException e)
{
System.err.println("input line is null: " + e);
}
}
/** writing the line and making sure the package will be pushed on the stream
* @param the line and the client info */
public void println(String line)
{
try
{
this.output.write(line, 0, line.length());
this.output.newLine();
this.output.flush();
} catch (IOException e) {System.err.print("failed to print line" + e);}
}
/** Running the object and translate the output from bytes to letters*/
public void run()
{
try
{
OutputStreamWriter outTemp;
String line;
outTemp = new OutputStreamWriter(this.soc.getOutputStream());
this.output = new BufferedWriter(outTemp);
client.startup();
while (true)
{
try
{
line = this.queue.take();
} catch (InterruptedException e){line = "";}
System.out.println(line);
this.output.write(line, 0, line.length());
this.output.newLine();
this.output.flush();
}
} catch (IOException e) {}
}
}
|