// Sends a string over a TCP/IP connection to a connection file descriptor // Usage: dsend(connfd, "Blah blah %s", moreblah); // Problem: Sometimes messages are sent, but more often than not they never get sent // if I change buf (in dsend) to buf[2048] and just vsnprintf 2048 size to start, then it works // however, I want to support an arbitrary length sent to dsend() // Any ideas appreciated! ssize_t writen(int fd, const void* vptr, size_t n) { size_t nleft; ssize_t nwritten; const char* ptr; ptr = vptr; nleft = n; while( nleft > 0 ) { if( (nwritten = write(fd, ptr, nleft)) <= 0 ) { if( errno == EINTR ) { nwritten = 0; // and call write() again } else { return -1; } } nleft -= nwritten; ptr += nwritten; } return n; } void dsend(int sockfd, const char *fmt, ...) { va_list args; char c, *buf; int buf_len; if( sockfd >= 0 ) { va_start(args, fmt); buf_len = vsnprintf(&c, 1, fmt, args) + 1; buf = malloc(buf_len); vsnprintf(buf, buf_len, fmt, args); va_end(args); writen(sockfd, buf, buf_len+1); free(buf); } }