| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
Can't write to socket more than once (C++/STL)
I'm using C++ and STL to write a socket class for a server-side application that will accept multiple connections.
I'm having problems with my function that writes text to the client, it looks like this: Code:
#define MAX_SOCK_WRITE 16384
long Client::Write()
{
int cfd = 0;
long sent = 0;
long total = 0;
long length = 0;
char buff[MAX_SOCK_WRITE+1];
memset(buff,0,MAX_SOCK_WRITE+1);
cfd = GetFd();
length = Out.str().length();
while(total < length) {
Out.get(buff,MAX_SOCK_WRITE+1,'~');
sent = write(cfd, buff, strlen(buff));
if(sent < 0) {
return sent;
}
else {
total += sent;
}
Out.seekg(sent,ios_base::cur);
}
Out.str().clear();
return total;
}
Out is of type stringstream. The socket won't write more than 16384 bytes at a time (system limitation). What I want to be able to do, is to send text to the Client's Out stringstream and then when the write function is called, have everything in Out sent to the socket (in up to 16384 byte chunks if Out contains more than that). If less is sent than is in Out, I want it to continue to send what's left until everything is sent, then I want it to clear the Out stream for the next bit of text. So I do: client::iterator ci; (assume ci points to a valid client at this point) ci->Out << "Some very very very very long text...\n\r"; Then later in the code, I call write: ci->Write(); The problem I'm having is, once I call write the first time, it doesn't seem to allow me to write anything else. What am I doing wrong? Is there a better way to do this? Thanks, GordonE P.S. Here's a simplistic breakdown Code:
client::iterator ci = valid_client;
loop {
ci->Out << "Greetings...\n\r";
ci->Write();
}
Output: Greetings... --- I should get multiple "Greetings...", because I'm inserting it into Out each time in the loop, but I'm only getting it once. Why? |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Can't write to socket more than once (C++/STL) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|