LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Networking Code Problem (https://www.linuxquestions.org/questions/programming-9/networking-code-problem-445502/)

Mercurius 05-16-2006 06:53 PM

Networking Code Problem
 
I study Beej and produced the following code :

Code:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <strings.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <string.h> //needed for strlen which is called in send()

#define DEST_PORT 80
#define DEST_IP "68.142.197.74"
#define MAXDATASIZE 300

char input_terminal()
{
        char string[64];
        printf("Command Line: ");
        scanf("%s", &string);
        printf("Sending Data ... \n");
        return string[64];
}

int main()
{
        int tot_bytes;
        int numbytes;
        char input[64];
        char buff[MAXDATASIZE];
        int my_socket; //creeaza variabila ce va stoca socket-ul
        struct sockaddr_in destination_address; //stocheaza adresa de conectare
       
        numbytes = MAXDATASIZE;
        tot_bytes = 0;
       
        if ((my_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
        {
                perror("socket");
                exit(1);
        }
       
        destination_address.sin_family = AF_INET; //host to byte order
        destination_address.sin_port = htons(DEST_PORT); //short, network to byte order
        destination_address.sin_addr.s_addr = inet_addr(DEST_IP); //destination ip
        bzero(&(destination_address.sin_zero), 8); //zero the rest of the struct

        if ((connect(my_socket, (struct sockaddr*)&destination_address, sizeof(struct sockaddr))) == -1)
        {
                perror("connect");
                exit(1);
        }
       
        input[64] = input_terminal();
       
        if (send(my_socket, &input[64], strlen(input), 0) == -1)
        {
                perror("send");
                exit(1);
        }
       
        while(numbytes > 0)
        {
                if ((numbytes = recv(my_socket, buff, MAXDATASIZE, 0)) == -1)
                {
                        perror("recv");
                        exit(1);
                }
                buff[numbytes] = '\0';
                printf("%s\n", buff);
                tot_bytes += numbytes;
        }

        close(my_socket);
       
        return 0;
}

It basicly connects to www.yahoo.com on port 80, prompts for a command line and of course I write "GET HTML". And then nothing happends ... If I take away the command line function and use a string to the send() function "GET HTML\n" it works very fine. gdb shows some weird data (im not a gdb expert):

Code:

54              input[64] = input_terminal();
(gdb) next
Command Line: GET HTML
Sending Data ...
56              if (send(my_socket, &input[64], strlen(input), 0) == -1)
(gdb) next
62              while(numbytes > 0)
(gdb) next
64                      if ((numbytes = recv(my_socket, buff, MAXDATASIZE, 0)) == -1)
(gdb) next

What is the problem that it does not want to receive data?

graemef 05-16-2006 09:19 PM

Well input[64] = input_terminal(); says in the 64th character copy the result from the function input_terminal(). Not I suspect what you want to do.

If I remember correctly scanf("%s",... will read in the first character up to the first white space (hence it will be GET rather than GET HTML) but check this by printing out the value.

What you want to do is to pass input without the square brackets, in effect this will be a pointer to the first location in the character array.

Wim Sturkenboom 05-17-2006 01:15 AM

You're missing 2 include file (stdio.h and arpa/inet.h)


Code:

char input_terminal()
{
        char string[64];
        printf("Command Line: ");
        scanf("%s", &string);
        printf(">>%s<<",string);
        printf("Sending Data ... \n");
        return string[64];
}

string is already a pointer, so don't use the ampersand in front of it
For debugging, I've added an additional printf (which proofs graemef's point that scanf does not read everything). Also, scanf does not read the newline. You can see that as the '<<' occurs on the same line.


All times are GMT -5. The time now is 12:06 PM.