Reputation: 47
I have used scanf()
to hold the screen and take input from user in a multi threaded client side socket program.
scanf()
is not holding the screen here and user is not able to enter data.
The code of thread function is is:
void *send_data(void *num)
{
int *sock_s=(int *) num;
int sock=*sock_s;
char msg[50];
printf("Enter data:");
while(1) {
scanf("%s",msg);
if(strcmp(msg,"exit")==0)
{
break;
}
send(sock_s,msg,sizeof(msg),0);
}
send(sock,msg,sizeof(msg),0);
}
and the code create tthis thread is:
status_s=pthread_create(&thread_s,NULL,send_data,(void *)&sock);
if(status_s==0)
printf("sending");
Upvotes: 0
Views: 592
Reputation: 1526
try this and see whether it works
printf("Enter data:");
scanf("\n%s",msg);
while(strcmp(msg,"exit")!=0)
{
send(sock_s,msg,sizeof(msg),0);
scanf("\n%s",msg);
}
i think there might be some problem with the while loop
Upvotes: -1
Reputation: 213960
scanf(" %s", msg);
or similar.scanf
and similar library functions from inside a thread, they are not thread-safe.scanf
is vulnerable and will get buffer overruns.Upvotes: 2