0

Possible Duplicate:
Checking value of argc

I'm going to stuck with socket programming this is my socket programming simple C code developed over linux-ubuntu but the code don't work and exit in the first if condtion becuse of the argc value that represents the port. i need help please :)

#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;

}

Community
  • 1
  • 1

1 Answers1

0

I am not exactly sure what you are asking, but I believe you are confused about how the if statement that checks the number of arguments passed to your program works. From the command-line, if the user runs:

myprogram

The value of argv will be {"myprogram"} and argc will be 1. On the other hand, if you run:

myprogram 23

The value of argv will be {"myprogram", "23"} and argc will be 2. I think you are simply not passing the appropriate argument for the port number when running the program.

nickolayratchev
  • 1,136
  • 1
  • 6
  • 15
  • yes, the problem now how can i make the argc to be 2 or higher, i don't understand how to overcome that – MostafaKhattab Oct 15 '12 at 21:57
  • `argc` is controlled by the number of arguments you pass to the program when you run it in the command line. If you run `myprogram 23 hello arg`, then `argc` will equal 4. – nickolayratchev Oct 15 '12 at 22:03