KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PipeTalk


1 import jcifs.smb.SmbNamedPipe;
2 import java.io.OutputStream JavaDoc;
3 import java.io.InputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5
6 public class PipeTalk {
7
8     static class ReceiverThread extends Thread JavaDoc {
9         InputStream JavaDoc in;
10         byte[] buf = new byte[20];
11         int n;
12
13         ReceiverThread( InputStream JavaDoc in ) {
14             this.in = in;
15         }
16         public void run() {
17             try {
18                 while(( n = in.read( buf )) != -1 ) {
19                     System.out.println( new String JavaDoc( buf, 0, n ));
20                 }
21             } catch( IOException JavaDoc ioe ) {
22                 ioe.printStackTrace();
23             }
24         }
25     }
26
27     public static void main( String JavaDoc argv[] ) throws Exception JavaDoc {
28
29         SmbNamedPipe pipe = new SmbNamedPipe( argv[0], SmbNamedPipe.PIPE_TYPE_RDWR );
30         InputStream JavaDoc in = pipe.getNamedPipeInputStream();
31         OutputStream JavaDoc out = pipe.getNamedPipeOutputStream();
32
33         ReceiverThread rt = new ReceiverThread( in );
34         rt.start();
35
36         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
37         String JavaDoc msg;
38         int c;
39         while(( c = System.in.read() ) != -1 ) {
40             if( c == '\n' ) {
41                 msg = sb.toString();
42                 if( msg.startsWith( "exi" )) {
43                     break;
44                 }
45                 System.out.println( sb.toString() );
46                 out.write( msg.getBytes() );
47                 sb.setLength( 0 );
48             } else {
49                 sb.append( (char)c );
50             }
51         }
52         in.close();
53         out.close();
54     }
55 }
56
57
Popular Tags