-
clean out a buffer
hi
how do i clean out a buffer?
i have a char *buffer and an int recmsgleng.
recmsgleng is set to 256.
the client connects to a server and is supposed to receive data of the size 256 bytes.
so i used the recv(sockfd, buffer, recmsgleng, 0) call.
when the data is received i called printf to print the variable
contents to the screen.
when nothing is received, there is still something in the buffer. it prints some strange symbols to the screen.
i wanted to clean out the buffer, in order to prevent printing these strange symbols.
how would i do that?
thank you
-
By "cleaning out the buffer", do you mean reinitializing it with zeros?
memset(buffer,0,recmsgleng);
-
ill provide you with the whole code:
Code:
/*
bannergrab written by [email protected]
02/23/03
usage: ./bannergrab <hostname/ip> <port>
*/
#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#define VERSION "0.1"
void banner(void);
int main (int argc, char *argv[])
{
int sockfd;
struct sockaddr_in dest;
struct hostent *he;
int recmsgleng = 2048;
char *buffer;
int port = atoi(argv[2]);
banner();
if (argc != 3)
{
printf("usage: %s <hostname/ip> <port>\n", argv[0]);
return 1;
}
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
perror("socket");
if((he = gethostbyname(argv[1])) == NULL)
{
perror("gethostbyname");
return 1;
}
dest.sin_family = AF_INET;
dest.sin_port = htons(port);
dest.sin_addr = *((struct in_addr *) he -> h_addr);
memset(&(dest.sin_zero), '\0', 8);
if((connect(sockfd, (struct sockaddr *)&dest, sizeof(struct sockaddr))) == -1)
perror("connect");
printf("connection to %s established\n", argv[1]);
printf("before receiving data: %s\n", buffer);
recv(sockfd, buffer, recmsgleng, 0);
printf("there is the following service running:\n%s\n", buffer);
close(sockfd);
return 0;
}
void banner(void)
{
printf("bannergrab version: %s\nwritten by thread\n", VERSION);
}
try to compile that code, and connect to a server to a port where a service is running.
i am only getting strange symbols printed on the screen, even if there is a service running.
hope you can help me.
thanks
-
What is the return value of the function recv()? Get the appropriate errno code if there was an error, which should clear your doubt.
Also, when connect fails, exit(), there is no point continueing there on.
-
thanks it was the recv function.
the error was: or actually is
Transport endpoint is not connected
-
now the service is running correctly, but i still get
the error:
bad address .
i am going crazy... darn
-
i can tell you that this statement was true
Code:
if((recv(sockfd, buffer, recmsgleng, 0) == -1)
perror("recv");
the output looks like that:
Code:
connection to 192.168.0.1 established
before receiving data: *junk*
recv: bad address
...
...
does that help to solve my problem?
-
thanks!
that solved my problem.
i added a file feature to my program and i am writing to the file with
the fputs() call.
is there a possibilty to append text to an already existing file?
because everytime i run the program, the file is being replaced.
thank you
-
Use the file mode "a" for appending when you create the file stream.