2
I am receiving a lot of data coming from a server, the client(the code below), after sending a message, it receives another, but I would like to know the size of the message that is coming from the server to allocate the variable before going to the recv
(assuming the server sends random text sizes).
- Is there any function to collect the size of the Buffer coming by Socket?
- You can get the size of the data from Socket for
fstat
orfseek
? - How can I make this implementation?
client.c
/*Socket and Network headers*/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#define PORT 8000
#define TCP 6
int main(int argc, char *argv[]){
int _sock;
int res;
char buffer[256];
struct sockaddr_in serv; //socket address struct
struct hostent *server;
// int socket(sin_family, socket_type, protocol)
// protocol TCP equal 6 (TRANSMISSION CONTROL PROTOCOL)
_sock = socket(AF_INET, SOCK_STREAM, TCP);
if(_sock < 0)
return 1;
server = gethostbyname("127.0.0.1");
// set zero in buffer
bzero( (char *) &serv, sizeof(serv));
// configure connection
serv.sin_family = AF_INET;
serv.sin_port = htons(PORT);
bcopy((char *) server->h_addr,
(char *) &serv.sin_addr.s_addr,
server->h_length);
//serv.sin_addr.s_addr = INADDR_ANY;
int len = sizeof(serv);
res = connect(_sock, (struct sockaddr *) &serv, len);
if(res < 0)
return 2;
bzero(buffer, 256);
fgets(buffer,255,stdin);
res = write(_sock,buffer, strlen(buffer));
int size;
ioctl(_sock, FIONREAD, &size);
printf("%d\n", size);
char c;
while(1)
if(read(_sock, &c, 1) != 1)
break;
else
printf("%c",c);
// res = read(_sock, buffer, 255);
//if(res)
// puts(buffer);
//strcpy(buffer,"return message.");
close(_sock);
}