The Linux Programming Interface: a linux and unix system Programming Handbook


A File-Server Application Using Message Queues



tải về 6.93 Mb.
Chế độ xem pdf
trang805/806
Chuyển đổi dữ liệu08.07.2022
Kích6.93 Mb.
#52607
1   ...   798   799   800   801   802   803   804   805   806
The Linux Programming Interface

46.8
A File-Server Application Using Message Queues
In this section, we describe a client-server application that uses one message queue
per client. The application is a simple file server. The client sends a request message
to the server’s message queue asking for the contents of a named file. The server
responds by returning the file contents as a series of messages to the client’s private
message queue. Figure 46-3 provides an overview of the application.
Because the server performs no authentication of the client, any user that can
run the client can obtain any of the files accessible to the server. A more sophisticated
server would require some type of authentication from the client before serving the
requested file.
Figure 46-3: Client-server IPC using one message queue per client
Common header file
Listing 46-7 is the header file included by both the server and the client. This
header defines the well-known key to be used for the server’s message queue
Server MQ
Client sends request to
Server MQ (mtext includes
ID of client queue)
2
3
Client reads
response(s)
6
fork()
msgg
et(
IPC_PRIV
ATE, ...)
1
Client creates
private queue
Client MQ
Server reads
request
4
5
Server child sends
response(s)
Server creates child
to handle request
Client
Server
Server child


956
Chapter 46
(
SERVER_KEY
), and defines the formats of the messages to be passed between the cli-
ent and the server.
The requestMsg structure defines the format of the request sent from the client
to the server. In this structure, the mtext component consists of two fields: the iden-
tifier of the client’s message queue and the pathname of the file requested by the
client. The constant 
REQ_MSG_SIZE
equates to the combined size of these two fields
and is used as the msgsz argument in calls to msgsnd() using this structure.
The responseMsg structure defines the format of the response messages sent
from the server back to the client. The mtype field is used in response messages to
supply information about the message content, as defined by the 
RESP_MT_*
constants.
Listing 46-7: Header file for 
svmsg_file_server.c
and 
svmsg_file_client.c
–––––––––––––––––––––––––––––––––––––––––––––––––––––––

tải về 6.93 Mb.

Chia sẻ với bạn bè của bạn:
1   ...   798   799   800   801   802   803   804   805   806




Cơ sở dữ liệu được bảo vệ bởi bản quyền ©hocday.com 2024
được sử dụng cho việc quản lý

    Quê hương