Socket:multiple reads and writes, how to avoid overflow?
I've been serching on this topic for a while, both on google and here. I've read "Beej's Guide to Network Programming" as someone here sugested and the FAQ section about sockets where Hammer talks about overflow and my code is like his, and I still get rubish when reading and writing.
Here's the part of the code that matters:
Code:
#define BUF_CLIENT 50
SERVER SIDE
void* handle_cliente(int client_fd){
int nread = 0,nwrite;
char find[BUF_CLIENT];
char buffer[BUF_CLIENT];
nread = read(client_fd, buffer, BUF_CLIENT-1);
buffer[nread-1] = '\0';
printf("nread: %d",nread); //just so I can see
strcpy(find,host_lookup(buffer)); //don't bother with this
printf("\n.%s.\n",buffer); //just so I can see
printf("tam:%d",strlen(find));
find[strlen(find)]='\0';
printf("\n.%s.\n",find); //just so I can see
nwrite=write(client_fd,(void*)find, strlen(find));
printf("\nwrite:%d\n",nwrite); //just so I can see
close(client_fd);
return NULL;
}
CLIENT SIDE
...
char ip[BUF_CLIENT];
write(fd, argv[3], strlen(argv[3])+1);
nread = read(fd, ip, BUF_CLIENT-1);
ip[nread]='\0';
printf("nread:%d",nread); //just so I can see
printf("IP: %s.\n",ip);
close(fd);
return 0;
...
I've been trying to solve this problem all afternoon and would apreciate a quick fix as I believe this is easy to solve :p
Thanks ;)
EDIT: Using bzero() on the arrays before storing data into them is going in the right direction? Still not working..