如何在套接字中传递和接收命令行参数?

How to pass and receive cmd line arguments in socket?

我正在使用 C 开发套接字,需要将命令行参数从客户端传递到服务器。然后服务器需要切换 sting 的大小写并将其发送回客户端。我坚持将命令行参数传递给服务器,然后在切换大小写后将其发回。这是我目前所拥有的:

#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 arguments. \n");
        close(sockfd);
        printf("Disconnected 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] = '[=10=]';
    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] = '[=11=]';
    char c;
    while (buffer[c] != '[=11=]')
    {
        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;
}

这是我的输出: !(https://i.stack.imgur.com/uz7RY.png) !(https://i.stack.imgur.com/nuSI0.png)

它应该是这样的: !( https://i.stack.imgur.com/JnIFn.png)

服务器必须能够响应多个连续的客户端请求并执行直到显式退出 (^-C)。 客户端应处理单个请求,然后在完成时退出。

我应该怎么做才对?

您应该从 argv[1] 开始,因为 argv[0] 是程序名称,而不是参数。

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