Thread: Help with client/server programming

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    22

    Help with client/server programming

    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

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Obviously you need store received messages somewhere, then retrieve them and provide them to the client in response to a GET request. Surely if you're working with sockets you've covered storing data.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    22
    Ok, I get that. So are the messages stored in the buffer? Thanks for the help, but I am not a programmer and it has been 8 years since I have written in C. Obviously, this class is far advanced and assumes a prereq which I satisfied on paper, but I am still recalling some of the basics. I am looking for a little booster to get me started in the right direction. I am sure I will need a while loop and I need to store the messages.

    Thanks.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I would use a linked list of messages. Here's a brief tutorial, Google has lots more.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. client server programming strange behaviour
    By nsudharrao90 in forum Networking/Device Communication
    Replies: 7
    Last Post: 04-10-2011, 12:53 PM
  2. Client server programming hijack
    By bhawna gopalka in forum Linux Programming
    Replies: 1
    Last Post: 10-07-2010, 05:18 AM
  3. C - Client Server game programming
    By git in forum C Programming
    Replies: 5
    Last Post: 09-06-2004, 02:09 PM
  4. help with sockets .. client-server programming
    By milan in forum Linux Programming
    Replies: 5
    Last Post: 01-05-2003, 06:47 AM
  5. Client/Server Programming!!!
    By Silverdream in forum C Programming
    Replies: 4
    Last Post: 03-30-2002, 12:19 AM