I want to send let's say 1GB data in 32 bits chunk so inside the for-loop I'm doing send(client_sock, buffer, tinfo->bufferSize, 0);
Which is fine. But then after that, I'm receiving the response which is throwing an error
(as below) after the 1st iteration. If I put the recv
out side for loop
then it works fine. But then I won't be able to ensure if each chunk is sent properly.
My question is
- Why can't we iterate over
receive
when we can iterate over
send
? - Is it ok to keep receive out of the
for
and don't worry about if every message is sent?
N.B. - Can't use external libraries, its a POC project for college. 'send 1 Gb of data in 32 bits chunk over socket'
void *tcpClient(void *arg) {
struct thread_info * tinfo = (struct thread_info *)arg;
char * buffer = (char *)malloc(sizeof(char)*(tinfo->bufferSize));
memset(buffer, 'a', sizeof(char)*(tinfo->bufferSize));
int client_sock = socket(AF_INET, SOCK_STREAM, 0);
connect(client_sock, (struct sockaddr *)&(tinfo->server_addr), sizeof(struct sockaddr));
long noOfChunks = oneGb/ tinfo->bufferSize;
for (int j = 0; j < noOfChunks; ++j) {
int totalBytesRcvd = 0;
int bytesRcvd = 0;
send(client_sock, buffer, tinfo->bufferSize, 0);
while (totalBytesRcvd < tinfo->bufferSize)
{
if ((bytesRcvd = recv(client_sock, buffer, tinfo->bufferSize, 0)) <= 0)
error("recv() failed or connection closed prematurely");
totalBytesRcvd += bytesRcvd; /* Keep tally of total bytes */
}
printf(buffer); /* Print the echo buffer */
}
free(buffer);
}
Error I'm getting:
Signal: SIGPIPE (signal SIGPIPE)
Terminated due to signal 13