The client sends the request, the server receives it, read the email from a file and saves it inside a proper structured message, but it doesn't send the message to the client, I've seen the server doesn't write on the mailslot, the number of bytes written is always 0 (variable numWritten).
Can you see the problem?
Here the source code
- Server
- Client
- email, this must be inside the folder where server is placed.
copy and paste the links above is required to open these pages on the browser, that host does not allow direct linking.
sorry for my english... I'm italian
Client
#include <windows.h>
#include <stdio.h>
#define MAX_DIMENSION 100
//Prototipes
BOOL WriteMail(HANDLE);
BOOL ReadMail(HANDLE);
void Interface(void);
typedef struct {
char from[MAX_DIMENSION],
to[MAX_DIMENSION],
object[MAX_DIMENSION],
text[MAX_DIMENSION];
int service_code;
} message;
typedef struct {
message req;
} request_msg;
request_msg request_message;
HANDLE handle;
BOOL WriteMail(HANDLE handle) {
DWORD numWritten;
static TCHAR MyMessage[100];
int i, ch, aux;
/************** Write messages into mailslot **************/
printf("from: ");
for( i = 0; (i < 99) && ((ch = getchar()) != EOF) && (ch != '\n'); i++ )
MyMessage[i] = (char)ch;
MyMessage[i]='\n';
MyMessage[i+1] = '\0';
strcpy (request_message.req.from, MyMessage);
printf("To: ");
for( i = 0; (i < 99) && ((ch = getchar()) != EOF) && (ch != '\n'); i++ )
MyMessage[i] = (char)ch;
MyMessage[i]='\n';
MyMessage[i+1] = '\0';
strcpy (request_message.req.to, MyMessage);
printf("Object: ");
for( i = 0; (i < 99) && ((ch = getchar()) != EOF) && (ch != '\n'); i++ )
MyMessage[i] = (char)ch;
MyMessage[i]='\n';
MyMessage[i+1] = '\0';
strcpy (request_message.req.object, MyMessage);
printf("Text: ");
for( i = 0; (i < 99) && ((ch = getchar()) != EOF) && (ch != '\n'); i++ )
MyMessage[i] = (char)ch;
MyMessage[i]='\n';
MyMessage[i+1] = '\0';
strcpy (request_message.req.text, MyMessage);
/************************************************************************/
request_message.req.service_code = 1; //tells to the server to save the content of mailslot
if ( WriteFile(handle, &request_message, sizeof(request_message), &numWritten, NULL) == 0) {
printf("Impossible to send the message to the server\n");
scanf("%d",&aux);
ExitProcess(-1);
}
/* Check if all bytes have been written*/
if (sizeof(request_message) != numWritten)
printf("WriteFile didn't write the correct number of bytes!\n");
return(1);
} //End WriteMail
BOOL ReadMail(HANDLE handle) {
DWORD numWritten, numRead;
int aux;
request_message.req.service_code = 2;
while(1)
{
/* Send the code 2 to the server to let it send the mail */
if ( WriteFile(handle, &request_message, sizeof(request_message), &numWritten, NULL) == 0) {
printf("Impossible to send the message to the server\n");
scanf("%d",&aux);
ExitProcess(-1);
}
/* Read the record inside the mailslot which contains the mail */
if(ReadFile(handle, &request_message, sizeof(request_msg), &numRead, 0) == 0) {
printf("Error receiving message\n");
ExitProcess(-1);
}
/* Check if every bytes have been read */
else if (sizeof(request_msg) != numRead)
printf("ReadFile didn't read the correct number of bytes!");
else {
/********** Print the message ************/
printf("From: %s\n",request_message.req.from);
printf("To: %s\n",request_message.req.to);
printf("Object: %s\n",request_message.req.object);
printf("Text: %s\n\n",request_message.req.text);
}
/* Free the buffer */
GlobalFree(&request_message);
/* pause */
Sleep(1000);
} //close while
return (1);
} //End ReadMail
void Interface(void) {
int service;
printf("1: Write new email\n"); //this works
printf("2: Read email\n");
printf("3: Delete email (to be implemented)\n");
printf("4: Exit\n\n");
printf("Chose an operation: ");
scanf("%d", &service);
getchar();
switch(service) {
case 1:
WriteMail(handle);
break;
case 2:
ReadMail(handle);
break;
case 4:
break;
default:
printf("%c \n",service);
}
} //End Interface
int main(int argc,char **argv) {
/* Attach the mailslot created by server */
handle = CreateFile("\\\\*\\mailslot\\slot",
GENERIC_WRITE,
FILE_SHARE_READ,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if (handle == INVALID_HANDLE_VALUE)
{
printf("CreateFile failed!");
// Close open meilslot
if (handle != INVALID_HANDLE_VALUE)
CloseHandle(handle);
return(-1);
}
Interface();
return(0);
}
Server
#include <windows.h>
#include <stdio.h>
#define MemError -2
#define IOError -1
#define NoError 0
#define MAX_DIMENSION 100
// Prototipes
int Service(request_msg, HANDLE);
BOOL ReadFromFile(HANDLE, char *);
BOOL WriteOnFile(request_msg);
typedef struct {
char from[MAX_DIMENSION],
to[MAX_DIMENSION],
object[MAX_DIMENSION],
text[MAX_DIMENSION];
int service_code;
} message;
typedef struct {
message req;
} request_msg;
request_msg request_message;
char *string;
HANDLE handle;
int Service(request_msg mex, HANDLE handle) {
int operation = mex.req.service_code;
switch(operation) {
case 1:
WriteOnFile(mex);
break;
case 2:
ReadFromFile(handle, "input.txt");
break;
default:
printf("%c \n",operation);
}
return NoError;
} //End Servizio
BOOL ReadFromFile(HANDLE handle, char *in) {
FILE *input;
DWORD numWritten;
int aux;
input = fopen(in,"r");
if(input == NULL)
return IOError;
string = calloc(MAX_DIMENSION,sizeof(char));
if(string == NULL)
return MemError;
fgets(string,MAX_DIMENSION,input);
strcpy (request_message.req.from, string);
fgets(string,MAX_DIMENSION,input);
strcpy (request_message.req.to, string);
fgets(string,MAX_DIMENSION,input);
strcpy (request_message.req.object, string);
fgets(string,MAX_DIMENSION,input);
strcpy (request_message.req.text, string);
fclose(input); //ok until here
if (WriteFile (handle, &request_message, sizeof(request_msg), &numWritten, NULL) == 0) {
if (numWritten!=sizeof(request_msg)) {
printf("Incorrect number of bytes written\n");
printf("sizeof(request_msg): %d\n\n",sizeof(request_msg));
}
printf("Error sending message to client\n");
scanf("%d",&aux);
ExitProcess(-1);
}
return(1);
} //END ReadFromFile
BOOL WriteOnFile(request_msg mex) {
FILE *output;
output = fopen("output.txt","a+");
if(output == NULL)
return IOError;
else {
fprintf(output,mex.req.from);
fprintf(output,mex.req.to);
fprintf(output,mex.req.object);
fprintf(output,mex.req.text);
fclose(output);
}
return(1);
} //End WriteOnFile
int main(int argc,char **argv)
{
request_msg *request_message,*mex;
BOOL err;
DWORD msgSize;
mex = malloc(sizeof(request_msg));
/* Create mailslot */
handle = CreateMailslot("\\\\.\\mailslot\\slot",
0,
MAILSLOT_WAIT_FOREVER,
NULL);
if (handle == INVALID_HANDLE_VALUE)
{
printf("CreateMailslot failed");
// Closes opened mailslots
if (handle != INVALID_HANDLE_VALUE)
CloseHandle(handle);
return(-1);
}
while(1)
{
DWORD numRead;
request_message = malloc(sizeof(request_msg));
/* Dimension of next record */
err = GetMailslotInfo(handle, 0, &msgSize, 0, 0);
/* Checks for errors */
if (!err) {
printf("GetMailslotInfo failed");
// Closes opened mailslots
if (handle != INVALID_HANDLE_VALUE)
CloseHandle(handle);
return(-1);
} // closes if(!err)
/* Check if msgSize is null*/
if (sizeof(request_msg) != (DWORD)MAILSLOT_NO_MESSAGE) {
/* read the record */
if(ReadFile(handle, request_message, sizeof(request_msg), &numRead, 0) == 0) {
printf("Errore nella ricezione del mex\n");
ExitProcess(-1);
}
/* Check if correct nuber of bytes has been read */
else if (sizeof(request_msg) != numRead)
printf("ReadFile: incorrect number of bytes!");
else {
/*************************************
******* Request for the service ******
*************************************/
strcpy(mex->req.from,request_message->req.from);
strcpy(mex->req.to,request_message->req.to);
strcpy(mex->req.object,request_message->req.object);
strcpy(mex->req.text,request_message->req.text);
mex->req.service_code = request_message->req.service_code;
Service(*mex, handle);
} // close else
/* Free the buffer */
GlobalFree(request_message);
} // close if(sizeof...
/* pause */
Sleep(1000);
} //close while
return NoError;
}
Edit: Source input directly to post - Amadeus
This post has been edited by Dark_Nexus: 09 August 2007 - 03:18 PM

New Topic/Question
Reply



MultiQuote



|