【问题标题】:check value of argc in C [duplicate]检查C中argc的值[重复]
【发布时间】:2012-10-05 22:54:19
【问题描述】:

可能重复:
Checking value of argc

我将坚持使用套接字编程这是我在 linux-ubuntu 上开发的简单的套接字编程 C 代码,但由于表示端口的 argc 值,该代码不起作用并在第一个 if 条件下退出。 我需要帮助:)

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <stdio.h>
const char APRESSMESSAGE[] = "APRESS - For Professionals, By Professionals!\n";

int main(int argc, char *argv[]) {
int simpleSocket = 0;
int simplePort = 0;
int returnStatus = 0;
struct sockaddr_in simpleServer;

if (2 != argc) {
    fprintf(stderr, "Usage: %s <port>\n", argv[0]);
    exit(1);
}
simpleSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
printf("%i\n", simpleSocket);
if (simpleSocket <= -1) {
    fprintf(stderr, "Could not create a socket!\n");
    exit(1);
} else {
    fprintf(stderr, "Socket created!\n");
}
/* retrieve the port number for listening */
simplePort = atoi(argv[1]);
/* set up the address structure */
/* use INADDR_ANY to bind to all local addresses
 */
puts("test1");
bzero(&simpleServer, sizeof(simpleServer));
simpleServer.sin_family = AF_INET;
simpleServer.sin_addr.s_addr = htonl(INADDR_ANY);
simpleServer.sin_port = htons(simplePort);
/*
 bind to the address and port with our socket
 */
returnStatus = bind(simpleSocket, (struct sockaddr *) &simpleServer,
        sizeof(simpleServer));
if (returnStatus == 0) {
    fprintf(stderr, "Bind completed!\n");
} else {
    fprintf(stderr, "Could not bind to address!\n");
    close(simpleSocket);
    exit(1);
}
/* let's listen on the socket for connections */
returnStatus = listen(simpleSocket, 5);
if (returnStatus == -1) {
    fprintf(stderr, "Cannot listen on socket!\n");
    close(simpleSocket);
    exit(1);
}
while (1) {
    struct sockaddr_in clientName = { 0 };
    int simpleChildSocket = 0;
    int clientNameLength = sizeof(clientName);
    /* wait here */
    simpleChildSocket = accept(simpleSocket,
            (struct sockaddr *) &clientName, &clientNameLength);
    if (simpleChildSocket == -1) {
        fprintf(stderr, "Cannot accept connections!\n");
        close(simpleSocket);
        exit(1);
    }
    /* handle the new connection request
     */
    /* write out our message to the client */
    write(simpleChildSocket, APRESSMESSAGE, strlen(APRESSMESSAGE));
    close(simpleChildSocket);
}
close(simpleSocket);
return 0;

}

【问题讨论】:

  • 你能显示你正在使用的命令吗?
  • 我在 eclipse 上运行代码,输出是 Usage: /home/dell/Desktop/C/np/Debug/np
  • 你是如何让它在 Eclipse 中运行的?
  • 尤达条件! if (2 != argc) =P
  • argc 与套接字无关。

标签: c ubuntu argc


【解决方案1】:

我不确定您要问什么,但我相信您对检查传递给程序的参数数量的if 语句的工作方式感到困惑。从命令行,如果用户运行:

myprogram

argv 的值将是 {"myprogram"}argc 将是 1。另一方面,如果您运行:

myprogram 23

argv 的值将是 {"myprogram", "23"}argc 将是 2。我认为您在运行程序时根本没有为端口号传递适当的参数。

【讨论】:

  • 是的,现在的问题是如何使 argc 为 2 或更高,我不明白如何克服这个问题
  • argc 由在命令行中运行程序时传递给程序的参数数量控制。如果您运行myprogram 23 hello arg,那么argc 将等于4。
猜你喜欢
  • 2012-10-05
  • 2020-10-12
  • 2015-01-03
  • 2017-12-22
  • 2017-09-16
  • 2021-12-16
  • 1970-01-01
  • 1970-01-01
  • 2017-04-18
相关资源
最近更新 更多