|
|
1.1 ! root 1: Sample: Named Pipe Client/Server Demonstration ! 2: ! 3: Summary: ! 4: ! 5: NPCLIENT and NPSERVER demonstrate the use of named pipes. ! 6: The basic design consist of a server application serving ! 7: multiple client applications. The user can use the client ! 8: applications as an interface to all of the other client ! 9: applications via the server. The effect is a simple ! 10: communication program that can be used over the network ! 11: between multiple clients. ! 12: ! 13: More Information: ! 14: ! 15: The actual implementation works by having the NPSERVER ! 16: application launch a new thread, which creates and services ! 17: a new instance of the server side of the named pipe every ! 18: time a client connects to it. You need only start one ! 19: instance of the NPSERVER application. It will service up to ! 20: 100 instances of the NPCLIENT application. (Note that the ! 21: 100 instance limit is hard coded into the sample. It does ! 22: not reflect the number of named pipe instances you can ! 23: create, which is virtually infinite.) ! 24: ! 25: TO USE: ! 26: ! 27: Start an instance of NPSERVER. A window will appear. ! 28: ! 29: Start an instance of NPCLIENT. Two dialog boxes will ! 30: appear, one on top of the other. The top level dialog box ! 31: will prompt you for a share name and a client or user name. ! 32: If the instance of NPCLIENT is local to (on the same machine ! 33: as) the NPSERVER instance, enter a '.' for the share name. ! 34: Otherwise, enter the machine name of the server that the ! 35: NPSERVER instance was started on, i.e. 'FoobarServer'. For ! 36: the client or user name, enter any name you wish to be ! 37: identified with. Hit enter or click the OK button. ! 38: ! 39: The upper dialog box will go away, and you'll see the Client ! 40: dialog box of NPCLIENT. It consists of two edit fields and ! 41: a 'Send' button. You will be able to read messages from ! 42: other clients (and yourself) in the larger/upper edit field. ! 43: (Note, if the message seems garbled, make sure the cursor of ! 44: the edit field is located in the lower left hand corner of ! 45: the field.) The smaller edit field is used to type ! 46: messages. To send a message: type something in the ! 47: lower/smaller edit field, and hit enter or click the Send ! 48: button. The message will appear in the larger edit field of ! 49: all the clients connected to the NPSERVER instance; ! 50: prepended by the user name you selected. Note that the user ! 51: name you selected will be entered into the caption bar of ! 52: the NPCLIENT instance. This allows you to more easily keep ! 53: track of multiple instances of NPCLIENT on the same machine. ! 54: ! 55: At the same time the top level dialog box was dismissed from ! 56: the NPCLIENT instance, the NPSERVER window was updated with ! 57: the picture of a red spool of thread accompanied by the user ! 58: name you selected. This red spool indicates an active ! 59: client thread connected to NPSERVER. The spool may be ! 60: connected to other spools with a thin blue line (similar to ! 61: the way the File Manager connects files or directories). ! 62: Any time a client disconnects from NPSERVER; the spool ! 63: representing it will be grayed out. ! 64: ! 65: DESIGN: ! 66: ! 67: Basically, the NPSERVER application launches multiple ! 68: instances of a server thread. When the application is ! 69: started, the first thread is created. It creates an ! 70: instance of the server side of the named pipe, and waits for ! 71: a client to connect. Once a client connects, another thread ! 72: is started and it too blocks waiting for a client. ! 73: Meanwhile, the first thread updates a global array of client ! 74: information with this specific client's information. The ! 75: thread then enters a loop reading from this client. Any ! 76: time this specific client sends a message, this server ! 77: thread will call a function (TellAll) which will write the ! 78: message to all the clients that have been listed in the ! 79: global array. ! 80: ! 81: On the client side, NPCLIENT tries to connect to the named ! 82: pipe with a CreateFile call. Once it has connected, it ! 83: creates a thread which loops and reads any message from the ! 84: server side. Once a message is read, it is printed in the ! 85: larger edit field. Any time the user hits the Send button, ! 86: the main thread grabs any text in the lower edit field, and ! 87: writes it to the server. ! 88: ! 89: The steps between NPSERVER and an instance of NPCLIENT looks ! 90: like this: ! 91: ! 92: NPSERVER NPCLIENT ! 93: -------- -------- ! 94: ! 95: CreateNamedPipe() ! 96: ConnectPipe() // Blocks ! 97: CreateFile() //Connects to pipe. ! 98: spawn separate thread to read pipe ! 99: return from block ! 100: updates array of clients ! 101: spawn another server thread ! 102: Loop ! 103: ReadFile() // Blocks on overlap ! 104: WriteFile() // User hits Send. ! 105: ! 106: return from block ! 107: WriteFile() // Broadcast to clients ! 108: End loop // When client breaks pipe. ! 109: ! 110: ! 111: ReadPipe Thread: ! 112: Loop ! 113: ReadFile() ! 114: block till server broadcasts ! 115: ! 116: return from block. ! 117: put string in edit field. ! 118: ! 119: End loop // when server breaks. ! 120: ! 121: The overlapped structure should be used anytime a pipe is ! 122: expected to block for any length of time on a read or write. ! 123: This allows the thread to return immediately from a read or ! 124: write to service any other part of your application. The ! 125: overlapped structure should also be used on a named pipe ! 126: anytime you expect to do simultaneous reads and writes. A ! 127: minor disadvantage of having to use the overlapped structure ! 128: is that you must update the file pointer in the buffer (or ! 129: in a file) manually. This is done by manually modifying the ! 130: offset field of the overlapped structure after each read. ! 131: The offset is obtained from the GetOverlappedResult call. ! 132: ! 133:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.