当前位置: 技术问答>linux和unix
socket写数据的问题,高手请进
来源: 互联网 发布时间:2015-10-09
本文导语: fcntl( fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK ); fd_set wfds; struct timeval tv; int retval; /* Watch stdin (fd 0) to see when it has input. */ FD_ZERO(&wfds); ...
fcntl( fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK );
fd_set wfds;
struct timeval tv;
int retval;
/* Watch stdin (fd 0) to see when it has input. */
FD_ZERO(&wfds);
for(;;) {
FD_SET(fd, &wfds);
/* Wait up to five seconds. */
tv.tv_sec = 1;
tv.tv_usec = 0;
retval = select(1+fd, NULL, &wfds, NULL, &tv);
/* Don't rely on the value of tv now! */
if (retval == -1)
perror("select()");
else if (retval && FD_ISSET(fd, &wfds)) {
//printf("Data is available now.n");
memset(buffer, 0x00, 1024);
net_write(fd, buffer, 1024);
s+=1024;
printf("Data Send total %dn", s);
if(s>=l) {
FD_CLR(fd, &wfds);
break;
cout
fd_set wfds;
struct timeval tv;
int retval;
/* Watch stdin (fd 0) to see when it has input. */
FD_ZERO(&wfds);
for(;;) {
FD_SET(fd, &wfds);
/* Wait up to five seconds. */
tv.tv_sec = 1;
tv.tv_usec = 0;
retval = select(1+fd, NULL, &wfds, NULL, &tv);
/* Don't rely on the value of tv now! */
if (retval == -1)
perror("select()");
else if (retval && FD_ISSET(fd, &wfds)) {
//printf("Data is available now.n");
memset(buffer, 0x00, 1024);
net_write(fd, buffer, 1024);
s+=1024;
printf("Data Send total %dn", s);
if(s>=l) {
FD_CLR(fd, &wfds);
break;
cout