Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-16-2006, 06:53 PM   #1
Registered: Jul 2005
Distribution: Slackware 11, Solaris 10
Posts: 143

Rep: Reputation: 15
Networking Code Problem

I study Beej and produced the following 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 ""
#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)
	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)
	input[64] = input_terminal();
	if (send(my_socket, &input[64], strlen(input), 0) == -1)
	while(numbytes > 0)
		if ((numbytes = recv(my_socket, buff, MAXDATASIZE, 0)) == -1)
		buff[numbytes] = '\0';
		printf("%s\n", buff);
		tot_bytes += numbytes;

	return 0;
It basicly connects to 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):

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?
Old 05-16-2006, 09:19 PM   #2
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
Old 05-17-2006, 01:15 AM   #3
Wim Sturkenboom
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

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

char input_terminal()
	char string[64];
	printf("Command Line: ");
	scanf("%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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
small syntax problem with C code (implemented in Code Composer Studio) illiniguy3043 Programming 6 01-07-2008 02:14 AM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
What is the problem with the below C++ code? coolguy_iiit Programming 4 01-24-2005 12:28 PM
Interactive Networking Code gr33ndata Programming 6 03-03-2004 08:34 AM
I need some networking source code!!! lamvd Linux - Newbie 2 02-23-2003 11:02 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:23 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration