kabhis
kabhis

Reputation: 45

how to handle bytestream data in c socket

void *rtp_rec()
{
    memset(rtpbuf, 0, sizeof(rtpbuf));
    unsigned char rtpbuf[2024]="";
    while(1)
    {
        receive = recvfrom(sock1, rtpbuf,sizeof(rtpbuf) ,0,(struct sockaddr*)&serverrtp,(socklen_t*)&sizeof(serverrtp););
        n = sendto(sock2,rtpbuf,strlen(rtpbuf),0,(struct sockaddr *)&sendtoother,sizeof(sendtoother));
    }
    return 0;
}
////////////////////////////////////////////////////////////////////////////
  1. I am receiving 180 byte data packet continuosly (byte by byte) in rtpbuf(buffer) using recvfrom() on socket (sock1).

  2. and I want to send same data packet of 180 byte to another socket (sock2) using sendto().

But my sendto() function sending 1 byte data packet, recvfrom() also receving data byte by byte.

Please suggest some logic.

Upvotes: 0

Views: 182

Answers (1)

dbush
dbush

Reputation: 223689

You need to check the value of recvfrom() to make sure there was no error, and to ensure you received the proper number of bytes. Also, you probably want to send as many bytes as you're receiving rather than using strlen(rtpbuf) just in case you have null bytes in the received data.

receive = recvfrom(sock1, rtpbuf,sizeof(rtpbuf) ,0,(struct sockaddr*)&serverrtp,(socklen_t*)&sizeof(serverrtp));
if (receive == -1) {
    perror("recvfrom failed");
} else {
    printf("received %d bytes\n", receive);
    n = sendto(sock2,rtpbuf,receive,0,(struct sockaddr *)&sendtoother,sizeof(sendtoother));
}

Upvotes: 1

Related Questions