Download your favorite Linux distribution at LQ ISO.
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 08-27-2011, 01:23 AM   #1
LQ Newbie
Registered: Aug 2011
Posts: 6

Rep: Reputation: Disabled
Unhappy char array wont display what it reads from the connected descriptor

This is my code. accept returns the connected descriptor but in the read function the buffer(array) contains some garbage values. I am trying to capture the request made by the browser. I have configured the proxy so it will connect to the desired port. please help

#include <arpa/inet.h>

#include <stdio.h>

#include <time.h>

#include <string.h>

#include <errno.h>

#include <netdb.h>

#include <sys/uio.h>

#include <arpa/inet.h>

#include <netinet/in.h>

#include <sys/types.h>

#include <sys/socket.h>


main(int argc, char **argv)


int listenfd, connfd,n,x;

socklen_t len;

struct sockaddr_in servaddr, cliaddr;

char buff[1501];

time_t ticks;

int yes = 1;

const char *ptr;

if ( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ){

fprintf(stderr, "socket creation failed\n");

exit (1);


bzero(&servaddr, sizeof(servaddr));

servaddr.sin_family = AF_INET;

if (inet_pton(AF_INET,"", &servaddr.sin_addr) <= 0){

printf("inet_pton error for %s", argv[1]);

return 1;


servaddr.sin_port = htons(4616); /* daytime server */

if ( (bind(listenfd, (SA *) &servaddr, sizeof(servaddr))) < 0) {

fprintf(stderr, "bind failed\n");

exit (1);

fprintf(stderr, "binded\n");


if ( listen(listenfd, LISTENQ) < 0) {

fprintf(stderr, "listen failed\n");

exit (1);

fprintf(stderr, "listning\n");


//for ( ; ; ) {

len = sizeof(cliaddr);

if ( (connfd = accept(listenfd, (SA *) &cliaddr, &len)) < 0 ) {

fprintf(stderr, "accept failed\n");

exit (1);


fprintf(stdout, "accepted\n");



while ( n=read(connfd,buff,1500) > 0) {


printf("%s \n",buff);

if (fputs(buff, stdout) == EOF) {

printf("fputs error");

return 1;


printf("[%s] \n",buff);



if (n < 0) {

printf("read error");

return 1;


if ( close(connfd) == -1) {

fprintf(stderr, "accept failed\n");

exit (1);



return 0;

Old 08-27-2011, 01:54 AM   #2
LQ Newbie
Registered: Aug 2011
Posts: 28

Rep: Reputation: Disabled
Initialize the buffer with " "

char buff[1501] = " ";

Last edited by kumawat10; 08-27-2011 at 01:56 AM. Reason: Forgot some points.. ;)
Old 08-27-2011, 10:13 AM   #3
LQ Newbie
Registered: Aug 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
I tried., but its not working, I thing something is wrong in the read function
Old 08-28-2011, 10:20 PM   #4
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
Moved: This thread is more suitable in Programming and has been moved accordingly to help your question get the exposure it deserves.


packet, programing, sniffing, socket

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
How do I point a char pointer to a part of another char array? trist007 Programming 8 11-06-2010 07:56 PM
scanf reading newline into char array while reading 1 char at a time austinium Programming 6 09-26-2010 11:27 PM
How to convert short array to char array? bvkim Programming 4 06-08-2010 09:26 AM
C++ help Dynamic array and "invalid conversion from ‘char’ to ‘char*’" heathf Programming 2 04-25-2009 09:20 PM
bad file descriptor on PEEK of a connected socket DavidHB Programming 3 08-23-2006 04:57 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:19 PM.

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