Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
04-11-2013, 10:50 AM
|
#1
|
Member
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111
Rep: 
|
Sockets - Prevent Partial Writes
I have written a socket client program in C++. At some point, I have to send data, and this is how I do it:
lenSent = write(serverSocket,sendBuf, len);
I can use send() instead of write(). It makes no difference to this question. I have set it to be non-blocking, because the thread must not get stuck. Unfortunately, it is connecting to a server which exits if I do a partial write to the socket.
How can I prevent a non-blocking socket from doing a partial write? Is there some way to poll it and make sure that it can handle the number of bytes I wish to send?
Thanks.
Brandon
|
|
|
04-11-2013, 12:20 PM
|
#2
|
Member
Registered: May 2008
Location: France
Distribution: Ubuntu, Debian
Posts: 343
Rep:
|
|
|
|
04-11-2013, 12:57 PM
|
#3
|
Member
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111
Original Poster
Rep: 
|
The man page for send says:
"The select(2) call may be used to determine when it is possible to send more data."
This isn't really enough for me to know how to do it. I have never used select before.
|
|
|
04-11-2013, 02:57 PM
|
#4
|
Member
Registered: May 2008
Location: France
Distribution: Ubuntu, Debian
Posts: 343
Rep:
|
Have you read this: http://beej.us/guide/bgnet/output/ht...d.html#sendall ?
Here is an example:
Code:
/*
* send_full() - Socket must be opened in non-blocking mode
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/select.h>
#define SEND_RECV_TIMEOUT_SEC 1
#define SEND_RECV_TIMEOUT_USEC 0
enum {
SELECT_ERROR,
SELECT_TIMED_OUT,
SELECT_TRUE,
SELECT_FALSE
};
int writable_data_is_available_on_socket(int sck)
{
fd_set write_set;
struct timeval timeout;
int i;
FD_ZERO(&write_set);
FD_SET(sck, &write_set);
timeout.tv_sec = SEND_RECV_TIMEOUT_SEC;
timeout.tv_usec = SEND_RECV_TIMEOUT_USEC;
if ((i = select(sck + 1, NULL, &write_set, NULL, &timeout)) == -1) {
fprintf(stderr, "select() error: %s\n", strerror(errno));
fflush(stderr);
return SELECT_ERROR;
} else if (i == 0) {
return SELECT_TIMED_OUT;
} else {
if (FD_ISSET(sck, &write_set))
return SELECT_TRUE;
else
return SELECT_FALSE;
}
}
/*
* Return n bytes sent or -1 if error (connection closed by server or ?)
*/
int send_full(int sck, const char *str)
{
int len = strlen(str), i, j = 0;
while (writable_data_is_available_on_socket(sck) == SELECT_TRUE) {
if ((i = send(sck, str + j, len, 0)) != -1) {
if (i > 0) {
j += i;
len -= i;
if (len == 0)
break;
}
} else {
j = -1;
if (errno == EPIPE) {
fprintf(stderr, "Connection closed by server\n");
fflush(stderr);
} else {
fprintf(stderr, "send() error: %s\n", strerror(errno));
fflush(stderr);
}
break;
}
}
return j;
}
|
|
|
04-11-2013, 03:01 PM
|
#5
|
Member
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111
Original Poster
Rep: 
|
Thanks! Does select do the write itself?
|
|
|
04-11-2013, 03:17 PM
|
#6
|
Member
Registered: May 2008
Location: France
Distribution: Ubuntu, Debian
Posts: 343
Rep:
|
Quote:
Originally Posted by Brandon9000
Thanks! Does select do the write itself?
|
No, you should use send_full() instead of write() inside your code.
|
|
|
04-11-2013, 03:36 PM
|
#7
|
Member
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111
Original Poster
Rep: 
|
Thanks again. I am also experimenting with checking to see how many unsent bytes are in the socket's internal buffer and waiting until it reaches zero with:
int error, value;
for (.....value > 0.....)
error = ioctl(serverSocket, SIOCOUTQ, &value);
Brandon
|
|
|
All times are GMT -5. The time now is 09:48 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|