▲ 20 r/C_Programming
recvfrom() function always return 10 bytes on UDP connection socket
Hey everyone,
I was practicing Socket Programming using C and ran into an issue. The recvfrom() function always returns 10 bytes on UDP socket regardless of the input given.
Here is the code which I have written:
udp_server.c:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define PORT 6900
#define LINES 128 // QUERY: Changing the value to 4096 breaks the program
int main(){
int sockfd, numrec, byterec;
char recvline[LINES + 1];
struct
sockaddr_in
ser_addr;
const struct
sockaddr_in
clnt_addr;
socklen_t
len = sizeof(clnt_addr);
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
perror("Socket Error!!");
exit(1);
}
memset(&ser_addr, '\0', sizeof(ser_addr));
ser_addr.sin_family = AF_INET;
ser_addr.sin_addr.s_addr = htonl(INADDR_ANY);
ser_addr.sin_port = htons(PORT);
bind(sockfd, (struct
sockaddr
*) &ser_addr, sizeof(ser_addr));
while(1){
if((numrec = recvfrom(sockfd, recvline, LINES, 0, (struct
sockaddr
*)&clnt_addr, &len)) < 0){
perror("recvfrom error");
exit(1);
}
// QUERY: FROM WHERE byterec IS FETCHING THE VALUE
fprintf(stdout, "Connection from %s, port %d. Received %d bytes.\n", inet_ntoa(clnt_addr.sin_addr), ntohs(clnt_addr.sin_port), byterec);
fflush(stdout);
// recvline[byterec]='\0';
if(strcmp(recvline, "Terminate\n") == 0){
fprintf(stdout, "a client want me to terminate\n");
fflush(stdout);
break;
}
if(sendto(sockfd, recvline, byterec, 0, (struct
sockaddr
*)&clnt_addr, len) < 0){
perror("sendto error");
exit(1);
}
}
fprintf(stdout, "server normal end\n");
fflush(stdout);
return 0;
}
udp_client.h:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#define PORT 6900
#define LINES 20
int main(){
int sockfd, bytesent, numrec, byterec;
char sendLine[LINES], recvLine[20];
struct sockaddr_in ser_addr;
struct sockaddr_in echo_addr;
socklen_t len = sizeof(echo_addr);
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
perror("Socket Error!!");
exit(1);
}
memset(&ser_addr, '\0', sizeof(ser_addr));
ser_addr.sin_family = AF_INET;
ser_addr.sin_addr.s_addr = htonl(INADDR_ANY);
ser_addr.sin_port = htons(PORT);
while(1){
printf("==>");
fflush(stdout);
fgets(sendLine, LINES, stdin);
if(strcmp(sendLine, "exit\n") == 0){
break;
}
bytesent = sendto(sockfd, sendLine, strlen(sendLine), 0, (struct sockaddr*) &ser_addr, sizeof(ser_addr));
if(bytesent == -1){
perror("sendto error");
exit(1);
}
if(strcmp(sendLine, "Terminate\n") != 0){
printf("----VARIABLE VALUES BEFORE----\n");
printf("recvLine size: %d\n", sizeof(recvLine));
printf("echo_addr size: %d\n", sizeof(echo_addr));
printf("len var address: %p\n", &len);
printf("len value: %d\n", len);
printf("byterec value: %zd\n", byterec);
printf("\n");
if ((byterec = recvfrom(sockfd, recvLine, sizeof(recvLine), 0, (struct sockaddr*) &echo_addr, &len)) < 0){
perror("recvfrom error");
exit(1);
}
printf("----VARIABLE VALUES AFTER----\n");
printf("recvLine size: %d\n", sizeof(recvLine));
printf("echo_addr size: %d\n", sizeof(echo_addr));
printf("len var address: %p\n", &len);
printf("len value: %d\n", len);
printf("byterec value: %zd\n", byterec);
}
// recvLine[byterec] = '\0';
printf("from server: %s\n", recvLine);
}
close(sockfd);
printf("echo client normal end\n");
return 0;
}
Additional Info: I am using cygwin for the compiler.
What I tried:
- I tried to use char datatype for
recvLinebuffer variable. In this case, the value ofbyterecvariable became1. - I gave the input of length more than the size of buffer. The value
byterecvariable changed to10. I assumed that it might be happening because of buffer overflow. Therefore, I tried step 3. - I gave the input of length less than the size of buffer. Still, the value of
byterecvariable is getting changed to10. - I tried to change the buffer size,
LINESconstant, tried to match their values. Doesn't matter what I try? the value ofbyterecalways changes to 10 afterrecvfrom()function.
After trying to debug for the whole day and countless googling, I am unable to figure out what's wrong with my code. Any help would be appreciated.
Thanks in advance.
u/Escapshion — 2 days ago