Thread: (socket) - Client question

  1. #1
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302

    ***(SOLVED)***(socket) - Client question

    I'm sorry if I am unclear! I am having a very hard time putting my thoughts/questions into words at the moment!! I know how to write the server. I can code it in 5 minutes. I am now trying to learn how to write the client. Which is not a problem. I just want to know why i have to get the port number from the command line before creating the socket on the client. Or do I? I am using this tutorial as a reference Linux Howtos: C/C++ -> Sockets Tutorial As to where on the server, it can be done after creating the socket. What i am asking is, is there a specific place/time i need to get the ip or port number? Or can i get the port number from the command line as long as its done before i connect to the server.

    Example=>

    WHY CANT THIS
    Code:
    portno = atoi(argv[2]);
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0) 
            error("ERROR opening socket");
        server = gethostbyname(argv[1]);
    BE THIS
    Code:
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0) 
            error("ERROR opening socket");
        server = gethostbyname(argv[1]);
        portno = atoi(argv[2]);
    OR EVEN THIS
    Code:
    bzero((char *) &serv_addr, sizeof(serv_addr));
        portno = atoi(argv[2]);
        serv_addr.sin_family = AF_INET;
        bcopy((char *)server->h_addr, 
             (char *)&serv_addr.sin_addr.s_addr,
             server->h_length);
        serv_addr.sin_port = htons(portno);
    Is there a certain standard for the placement of them?
    Last edited by Annonymous; 06-15-2011 at 08:13 PM. Reason: Post was unclear.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Creating a socket does not involve a port number or an address, so when, how, and if you get these things is irrelevant to the socket() call.

    Bind() for a server and connect() for a client require an address and a port (WRT inet sockets), so you need them for that, which is usually the next step after creating a socket. As long as they are formatted correctly, it's irrelevant when and where you got them.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    Sweet! Thank you MK27

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket ( server client Get pid )
    By Syd in forum C Programming
    Replies: 0
    Last Post: 02-15-2011, 08:00 AM
  2. SSH Client Socket
    By shawon in forum Linux Programming
    Replies: 5
    Last Post: 10-28-2009, 04:01 AM
  3. C-Windows-Socket-(Client/Server)
    By EyesOnly in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-06-2009, 11:12 AM
  4. Socket, client (help-me)
    By darkducke in forum C Programming
    Replies: 3
    Last Post: 12-23-2007, 09:38 AM
  5. socket web client
    By Abila in forum C Programming
    Replies: 0
    Last Post: 06-28-2003, 04:42 PM