LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 08-27-2011, 02:23 AM   #1
TAN
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>




int

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,"127.0.0.1", &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");

//}



for(;{

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

buff[n]=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);

}



close(listenfd);

return 0;

}
 
Old 08-27-2011, 02:54 AM   #2
kumawat10
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 02:56 AM. Reason: Forgot some points.. ;)
 
Old 08-27-2011, 11:13 AM   #3
TAN
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, 11:20 PM   #4
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

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


Reply

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


All times are GMT -5. The time now is 01:30 AM.

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