Thread: Looping a socket connection

  1. #1
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63

    Looping a socket connection

    Hi all,

    Is it possible to loop a connection to a site where different pages only means changing 1 number.

    The modified socket details below work fine the first time round, but when it loops the first print (debug) works, but is followed by numbers from the first selection and the printf(enter new number) never shows.


    while(1) {


    printf("Debug Test") ;
    printf("\nEnter New Number ");
    fgets(NewNum,7,stdin) ;
    sBuffer1 = "GET /Path" ;
    char *sBuffer2 = NewNum ;


    strcat(sBuffer1,sBuffer2) ;
    sent_bytes = send(sDesc, sBuffer1, (int) strlen(sBuffer1), 0) ;







    data_bytes = recv(sDesc, buffer, BUFF, 0) ;



    buffer[data_bytes] = '\0' ;
    printf("%s",buffer) ;
    }


    John

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > sBuffer1 = "GET /Path" ;
    > strcat(sBuffer1,sBuffer2) ;

    This by itself is never going to work as you want, regardless of whatever else you're trying to do.

    You need to allocate suitable sized buffers and then use strcpy / strcat / snprintf to build your string.

    > data_bytes = recv(sDesc, buffer, BUFF, 0) ;
    > buffer[data_bytes] = '\0' ;
    1. You should use BUFF-1 so you know you have room for the \0
    2. You should check for success before using data_bytes as an array subscript.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket connection problem
    By raman.x.verma in forum C Programming
    Replies: 2
    Last Post: 10-26-2010, 09:27 PM
  2. reuse a socket connection
    By radeberger in forum Networking/Device Communication
    Replies: 0
    Last Post: 03-18-2009, 11:38 AM
  3. socket connection error
    By sudu in forum C Programming
    Replies: 7
    Last Post: 09-10-2006, 11:24 PM
  4. Socket Connection Still Alive!!
    By maven in forum Networking/Device Communication
    Replies: 2
    Last Post: 07-21-2006, 02:14 PM
  5. Varifying Socket Connection :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 05-11-2002, 03:26 PM

Tags for this Thread