c - Code blocked at read in Client Server application in Linux -
i developing client-server-client chat application, when client enter other client wont receive until input i.e code blocked @ read. how unlock read??
code:
if(fd_isset(sfd,&writefds)) { printf("\n%s: ",my_name); gets(message); memset(p, 0, sizeof(sbuf) - header_len); strcpy(p,message); if(sendto(sfd,sbuf, header_len+strlen(message),0,(struct sockaddr*)&ssin,serverlen)<0) { perror("write failed"); exit(-1); } }
strace:
select(4, [3], null, null, {5, 0}) = 1 (in [3], left {4, 999994}) recvfrom(3, "\5\0rohit\4\0saurhellllo\0\0\0\0\0\0\0\0\0\0\0\0"..., 200, 0, {sa_family=af_inet, sin_port=htons(1000), sin_addr=inet_addr("127.0.0.1")}, [16]) = 200 write(1, "rohitr : hellllo\n", 17rohitr : hellllo ) = 17 write(1, "\n", 1 ) = 1 write(1, "saur: ", 6saur: ) = 6 read(0,
"when attempting read file (other pipe or fifo) supports non-blocking reads , has no data available:
if o_nonblock set, read() shall return -1 , set errno [eagain].
if o_nonblock clear, read() shall block calling thread until data becomes available.
the use of o_nonblock flag has no effect if there data available."
edit: although don't see using read here. replace gets
read
or try use fcntl
make file descriptor non blocking :
int flags = fcntl(fd, f_getfl, 0); fcntl(fd, f_setfl, flags | o_nonblock);
fcntl
manual here
Comments
Post a Comment