I'm trying to write a code for a socket where I first read the stdin
and send to a socket then read from said socket to send to stdout
and so far I have something that looks more or less like this (before you attack me I also don't know why I have to do this) :
void read_write(int socket_descriptor_file){
int n = 1;
char buffer_in[1024];
while(n>0){
n = fread(&buffer_in,sizeof(buffer_in),1, stdin);
if(n==0) break;
ssize_t sent_status = send(socket_description_file, buffer_in, sizeof(buffer_in), 0);
if(sent_status == -1){
printf("nothing sent");
}
ssize_t receive_status = recv(socket_descriptor_file,buffer_in,sizeof(buffer_in), 0);
if(receive_status == -1){
printf("nothing received ");
}
fwrite(&buffer_in,sizeof (char), sizeof(buffer_in), stdout);
fflush(stdout);
}
}
I'm unsure as to if the said buffer when applying the send function will automatically clear so that I can use the buffer to store the message from the recv
function.
The objective of this code is to simulate a chat between a host 1 and a host 2. So it is necessary to send treat the message immediately.
I'm also apparently supposed to use the poll function but I don't really know how to use it.
I'm not really well informed in this subject so please let me know if there are any further problems with my code. I would be happy to hear them :)
The buffer is only memory. And after a successfull call to send the content is not needed any more. So it is ok to reuse the buffer for recv. Probably you want to store the reveived number of bytes (reveive_status) and not the size of the buffer to your file.