C/C++ Help
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Remember me

Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
Unread Dev Articles Community Forums Sponsor:
Old June 2nd, 2003, 10:30 PM
GordonE GordonE is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
Join Date: Jun 2003
Location: Ohio
Posts: 3 GordonE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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:


#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];


     cfd = GetFd();

     length = Out.str().length();
     while(total < length) {
          sent = write(cfd, buff, strlen(buff));
          if(sent < 0) {
               return sent;
          else {
               total += sent;

     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:

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?



P.S. Here's a simplistic breakdown


client::iterator ci = valid_client;
loop {
     ci->Out << "Greetings...\n\r";


--- 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?

Reply With Quote

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Can't write to socket more than once (C++/STL)

Developer Shed Advertisers and Affiliates

Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 

Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.

© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap