Home > Back-end >  How to pass and receive cmd line arguments in socket?
How to pass and receive cmd line arguments in socket?

Time:10-02

I am working on socket with C, and needs to pass command line argument from the client to the server. The server then needs to switch the cases of the sting and send it back to the client. I am stuck at passing the command line argument to the server and then sending it back after switching the case. Here is what I have so far:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 10291
#define MAXLINE 9999
// Client side code
int main(int argc, char *argv[])
{
    int sockfd;
    char buffer[MAXLINE];
    char clientMsg;
    struct sockaddr_in servaddr;
    // Creating socket file descriptor
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket creation failed");
        exit(EXIT_FAILURE);
    }
    memset(&servaddr, 0, sizeof(servaddr));
    // Filling server information
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(PORT);
    servaddr.sin_addr.s_addr = INADDR_ANY;
    //Connect to the Server
    connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
    printf("Connected to the Server. \n");
    int n, len;
    // Getting Input From Client
    /*
    printf("Please Enter Your Message: ");
    scanf("%s", &clientMsg);
    */
    if (argc == 2)
    {
        for (int i = 0; argc; i  )
        {
            //                      send(sockfd, &argv[i], 10000, 0);
            sendto(sockfd, (const char *) &clientMsg, strlen(&clientMsg),
                MSG_CONFIRM, (const struct sockaddr *) &servaddr,
                sizeof(servaddr));
        }
        printf("Message sent to server. \n");
    }
    else
    {
        printf("Error: Please enter the correct number of arguement. \n");
        close(sockfd);
        printf("Disconneted from the Server. \n");
        exit(1);
    }
    /*
    // Sending message to server
    sendto(sockfd, (const char *) &clientMsg, strlen(&clientMsg),
                    MSG_CONFIRM, (const struct sockaddr *) &servaddr,
            sizeof(servaddr));
    printf("Message sent to server. \n");
    */
    // Receive message from server
    n = recvfrom(sockfd, (char*) buffer, MAXLINE,
        MSG_WAITALL, (struct sockaddr *) &servaddr, &len);
    buffer[n] = '\0';
    printf("Message Received From Server: %s\n", buffer);
    //Disconnet from the Server
    close(sockfd);
    printf("Disconneted from the Server. \n");
    return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 10291
#define MAXLINE 9999
// Server Side code
int main(int argc, char *argv[])
{
    int sockfd;
    char buffer[MAXLINE];
    struct sockaddr_in servaddr, cliaddr;
    // Creating socket file descriptor
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket creation failed");
        exit(EXIT_FAILURE);
    }
    memset(&servaddr, 0, sizeof(servaddr));
    memset(&cliaddr, 0, sizeof(cliaddr));
    // Filling server information
    servaddr.sin_family = AF_INET;    // IPv4
    servaddr.sin_addr.s_addr = INADDR_ANY;
    servaddr.sin_port = htons(PORT);
    // Bind the socket with the server address
    if (bind(sockfd, (const struct sockaddr *) &servaddr, sizeof(servaddr)) < 0)
    {
        perror("bind failed");
        exit(EXIT_FAILURE);
    }
    int len, n;
    len = sizeof(cliaddr);    //len is value/resuslt
    n = recvfrom(sockfd, (char*) buffer, MAXLINE,
        MSG_WAITALL, (struct sockaddr *) &cliaddr, &len);
    buffer[n] = '\0';
    char c;
    while (buffer[c] != '\0')
    {
        char ch = buffer[c];
        if (ch >= 'A' && ch <= 'Z')
            buffer[c] = buffer[c]   32;
        else if (ch >= 'a' && ch <= 'z')
            buffer[c] = buffer[c] - 32;
        c  ;
    }
    //Returning Encrypted String Received By Client
    sendto(sockfd, (const char *) buffer, strlen(buffer),
        MSG_CONFIRM, (const struct sockaddr *) &cliaddr, len);
    close(sockfd);
    return 0;
}

Here's my output: !(https://i.stack.imgur.com/uz7RY.png) !(https://i.stack.imgur.com/nuSI0.png)

Here is what it should look like: !( https://i.stack.imgur.com/JnIFn.png)

The server must be able to respond to multiple consecutive client requests and execute until explicitly quit (^-C). The client should process a single request and then quit on completion.

How should I make it right?

CodePudding user response:

You should start from argv[1], since argv[0] is the program name, not an argument.

for (int i = 1; i < argc; i  ) {
    sendto(sockfd, argv[i], strlen(argv[i]),
        MSG_CONFIRM, (const struct sockaddr *) &servaddr,
        sizeof(servaddr));
}
  • Related