#i cant get the password and the directory

4 messages · Page 1 of 1 (latest)

keen hazel
#

this is my code its about socket programming the issue basically when i compile using:
./a.out -p <port number> -d <directory name> -w <password>

the password and the directory name is not executed the right way can ill appreciate if someone can help me with this thing

int main(int argc, char *argv[])
{
    int sd, opt, optval;
    struct sockaddr_in addr;
    unsigned short port = 0;
    char *password = NULL;
    char *directory = NULL;

    while ((opt = getopt(argc, argv, "p:d:w:")) != -1)
    {
        switch (opt)
        {
        case 'p':
            port = atoi(optarg);
            break;
        case 'd':
            directory = optarg;
            break;

        case 'w':
            password = optarg;
            break;

        default:
            fprintf(stderr, "usage: %s -p port_number -d directory_name -w password\n", argv[0]);
            exit(EXIT_FAILURE);
        }

        if (port == 0 || directory == NULL || password == NULL)
        {
            fprintf(stderr, "usage: %s -p port_number -d directory_name -w password\n", argv[0]);
            exit(EXIT_FAILURE);
        }

        if ((sd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
            PANIC("Socket");
        addr.sin_family = AF_INET;

        if (port > 0)
            addr.sin_port = htons(port);
        else
            addr.sin_port = htons(PORT);

        addr.sin_addr.s_addr = INADDR_ANY;

        // set SO_REUSEADDR on a socket to true (1):
        optval = 1;
        setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval);

        if (bind(sd, (struct sockaddr *)&addr, sizeof(addr)) != 0)
            PANIC("Bind");
        if (listen(sd, SOMAXCONN) != 0)
            PANIC("Listen");

        printf("Port number: %d\n", ntohs(addr.sin_port));
        printf("Directory: %s\n", directory);
        printf("Password: %s\n", password);
jade iceBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

swift bramble
#

Perhaps you could provide the rest of the code?

spare rapids
#

Is it really wise to be doing so much in the loop that processes the CL arguments?
I would loop, parse and get out.
Only then actually open the socket.