I have an assignment to write a simple mail application program. I am working on the server side of things now, but I am stuck. I have the code working, but it doesn't satisfy requirements. I am basically focusing on the server portions for now. Points #1 and #3 apply to the server. I am really stuck on point #3. Here is the assignment criterion:
I have to implement both the client and iterative server satisfying the following design guidelines
1. The server listens on port YOUR-PORT-NUMBER for incoming TCP connection. When a client connects, the server sends a "Welcome to Mail Server" message and then waits for a command from the client.
2. When a client receives the welcome message, it has the option of sending two commands
A. "Get <email address>"
B. "Sendto <email address> <message>"
3. The server manages a list of messages to be sent. If a client sends a GET request, the server sends all messages belonging to the given email address through its existing connected socket, then deletes them from its message queue, and finally closes the active socket.
4. The client can send a message by using SENDTO command.
5. The client software MUST accept the server name and port number as command-line arguments.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define SERVER_PORT 27202
/* Simple Mail Application server */
int main()
{
int i, len, num, rc;
int listen_sd, accept_sd;
/* Buffer for data */
char buffer[100];
struct sockaddr_in addr;
printf("Usage: Simple Mail Application server\n");
num = 1;
/* Create a TCP stream socket to receive */
/* incoming connections on */
listen_sd = socket(PF_INET, SOCK_STREAM, 0);
if(listen_sd < 0)
{
fprintf(stderr, "Cannot create a TCP socket\n");
exit(-1);
}
else
printf("Socket() is OK\n");
printf("Binding the socket...\n");
/* Bind the socket */
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(SERVER_PORT);
rc = bind(listen_sd, (struct sockaddr *)&addr, sizeof(addr));
if(rc < 0)
{
fprintf(stderr, "Bind() error\n");
close(listen_sd);
exit(-1);
}
else
printf("Bind() is OK\n");
/* Set the listen backlog */
rc = listen(listen_sd, 10);
if(rc < 0)
{
fprintf(stderr, "Listen() error\n");
close(listen_sd);
exit(-1);
}
else
printf("Listen() is OK\n");
/* Inform user that the server is ready */
printf("The Server is ready!\n");
for(i=0; i < num; i++)
{
printf(" Waiting on client(s) to connect.....\n");
accept_sd = accept(listen_sd, NULL, NULL);
if(accept_sd < 0)
{
fprintf(stderr, "Accept() error\n");
close(listen_sd);
exit(-1);
}
else
printf("Accept() is OK and completed successfully!\n");
/* Receive a message from the client */
printf("I am waiting client(s) to send message(s) to me...\n");
rc = recv(accept_sd, buffer, sizeof(buffer), 0);
if(rc <= 0)
{
fprintf(stderr, "Recv() error\n");
close(listen_sd);
close(accept_sd);
exit(-1);
}
else
printf("The message from client: \"%s\"\n", buffer);
/* Echo the data back to the client */
printf("Echoing it back to client...\n");
len = rc;
rc = send(accept_sd, buffer, len, 0);
if(rc <= 0)
{
fprintf(stderr, "Send() error\n");
close(listen_sd);
close(accept_sd);
exit(-1);
}
else
printf("Send() is OK.\n");
/* Close the incoming connection */
close(accept_sd);
}
/* Close the listen socket */
close(listen_sd);
return 0;
}
My current code is set to handle and echo only one message. I basically need help modifying it to allow the functionality to satisy requirement #3 above.
Thanks,
Mike