Issue : Towards the end of the codes, fputs( join, stdout );
prints the string join
correctly. However on the visitor side the same string is not printing correctly.
Server Side : ( everything is normal )
root@s1:/test# gcc server_2.c && ./a.out
You are visitor number : 1
Their Address : [127.0.0.1]
You are visitor number : 2
Their Address : [127.0.0.1]
You are visitor number : 3
Visitor side : ( Only "You
" is printed and it is also lacking it's own new line )
root@s1:/# echo "a" | nc localhost 91
Youroot@s1:/# ^C
root@s1:/# echo "a" | nc localhost 91
root@s1:/# echo "a" | nc localhost 91
Youroot@s1:/#
Code : ( Problem is closer to the end )
int main() {
int one = 1;
struct sockaddr_in My_Address, Their_Address;
socklen_t sin_len = sizeof(Their_Address);
int Path_to_Me = socket(AF_INET, SOCK_STREAM, 0);
setsockopt(Path_to_Me, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int));
int Door = 91;
My_Address.sin_family = AF_INET;
My_Address.sin_addr.s_addr = INADDR_ANY;
My_Address.sin_port = htons(Door);
bind(Path_to_Me, (struct sockaddr *) &My_Address, sizeof(My_Address));
listen(Path_to_Me, 5);
int Me_and_Them;
char response[] = "You are visitor number :";
int count;
count = 0;
for (;;) {
count = count + 1;
char str_count[10];
sprintf(str_count, " %d\n", count);
char* join;
join = ( malloc( strlen(response) + strlen(str_count) + 1 ) );
strcpy(join, response);
strcat(join, str_count);
fputs( join, stdout);
Me_and_Them = accept(Path_to_Me, (struct sockaddr *) &Their_Address, &sin_len);
printf("Their Address : [%s]\n", inet_ntoa(Their_Address.sin_addr));
write(Me_and_Them, join, sizeof(join) - 1);
close(Me_and_Them);
}
return 0;
}