Pazartesi, Mayıs 29, 2006

Client/Server 3



Nihayet..
This project involves a very simple FTP application.
You are supposed to develop two programs, Client and Server, which have the above communication
scheme. Client program accepts the following user commands from the console:
get filename : requests a file. filename is the name of the requested file and it does not
include path information. This request is sent to the server through the
message queue. Upon this request, the server sends the file to the client
through the FIFO. If the requested file does not exist, the server does not
send anything.
put filename : requests uploading a file. After sending this request, the client
immediately sends the file to the server through the FIFO.
dir : request the list of the current directory (where server program is
executed).


--fullduplex.h file--
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "errno.h"
#include "ctype.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "sys/msg.h"
#include "fcntl.h"
#include "unistd.h"

#define NP1 "/tmp/np1"
#define NP2 "/tmp/np2"
#define MAX_BUF_SIZE 1024

struct my_msgbuf {
long mtype;
char mtext[200];
};

--client.c--

#include "fullduplex.h"
int main(int argc, char *argv[]){

int msgqid;
struct my_msgbuf msgbuf;
char command[10], filename[50];
FILE *file;
int rdfd, wrfd;
char readbuffer[MAX_BUF_SIZE];
int numread;

printf("client starts : ");
printf("%s %s\n", argv[0], argv[1]);
msgqid = atoi(argv[1]);

msgbuf.mtype = 1;//neden bilmiyorum
printf("\n--> ");
gets(msgbuf.mtext);
fflush(stdin);
if(msgsnd(msgqid,(struct my_msgbuf*)&msgbuf, sizeof(msgbuf), 0) == -1){
perror("msgsnd");
exit(1);
}
while(strcmp(msgbuf.mtext, "exit") != 0){
if(strcmp(msgbuf.mtext, "dir") == 0){
rdfd = open(NP2, O_RDONLY);
while((numread = read(rdfd, readbuffer, sizeof(readbuffer))) > 0){
readbuffer[numread] = '\0';
printf("%s", readbuffer);
}
printf("\n");
close(rdfd);
}
else{
sscanf(msgbuf.mtext, "%s %s\n", command, filename);
if(strcmp(command, "putfile") == 0){
file = fopen(filename, "r");
wrfd = open(NP1, O_WRONLY);
while((numread = fread(readbuffer, 1, sizeof(readbuffer), file)) > 0){
write(wrfd, readbuffer, numread);
}
fclose(file);
close(wrfd);
}
else{
if(strcmp(command, "getfile") == 0){
file = fopen(filename, "w");
rdfd = open(NP2, O_RDONLY);
while((numread = read(rdfd, readbuffer, sizeof(readbuffer))) > 0){
fwrite(readbuffer, 1, numread, file);
}
fclose(file);
close(rdfd);
}
else{
printf("unknown command\n");
}
}
}
printf("\n--> ");
gets(msgbuf.mtext);
fflush(stdin);
if(msgsnd(msgqid,(struct my_msgbuf*)&msgbuf, sizeof(msgbuf), 0) == -1){
perror("msgsnd");
exit(1);
}
}
printf("client ends\n");

return 1;
}

--server.c--
#include "fullduplex.h"
int main(void){

int ret_val;
key_t key;
int msgqid;
struct my_msgbuf msgbuf;
char command[10], filename[50];
FILE *file;
int rdfd, wrfd;
char readbuffer[MAX_BUF_SIZE];
int numread;

printf("server starts\n");
ret_val = mkfifo(NP1, S_IFIFO | 0666);
if((ret_val == -1) && (errno != EEXIST)){
perror("mkfifo");
exit(1);
}
ret_val = mkfifo(NP2, S_IFIFO | 0666);
if((ret_val == -1) && (errno != EEXIST)){
unlink(NP1);
perror("mkfifo");
exit(1);
}
if((key = ftok(".",'A')) == -1){
unlink(NP1);
unlink(NP2);
perror("ftok");
exit(1);
}
if((msgqid = msgget(key, IPC_CREAT | 0666)) == -1){
unlink(NP1);
unlink(NP2);
perror("msgget");
exit(1);
}
printf("fifo 1 (server read) : %s\n", NP1);
printf("fifo 1 (server write) : %s\n", NP2);
printf("message queue id : %d\n", msgqid);

if(msgrcv(msgqid, (struct my_msgbuf*)&msgbuf, sizeof(msgbuf), 0, 0) == -1){
unlink(NP1);
unlink(NP2);
msgctl(msgqid, IPC_RMID, 0);
perror("msgrcv");
exit(1);
}
while(strcmp(msgbuf.mtext, "exit") != 0){
printf("received command : %s\n", msgbuf.mtext);
if(strcmp(msgbuf.mtext, "dir") == 0){
file = popen("dir", "r");
wrfd = open(NP2, O_WRONLY);
while((numread = fread(readbuffer, 1, sizeof(readbuffer), file)) > 0){
write(wrfd, readbuffer, numread);
}
pclose(file);
close(wrfd);
}
else{
sscanf(msgbuf.mtext, "%s %s\n", command, filename);
if(strcmp(command, "putfile") == 0){
file = fopen(filename, "w");
rdfd = open(NP1, O_RDONLY);
while((numread = read(rdfd, readbuffer, sizeof(readbuffer))) > 0){
fwrite(readbuffer, 1, numread, file);
}
fclose(file);
close(rdfd);
}
else{
if(strcmp(command, "getfile") == 0){
file = fopen(filename, "r");
wrfd = open(NP2, O_WRONLY);
while((numread = fread(readbuffer, 1, sizeof(readbuffer), file)) > 0){
write(wrfd, readbuffer, numread);
}
fclose(file);
close(wrfd);
}
else{
printf("unknown command\n");
}
}
}
if(msgrcv(msgqid, (struct my_msgbuf*)&msgbuf, sizeof(msgbuf), 0, 0) == -1){
unlink(NP1);
unlink(NP2);
msgctl(msgqid, IPC_RMID, 0);
perror("msgrcv");
exit(1);
}
}
printf("received command : %s\n", msgbuf.mtext);

unlink(NP1);
unlink(NP2);
msgctl(msgqid, IPC_RMID, 0);
printf("server ends\n");

return 0;
}

3 yorum:

Adsız dedi ki...

özgür karata$?

Adsız dedi ki...

gezegene'e ödevlerini yazmak da ilginç bir fikir.

sickprincess dedi ki...

Ben degilim, tanimam, tanismam! Ben kendi halimde bir ogrenciyim, Linux falan ogrenmeye calisiyoruz!