Thread: writing a client program - socket programming Linux

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    13

    writing a client program - socket programming Linux

    Hi all, I am trying to write a client side program through socket programming. My question is that how can I specify port for my client. Suppose I want to use port number 5454 for the TCP connection to connect to a server listening at any port. How can I do that ? waiting in anticipation

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Useful Links And Good Books
    Perhaps start with the "beej" tutorials.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    13
    thanks for reply. I was a little confused, now I think I got the thing. I am now binding the port at the client side just like we bind the port at the server side. I think it is the right approach

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    No, you don't bind() the client socket at all. You use connect() and specify the server address there.
    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

  5. #5
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    You can take the port number as a CLA. So for the server:

    Code:
    int portno = atoi(argv[1]); //use atoi to convert the 2nd CLA to an int. atoi means, ascii to integer.
    Code:
    serv_addr.sin_port = htons(portno); //serv_addr is a variable of the struct, sockaddr_in.
                                                       //sin port is a field inside of the struct of type unsigned short. 
                                                       //you must use htonl(host to network long). Its converts host byte order to network byte order.
    Now you can specify the portno on the command line as the 2nd arg.

    Same applies to the client.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket programming - input from client problem
    By Kitase in forum C Programming
    Replies: 2
    Last Post: 10-12-2010, 07:53 AM
  2. Replies: 2
    Last Post: 06-30-2010, 06:52 PM
  3. writing dhcp client code using sockets on Linux
    By dash in forum Linux Programming
    Replies: 3
    Last Post: 08-04-2009, 10:20 AM
  4. socket programming in linux
    By crazeinc in forum C Programming
    Replies: 1
    Last Post: 05-27-2005, 07:40 PM
  5. Linux Socket programming help
    By Keshi in forum C Programming
    Replies: 3
    Last Post: 03-15-2005, 03:58 PM

Tags for this Thread