3 Replies - 2036 Views - Last Post: 13 August 2007 - 02:58 AM Rate Topic: -----

#1 Alhazred  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 181
  • Joined: 25-July 07

[C] Server process doesn't send message to client

Post icon  Posted 25 July 2007 - 07:18 AM

I'm writing a client-server software which should emulate an email service. I'm now at the beginning and I'm trying to let communicate the client and the server process. I have no problem writing a new email, I send it to the server and the server saves it. I have a problem when I have to read an email from the server.
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 :P

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


Is This A Good Question/Topic? 0
  • +

Replies To: [C] Server process doesn't send message to client

#2 Alhazred  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 181
  • Joined: 25-July 07

Re: [C] Server process doesn't send message to client

Posted 07 August 2007 - 12:16 AM

I realized I missed to call a CreateFile() in the server process.
Now I've added it inside the main(), but have problem with the following GetMailslotInfo(), it returns the error code 1 which means "Invalid Function" though it looks to be correct.

Do you see something strange?

The code is changed and linked in the first post.
Was This Post Helpful? 0
  • +
  • -

#3 Alhazred  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 181
  • Joined: 25-July 07

Re: [C] Server process doesn't send message to client

Posted 09 August 2007 - 10:20 AM

I solved the problem with GetMailslotInfo... now I have a problem with the function "Leggi email" ("LeggiDaFile()" inside the server code) which should read an email on the server, the server should read a text file and send the content to the client, I can't do the last part. The server reads the file and fills the structured type but doesn't send it through the mailslot to the client, I have an "Access Denied" error.

Why does it happen? I set read/write flags.

Updated code is available at the links in the first post.
Was This Post Helpful? 0
  • +
  • -

#4 Alhazred  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 181
  • Joined: 25-July 07

Re: [C] Server process doesn't send message to client

Posted 13 August 2007 - 02:58 AM

I found a reason why the problem could happen, messages sent from server must be less than 400 bytes and mine were 404 bytes.
Now the size of my messages has been changed to stay under 400 bytes, but still have problems.
It should be due to security attributes, I set NULL and it seems I have to set a security attribute structure. Unfortunatly can't understand how to do reading MSDN.
Can you help me?

Changed code linked in the first post.

Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1